如何在另一个表单文件中使用字段
本文关键字:字段 文件 表单 另一个 | 更新日期: 2023-09-27 18:23:48
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
int port; // I declared a variable and I wanna use this in another form like
}
// ------------------------------------------------------- //
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SagTikMenuOlustur();
}
void menu1_Click(object sender, EventArgs e)
{
Form2 frq = new Form2();
frq.Show();
MessageBox.Show("{0} server is online ",port); //How to I declare ????
}
}
将字段设置为公共
或
为该字段创建属性。
这是使用的方法
请参阅此链接:如何访问另一个表单的表单控件?
最好的办法是为它创建一个属性。
试试这个
public partial class Form3 : Form
{
int _port;
public int Port
{
get { return _port; }
set { _port = value; }
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void menu1_Click(object sender, EventArgs e)
{
Form2 frq = new Form2();
frq.Show();
Form3 frm3 = new Form3();
frm3.Port = 8080;
MessageBox.Show("{0} server is online ", frm3.Port);
}
}
您必须将端口更改为public。
公共分部类Form3:Form{公共窗体3(){
InitializeComponent();
}
public int port; <<== Change to public
或公共int端口{get;set;}