Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使单击按钮更改其他形式的标签文本?_C# - Fatal编程技术网

C# 如何使单击按钮更改其他形式的标签文本?

C# 如何使单击按钮更改其他形式的标签文本?,c#,C#,当我点击Form1(主窗体)中的按钮时,它会打开Form2,同时也会更改标签的文本(Label4) 按钮的当前代码: Admin objFrmAdmin = new Admin(); this.Hide(); objFrmAdmin.Show(); 我不想更改表单的显示代码,我只想添加它,这样当我单击按钮打开该表单时,它也会更改表单2中标签上的文本。changeAdmin表单构造函数,类似这样: //add parameter text public Admin(string text) {

当我点击Form1(主窗体)中的按钮时,它会打开Form2,同时也会更改标签的文本(Label4)

按钮的当前代码:

Admin objFrmAdmin = new Admin();
this.Hide();
objFrmAdmin.Show();

我不想更改表单的显示代码,我只想添加它,这样当我单击按钮打开该表单时,它也会更改表单2中标签上的文本。

change
Admin
表单构造函数,类似这样:

//add parameter text
public Admin(string text)
{
    InitializeComponent();
    //change label 4 text
    this.Label4.Text = text;
}
Admin objFrmAdmin = new Admin("text to show on label");
this.Hide();
objFrmAdmin.Show();
public void ChangeText(string text)
{
    this.Label4.Text = text;
    //put other code here if needed, ie. hide buttons or something like that
}
更改初始化
Admin
表单的方式,如下所示:

//add parameter text
public Admin(string text)
{
    InitializeComponent();
    //change label 4 text
    this.Label4.Text = text;
}
Admin objFrmAdmin = new Admin("text to show on label");
this.Hide();
objFrmAdmin.Show();
public void ChangeText(string text)
{
    this.Label4.Text = text;
    //put other code here if needed, ie. hide buttons or something like that
}
编辑:另一种实现方法是在管理表单上公开方法并调用它。所以,保持构造函数不带参数,就像它是一样,并在
Admin
表单上公开方法,如下所示:

//add parameter text
public Admin(string text)
{
    InitializeComponent();
    //change label 4 text
    this.Label4.Text = text;
}
Admin objFrmAdmin = new Admin("text to show on label");
this.Hide();
objFrmAdmin.Show();
public void ChangeText(string text)
{
    this.Label4.Text = text;
    //put other code here if needed, ie. hide buttons or something like that
}
现在,只要在初始化
Admin
表单后调用该方法即可

Admin objFrmAdmin = new Admin();
this.Hide();
objFrmAdmin.ChangeText("text to show");
objFrmAdmin.Show();

更改
Admin
表单构造函数,如下所示:

//add parameter text
public Admin(string text)
{
    InitializeComponent();
    //change label 4 text
    this.Label4.Text = text;
}
Admin objFrmAdmin = new Admin("text to show on label");
this.Hide();
objFrmAdmin.Show();
public void ChangeText(string text)
{
    this.Label4.Text = text;
    //put other code here if needed, ie. hide buttons or something like that
}
更改初始化
Admin
表单的方式,如下所示:

//add parameter text
public Admin(string text)
{
    InitializeComponent();
    //change label 4 text
    this.Label4.Text = text;
}
Admin objFrmAdmin = new Admin("text to show on label");
this.Hide();
objFrmAdmin.Show();
public void ChangeText(string text)
{
    this.Label4.Text = text;
    //put other code here if needed, ie. hide buttons or something like that
}
编辑:另一种实现方法是在管理表单上公开方法并调用它。所以,保持构造函数不带参数,就像它是一样,并在
Admin
表单上公开方法,如下所示:

//add parameter text
public Admin(string text)
{
    InitializeComponent();
    //change label 4 text
    this.Label4.Text = text;
}
Admin objFrmAdmin = new Admin("text to show on label");
this.Hide();
objFrmAdmin.Show();
public void ChangeText(string text)
{
    this.Label4.Text = text;
    //put other code here if needed, ie. hide buttons or something like that
}
现在,只要在初始化
Admin
表单后调用该方法即可

Admin objFrmAdmin = new Admin();
this.Hide();
objFrmAdmin.ChangeText("text to show");
objFrmAdmin.Show();

可以使用对象继承人权限直接更改标签

Admin objFrmAdmin = new Admin();
this.Hide();
objFrmAdmin.lblLabelToChange.Text = "Your Test Here";  //<= this line.
objFrmAdmin.Show();
电话:

Admin objFrmAdmin = new Admin("The new text");

可以使用对象继承人权限直接更改标签

Admin objFrmAdmin = new Admin();
this.Hide();
objFrmAdmin.lblLabelToChange.Text = "Your Test Here";  //<= this line.
objFrmAdmin.Show();
电话:

Admin objFrmAdmin = new Admin("The new text");

您是否尝试使用form.button1.label=valueYes访问表单控件,但由于某种原因无法从Form1代码中找到Form2上的任何控件。确定-需要将控件公开。您是否尝试使用form.button1.label=valueYes访问表单控件,但由于某些原因,它无法从Form1代码中找到Form2上的任何控件。确定-需要将控件公开。尽管您的第一个建议可以解决OP的问题,但通常不建议将控件公开,因为一个表单由另一个表单的代码控制。这会导致混乱、更高的可维护性等。尽管您的第一个建议可以解决OP的问题,但一般不建议使用控件
公共
,因为一个表单由另一个表单的代码控制。这会导致混乱、更高的可维护性等。