Home >Backend Development >C++ >How Do I Pass Values Between Forms in C#?
In C#, the data transfer between the windows can be implemented through the following steps:
Create a public attribute in Form2 so that Form1 can access this value:
<code class="language-csharp">public string TheValue { get { return someTextBoxOnForm2.Text; } set { someTextBoxOnForm2.Text = value; } //添加set方法,实现双向数据绑定 }</code>
In the Form1 button click the event, use the showdialog method to display Form2 and wait for its response:
Code Description:
<code class="language-csharp">using (Form2 form2 = new Form2()) { if (form2.ShowDialog() == DialogResult.OK) { someControlOnForm1.Text = form2.TheValue; } }</code>Open Form2 by modal dialog box.
Check whether Form2 is closed with
form2.ShowDialog()
If the dialog box is turned off in "OK", use the value entered in if
DialogResult.OK
form2.TheValue
Property, Enabling Two-Way Data Binding. be reflected back in someTextBoxOnForm2
, and view -Versa, Offering More Flexibility in Data Transfer. The Image Caption is Also Improved for Clarity. someControlOnForm1
The above is the detailed content of How Do I Pass Values Between Forms in C#?. For more information, please follow other related articles on the PHP Chinese website!