C #, winform, showdialog, and sub-form passing values to the parent form

Source: Internet
Author: User

Call the showdialog Method Code The execution is paused. The execution will continue after the form relation of the showdialog method is called. In addition, the form can return a dialogresult value, which describes the reason for closing the form, such as OK, cancel, Yes, and no. In order for the form to return a dialogresult, you must set the dialogresult value of the form or set the dialogresult attribute on a button of the form.

Example:
The following is the subform code. You must enter the phone and return it to the parent form.

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;

Namespace windowsapplication1
{
Public partial class phone: Form
{
Public phone ()
{
Initializecomponent ();
Btnok. dialogresult = dialogresult. OK;
Btnok. dialogresult = dialogresult. Cancel;
}
Public String phonenumber
{
Get {return textbox1.text ;}
Set {textbox1.text = value ;}
}
Private void phone_load (Object sender, eventargs E)
{

}
}
}

It does not contain any code for processing button click events. Because the dialogresult attribute of each button is set, the form disappears after you click OK or cancel. The following code shows how to call the phone dialog box in the parent form.
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;

Namespace windowsapplication1
{
Public partial class form7: Form
{
Public form7 ()
{
Initializecomponent ();
}

Private void button#click (Object sender, eventargs E)
{
Phone FRM = new phone ();
FRM. showdialog ();
If (FRM. dialogresult = dialogresult. OK)
{
Label1.text = "phone number is" + frm. phonenumber;

}
Else if (FRM. dialogresult = dialogresult. Cancel)
{
Label1.text = "form was canceled ";

}
FRM. Close ();
}
}
}

It looks very simple. Create a new phone object frm and call frm. the showdialog method is to stop the code, wait for the phone form to return, and then check the dialogresult attribute of the phone form. Because the form has not been released and is invisible, you can still access the public attribute phonenumber, once the required data is obtained, you can use the form close method.
Everything is normal, but what if the returned format is incorrect? Put the showdialog method in the loop and you can call it again, so that the user can re-enter the method to get the correct value.

The above code is changed to the following.

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;

Namespace windowsapplication1
{
Public partial class form7: Form
{
Public form7 ()
{
Initializecomponent ();
}

Private void button#click (Object sender, eventargs E)
{
Phone FRM = new phone ();

While (true)
{
FRM. showdialog ();
If (FRM. dialogresult = dialogresult. OK)
{
Label1.text = "phone number is" + frm. phonenumber;
If (FRM. phonenumber. Length = 8 | frm. phonenumber. Length = 12)
{
Break;
}
Else
{
MessageBox. Show ("");
}
}
Else if (FRM. dialogresult = dialogresult. Cancel)
{
Label1.text = "form was canceled ";
Break;
}
}
FRM. Close ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.