C#子窗口 调用父窗口的变量

2024-12-28 06:31:46
推荐回答(5个)
回答(1):

  1. 需要将要访问的变量设置为公有的,即 public;

  2. 在子窗口中 this.ParentForm.(变量名) 即可访问

回答(2):

楼主你好:
你在你第二个窗体里面直接这样写就行了

private void button2_Click(object sender, EventArgs e)
{
Form2 FRM = new Form2();
MessageBox.Show(FRM.friendid .ToString());
}

希望对你有帮主,望采纳。

谢谢、

回答(3):

在子窗体中设置一个int变量,使用set get方法定义好。然后子啊父窗体中打开子窗体时把friendid 变量传递过去

回答(4):

可能通过构造函数传参,子窗体带构造函数new 子窗体(int firiendId),或是通过静态变量,public static int friendId,不过这样的话安全性要差一些

回答(5):

1.可以用构造函数,new子窗口的时候传过去。
2.可以在父窗口定义为属性,子窗口里实例化父窗口可直接调用