开发者

user input and acceptance inside a function

My function accepts user input and then do somework when user clicks ok.

private void cannyToolStripMenuItem_Click(object sender, EventArgs e) { canny(); }

private void canny()
        {

// get user input
// if user clicks ok 
          if (ok button is clicked)
              {
                  messagebox.show(" you clicked ok")
                // 
               //do dome work
               //
               }


         }

But I can't see an开发者_如何学运维y messagebox. What I am missing.

 private void ok_Click(object sender, EventArgs e)
        {
// should I add here some thing
        }

what i am missing. regards,


I think what you are trying to achieve is to get the result from a dialog box. If that is the case you want to do the following:

private void ShowDialogAndDoSomethingBasedOnTheResult()
{
    DialogResult result = MessageBox.Show(
        "Dialog text",
        "Caption to go in title bar",
        MessageBoxButtons.OK);
    if (result == DialogResult.OK)
    {
        //Do work
    }
}

See http://msdn.microsoft.com/en-gb/library/0x49kd7z.aspx for more examples.


Well, yes, you do:

private void ok_Click(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.Ok;
}

Which closes the dialog, it will only stay running as long as its DialogResult property is None. It isn't strictly necessary, you can also use the designer. Change the button's DialogResult property, now you don't need to write the code. That is however not often appropriate, you usually want to check if the user provided all the information you require. Ymmv.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜