c # Transfer parameters

Original text reprinted: https://blog.csdn.net/g1036583997/Article/details/9630037

 

1. (1-> 2) Pass the parameters in the first form to the second form

Write the second window to the parameter constructor, assign the variable with the parameters, or declare this variable as static, directly assign

Form2
String strText = “”; // You want to assign the variable
public Form2(string text)
{
strText=text;
}
Form1
Form2 form2=new Form2(textBox1.text.trim());
form2.show();

2. (1 <-2) Pass the parameters of the second form to the first form

The first form code:

public partial class Form1 : Form
{
public string str;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
F.ShowDialog (); // must use the showdialog () function, otherwise the parameter transmission cannot be performed normally
this.str = f.str;
label1.Text = str;
}
}
Second form code:
public partial class Form2 : Form
{
public string str, str1, str2;

public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
str1=textBox1.Text;
str2 = textBox2.Text;
str = str1 + str2;

}
Need to set the DialogResult of Form2’s Button for OK

3. (1 <-> 2) C # form parameters mutual transmission (first pass, the second pass first one can be)

For example, there is a Button1 on Form1, and there is a TextBox1 and a Button1 on Form2.

Form1
class Form1:Form
{
public string Str;
Private void button1_click (Object Sender, Eventargs E)
{
Form2 f2=new Form2();
f2.f1=this;
f2.show();
}
}

Form2
class Form2:Form
{
public Form1 f1;
private void button1_Click(object sender, EventArgs e)
{
f1.Str=this.textBox1.Text;
}
}

—————-
Copyright Notice: This article is a original article of CSDN Bo Lord “G103658399”, follows the CC 4.0 BY-SA copyright agreement, please attach the original link and this statement.
Original link: https://blog.csdn.net/g1036583997/Article/details/9630037