C# 将值设置为另一个窗体
我必须填写C# 将值设置为另一个窗体,c#,C#,我必须填写Form1和Form2 Form1来源 namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void label1_Click(object sender, EventArgs e)
Form1
和Form2
Form1
来源
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.SetBtn = "teste test";
frm.Show();
}
public string setLb
{
set
{
label1.Text = value;
}
}
}
}
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.setLb = "test test";
}
public string SetBtn
{
set
{
button1.Text = value;
}
}
}
}
Form2
来源
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.SetBtn = "teste test";
frm.Show();
}
public string setLb
{
set
{
label1.Text = value;
}
}
}
}
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.setLb = "test test";
}
public string SetBtn
{
set
{
button1.Text = value;
}
}
}
}
我尝试在Form1
中设置标签文本,在Form2
我使用相同的方法设置值,但仅当将值从
Form1
设置为Form2
时才起作用。按钮1.Form2
上的文本更改为teste test
,但Form1
上没有发生任何事情使用构造函数方法,下面是示例
表格2
public Form2(string strTextBox)
{
InitializeComponent();
label1.Text=strTextBox;
}
在form1中单击事件
private void label1_Click(object sender, System.EventArgs e)
{
Form2 frm=new Form2(textBox1.Text);
frm.Show();
}
有关更多信息,请参考 使用构造函数方法,下面是示例 表格2
public Form2(string strTextBox)
{
InitializeComponent();
label1.Text=strTextBox;
}
在form1中单击事件
private void label1_Click(object sender, System.EventArgs e)
{
Form2 frm=new Form2(textBox1.Text);
frm.Show();
}
有关更多信息,请参考 您需要将对
Form1
实例的引用传递给Form2
实例
您可以这样做:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this); // <---- Pass a reference to this form to Form2
frm.SetBtn = "teste test";
frm.Show();
}
public string setLb
{
set
{
label1.Text = value;
}
}
}
}
您需要将对
Form1
实例的引用传递给Form2
实例
您可以这样做:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this); // <---- Pass a reference to this form to Form2
frm.SetBtn = "teste test";
frm.Show();
}
public string setLb
{
set
{
label1.Text = value;
}
}
}
}
在Form2中,必须为Form1添加一个参数 i、 e
在Form2中,必须为Form1添加一个参数 i、 e
如果要将值设置为其他表单文本框如果要将值设置为其他表单文本框