【发布时间】:2015-01-10 09:21:29
【问题描述】:
我有几个表单,我希望能够为某个表单添加一个按钮,无论代码在哪里。
通常我会执行this.Controls.Add(button) 之类的操作,但我不希望将其添加到该表单中。我尝试过类似Form1 frm = new Form1() 和frm.Controls.Add(button) 的操作,但这也不起作用。需要怎么写?
此代码不起作用,表单仍为空白
Button b = new Button();
b.Size = new Size(50,50);
b.Location = new Point(50,50);
new Form1().Controls.Add(b);
没有错误,但没有添加任何内容。
我找到了一种解决方法。
Control ctrl = this;
ctrl.Controls.Add(b);
这行得通,但我宁愿有一种方法来准确指定将其添加到哪个表单
【问题讨论】:
-
Didn't work不是问题描述。请更具体,并为您的问题添加更多详细信息。 -
没有添加任何内容,表单只是空白
-
贴一小段代码说明问题。