我在C#2种的窗口形式之间传递数据。 Form1中的主要形式,其文本框将接收传递给它的文本从form2_textbox &显示在其文本框(form1_textbox)。传数据与属性
首先,打开Form1中,用一个空的文本框和一个按钮,在点击form1_button,窗口2打开。在窗体2,我进入了form2_textbox &一个文本,然后单击该按钮(form2_button)。在点击这个按钮的情况下,它会发送文本到Form1的文本框& Form1中会实现对焦,其空form1_textbox从窗口2收到一个文本。
我使用性能来实现这一任务。 FORM2.CS
公共部分类窗体2:晶型 { //在形式2 公共事件的EventHandler SomeTextInSomeFormChanged声明事件;
public Form2()
{
InitializeComponent();
}
public string get_text_for_Form1
{
get { return form2_textBox1.Text; }
}
//On the button click event of form2, the text from form2 will be send to form1:
public void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.set_text_in_Form1 = get_text_for_Form1;
//if subscribers exists
if(SomeTextInSomeFormChanged != null)
{
SomeTextInSomeFormChanged(this, null);
}
}
}
Form1.cs的
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string set_text_in_Form1
{
set { form1_textBox1.Text = value; }
}
private void form1_button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
f2.SomeTextInSomeFormChanged +=new EventHandler(f2_SomeTextInSomeFormChanged);
}
//in form 1 subcribe to event
Form2 form2 = new Form2();
public void f2_SomeTextInSomeFormChanged(object sender, EventArgs e)
{
this.Focus();
}
}
Form1文本框的文本正在设置按钮点击!可能所有你需要的是按钮点击处理程序中的form1.Focus()。 – VinayC 2011-02-23 07:10:09
[Duplicate 1](http://stackoverflow.com/questions/13465158/)。 [Duplicate 2](http://stackoverflow.com/questions/5087934/)。 [Duplicate 3](http://stackoverflow.com/questions/5088213/)。 [Duplicate 4](http://stackoverflow.com/questions/9073267/)。 [Duplicate 5](http://stackoverflow.com/questions/20186722/)。 – 2016-05-21 16:51:49