【发布时间】:2015-06-09 13:59:40
【问题描述】:
我在运行时动态添加两个控件,但是只显示最先创建的控件。
代码如下:
Label tempLab = new Label();
tempLab.text = "Test Label";
MyControl.Controls.Add(tempLab);
tempLab.Location = new Point(5,5);
Button tempBut = newButton()
tempBut.text = "Test Button";
MyControl.Controls.Add(tempBut);
tempBut.Location = new Point(20,20);
不是copypasta,所以忽略大写的语法错误。 有什么想法吗?
它们被添加到一个组框。我已经尝试将它们添加到面板或只是表单中,并且发生了同样的问题。我不需要事件处理程序,所以请不要引用该要求。
【问题讨论】:
-
是否每个控件都放在另一个控件之上?
-
不,我什至将坐标更改为显着分开。
-
除了这些控件之外,表单上还有其他内容吗?
-
如果你要尝试
this.MyControl.Controls.Add(this.But) ...,etc..,你一定会错过一些东西..看看这个 MSDN 示例如何做到这一点..我刚刚尝试过,它确实有效,所以不确定你是什么做错了Adding Controls at RunTime -
该位置似乎没有大小,可以说是一条不可见的平线。这个
tempBut.Location = new Point(20,20);尝试更改为这个this.tempBut.Location = new System.Drawing.Point(20,20); this.tempBut.Size = new System.Drawing.Size(30, 15);