Home > Backend Development > C++ > How Do I Pass Values Between Forms in C#?

How Do I Pass Values Between Forms in C#?

DDD
Release: 2025-01-31 23:46:10
Original
515 people have browsed it

How Do I Pass Values Between Forms in C#?

Passing the value between the windows in C#

In C#, the data transfer between the windows can be implemented through the following steps:

Create attributes in Form2:

Create a public attribute in Form2 so that Form1 can access this value:

Use the showdialog method in Form1:

<code class="language-csharp">public string TheValue
{
    get { return someTextBoxOnForm2.Text; }
    set { someTextBoxOnForm2.Text = value; } //添加set方法,实现双向数据绑定
}</code>
Copy after login

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>
Copy after login
Open Form2 by modal dialog box.

Check whether Form2 is closed with
    , which means clicking "OK".
  • form2.ShowDialog() If the dialog box is turned off in "OK", use the value entered in
  • and assign it to
  • . Here we add the method to make the data pass in both directions. if DialogResult.OK
  • This Revied Answer Improves The Code by Adding A
  • Accessor to the 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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template