窗体传值的方式

方式一: 使用公共静态变量传值

主窗体frmMain中代码

1
2
3
4
5
6
7
public partial class frmMain : Form
{
 //声明工位ID 为公共静态变量
 public static string terminalID = "";
 //给静态变量赋值
 terminalID = "q13bh01-bh12";
 }

子窗体frmGroup中代码

1
2
3
4
5
6
private void frmGroup_Load(object sender, EventArgs e)
{
  this.txtTerminalID.Text= frmMain.terminalID.Trim();
  //可以再赋值给静态成员,方便其他窗体调用
  frmMain.terminalID = "q13bh01-bh11";
}

特点 : 双向传值,实现简单
缺点: 静态变量在类加载的时候分配内存,存储于方法区,一般不会被销毁,在系统不够内存情况下会自动回收静态内存,这样就会引起访问全局静态错误。

方式二: 使用公共变量传值

主窗体frmMain中代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public partial class frmMain : Form
{
 //声明工位ID 为公共变量
 public string terminalID = "";
 //给变量赋值
 terminalID = "q13bh01-bh12";
 //单击‘行为'按钮的时候会给窗体传值
 private void btnGroup_Click(object sender, EventArgs e)
    {
      frmGroup frmGro = new frmGroup();
      //变量传值 ,注意顺序写在ShowDialog()方法之前
      frmGro .stationID = this.terminalID;
      frmGro .ShowDialog();
    }
 }

子窗体frmGroup中代码

1
2
3
4
5
public partial class frmGroup : Form
{
 //定义公共属性
 public string stationID = "";
}

特点 : 单向传值,只能主窗体给子窗体传值,实现简单

方式三: 使用委托传值

先来看子窗体frmGroup中代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
namespace Siemens.Simatic.GUIClient.MESClient
{
  //1、声明一个委托
  public delegate void setTextValue(string textValue,bool flag);
  public partial class frmGroup : Form
  {
    //2、声明一个委托类型的事件
     public event setTextValue setFormTextValue;
     public string groupName = "";
     public bool flagBtnGroup = false;
     public frmGroup()
     {
      InitializeComponent();
     }
     //轮询‘行为'按钮(相当于按钮单击事件)
     private void tmrBtn_Tick(object sender, EventArgs e)
     {
      if (sender is ButtonX) {
        ButtonX butX = (ButtonX)sender;//判断触发事件的是不是Button
        groupName = butX.Text.Trim();
        flagBtnGroup = true;
        //3、准备要回传的数据。
        setFormTextValue(this.groupName.Replace(" ", ""), this.flagBtnGroup );
        this.Close();
        return;
      }
    }

主窗体frmMain中代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private void btnGroup_Click(object sender, EventArgs e)
   {
     frmGroup frmGro = new frmGroup();
      //4、初始化事件
     frmGro .setFormTextValue += new setTextValue(frmGro _setFormTextValue);
     //变量传值 ,注意顺序写在ShowDialog()方法之前
     frmGro .stationID = this.terminalID;
     frmGro .ShowDialog();
   }
   //5、事件具体实现
    public void frmGro _setFormTextValue(string textValue,bool flag)
   {
     this.newGroupName = textValue;
     this.flagBtnGroup = flag;
     if (!string.IsNullOrEmpty(newGroupName))
     {
        ……
     }
    }

特点 :适合子窗体数据实时回传父窗体。

方式四:使用构造函数传递一个数值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class FormB
{
 int orgId;
 public FormB(int orgId)
 {
   this.orgId=orgId;
 }
}
 
class FormA
{
public void ShowB()
{
  FormB fb=new FormB(5); // 5是要传递过去的值
  fb.Show();
}
}

方式五:把A窗体整个传递给B窗体

有些时候需要在B窗体里对A窗体的某个控件值做修改,并且需要立即改变A窗体的控件显示,本方法适合这种情况。

修改FormA,把要在B里处理的控件的可访问性(Modifiers)设为public,然后按下面的方式处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class FormA
{
public void ShowB()
{
  FormB fb=new FormB(this); // 在构造函数里把A窗体本身传过去
  fb.Show();
}
}
 
class FormB
{
FormA fm; //B窗体增加一个FormA类型的成员
public FormB(FormA fm) //添加一个带参数的构造方法,参数类型是FormA
{
  this.fm=fm;
}
protected void Method1()
{
  fm.txtName.Text="Haha!"; //在这儿可以直接操作A窗体里的控件了!
}
}
原文地址:https://www.cnblogs.com/yyldh/p/14481116.html