Winform中,怎么实现Form2中点击打开按钮,打开Form1,输入文本,再点击Form1中的“确定”按钮,把输入的值显示到Form2的文本框中?
首先自定义一个事件参数TextBoxArgs:
Public class TextBoxArgs:EventArgs
{
public string TxtValue{get;set}
Public TextBoxArgs(string val)
{
TextValue=val;
}
}
在Form1后台声明一个事件:public event Action<object,TextBoxArgs> SetTextBoxValue;
Form2中:
在Form2的”打开”按钮的Click事件中:
Form1 f1=new Form1();
f1.SetTextBoxValue+=F1_SetTextBoxValue;
f1.Show();
Private void F1_SetTextBoxValue(object sender,TextBoxArgs e)
{
txtValue.Text=e.TxtValue;
}
Form1中:
Form1的确定按钮的Click事件处理程序中:
string txtValue=txtInfo.Text.Trim();
TextBoxArgs arg=new TextBoxArgs(txtValue);
SetTextBoxValue?.Invoke(this,arg);
完成!
本文介绍如何在Winform应用程序中实现Form2通过打开Form1进行数据输入,并将输入的数据回传至Form2显示的具体步骤。通过自定义事件参数和事件处理程序,实现了两窗体间的数据交互。
8420

被折叠的 条评论
为什么被折叠?



