本文介绍了c#从form2添加标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Form2代码: public void button2_Click(object sender,EventArgs e)
{
Form1 form1form = new Form1();
标签asd =新标签();
asd.Text =asdasasdasdasd;
form1form.Controls.Add(asd);
Form2 form2form = new Form2();
form2form.close();
$ / code>
我想从form2在form1上添加新的标签和按钮
它是如何制作的?
谢谢
解决方案
如果您想从 form2form
访问 form1form
,则必须公开引用 form1form
。在 form1form
中声明属性如下:
public static form1form Instance {get ;私人设置; }
然后在<$ c中设置实例
$ c $> $ form1form
:
private void form1form_Load(object sender,EventArgs e)
{
Instance = this;
在 form2form
中:
public void button2_Click(object sender,EventArgs e)
{
Label asd = new Label() ;
asd.Text =asdasasdasdasd;
form1form.Instance.Controls.Add(asd);
}
Form2 code:
public void button2_Click(object sender, EventArgs e)
{
Form1 form1form = new Form1();
Label asd = new Label();
asd.Text = "asdasasdasdasd";
form1form.Controls.Add(asd);
Form2 form2form = new Form2();
form2form.close();
}
I want to add new label and button on form1 from form2
how it made ?
thanks
解决方案
If you want to access form1form
from form2form
you must have public reference to form1form
. Declare property in form1form
like below:
public static form1form Instance { get; private set; }
Then set Instance
in Load
event of form1form
:
private void form1form_Load(object sender, EventArgs e)
{
Instance = this;
}
In form2form
:
public void button2_Click(object sender, EventArgs e)
{
Label asd = new Label();
asd.Text = "asdasasdasdasd";
form1form.Instance.Controls.Add(asd);
}
这篇关于c#从form2添加标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!