C#,winform,showdialog, the subform passes the value to the parent form-practical tips

Source: Internet
Author: User
When the ShowDialog method is invoked, the calling code is paused, waiting until the form relationship of the ShowDialog method is invoked before continuing. And the form can return a DialogResult value that describes why the form was closed, such as Ok,cancel,yes,no. In order for the form to return a DialogResult, you must set the DialogResult value for the form, or set the DialogResult property on a button on the form.

Example:
The following is a subform code that requires you to enter phone and then 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)
{

}
}
}

Does not contain any code to handle button click events, because the DialogResult property of each button is set, so when you click the OK or Cancel button, the form disappears. The following code shows the method of calling 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 Button1_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 ();
}
}
}

Looks very simple, creating a new phone object frm, In the call Frm.showdialog method is, the code stops, waits for the phone form to return, then examines the phone form the DialogResult property, because the form has not released, is not visible, therefore still can access the public property PhoneNumber, once obtains the data which needs, can the whine use the window The Close method of the body.
Everything is normal, but if the returned format is not correct how to do, it is necessary to put the ShowDialog method in the loop, you can call again, let the user re-enter, you can get the correct value.

The above code can be 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 Button1_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 ();
}
}
}

Related Article

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.