开发者

onclosing form return value

Is there a way tha a form retur开发者_如何学Cns a value? something like ...

sub main() 

   Dim task as new TaskForm()
   dim res as integer=0
   res = task.opendialog()

end sub


If you use ShowDialog, you can set the form's DialogResult property to a value. Keep in mind that the form can't return arbitrary results this way, only the values of the DialogResult enumeration.

Otherwise, you'd have to set the form's Tag Property and manually retrieve it after the form is closed, but before you discard the reference to it.


It depends a bit on what you want to have returned. If you want to show a dialog and figure out which button that was used for closing it (OK, Cancel, ...) you can display the form using the ShowDialog method, which returns a DialogResult value:

DialogResult result = theForm.ShowDialog();
if (result == DialogResult.OK)
{
    // OK was clicked
}

If you want to return some other value, the easiest way is to expose this through a property on the form, or to provide a static method in the form that will create an instance of it, collect the needed input and than return the data:

class NameInputForm : Form
{
    // form initialization / construction left out for brevity, but let's assume
    // it contains a TextBox control called UserNameTextBox and a button with
    // its DialogResult property set to OK

    public static string GetNameFromUser()
    {
        using (NameInputForm form = new NameInputForm())
        {
            if (form.ShowDialog() == DialogResult.OK)
            {
                return form.UserNameTextBox.Text;
            }
        }
        return "";
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜