我已经以编程方式在C#表单中添加了几个按钮,但只有我通过Designer添加的按钮实际显示出来。其余的保持隐形。在此之前,我正在进行大量的操作;这可能是原因吗?
public FormView
{
List<Button> listOfButtons = new List<Button>();
int frame = 0;
for (int i = 0; i < 36; i++)
{
listOfButtons.Add(new Button());
}
listOfButtons[4 * frame].Width = 92;
listOfButtons[4 * frame].Height = 92;
listOfButtons[4 * frame].BackColor = Color.Red;
listOfButtons[4 * frame].BackgroundImage = System.Drawing.Image.FromFile("image.png");
listOfButtons[4 * frame].BackgroundImageLayout = ImageLayout.Stretch;
listOfButtons[4 * frame].ImageAlign = ContentAlignment.MiddleCenter;
listOfButtons[4 * frame].TextAlign = ContentAlignment.TopCenter;
listOfButtons[4 * frame].Location = new Point(1, 0);
listOfButtons[4 * frame].Click += new EventHandler(this.button1_Click);
listOfButtons[4 * frame + 1].Width = 92;
listOfButtons[4 * frame + 1].Height = 92;
listOfButtons[4 * frame + 1].BackColor = Color.Red;
listOfButtons[4 * frame + 1].BackgroundImage = System.Drawing.Image.FromFile("image.png");
listOfButtons[4 * frame + 1].BackgroundImageLayout = ImageLayout.Stretch;
listOfButtons[4 * frame + 1].ImageAlign = ContentAlignment.MiddleCenter;
listOfButtons[4 * frame + 1].TextAlign = ContentAlignment.TopCenter;
listOfButtons[4 * frame + 1].Location = new Point(1, 99);
listOfButtons[4 * frame + 1].Click += new EventHandler(this.button2_Click);
listOfButtons[4 * frame + 2].Width = 92;
listOfButtons[4 * frame + 2].Height = 92;
listOfButtons[4 * frame + 2].BackColor = Color.Red;
listOfButtons[4 * frame + 2].BackgroundImage = System.Drawing.Image.FromFile("image.png");
listOfButtons[4 * frame + 2].BackgroundImageLayout = ImageLayout.Stretch;
listOfButtons[4 * frame + 2].ImageAlign = ContentAlignment.MiddleCenter;
listOfButtons[4 * frame + 2].TextAlign = ContentAlignment.TopCenter;
listOfButtons[4 * frame + 2].Location = new Point(99, 0);
listOfButtons[4 * frame + 2].Click += new EventHandler(this.button3_Click);
listOfButtons[4 * frame + 3].Width = 92;
listOfButtons[4 * frame + 3].Height = 92;
listOfButtons[4 * frame + 3].BackColor = Color.Red;
listOfButtons[4 * frame + 3].BackgroundImage = System.Drawing.Image.FromFile("image.png");
listOfButtons[4 * frame + 3].BackgroundImageLayout = ImageLayout.Stretch;
listOfButtons[4 * frame + 3].ImageAlign = ContentAlignment.MiddleCenter;
listOfButtons[4 * frame + 3].TextAlign = ContentAlignment.TopCenter;
listOfButtons[4 * frame + 3].Location = new Point(99, 99);
listOfButtons[4 * frame + 3].Click += new EventHandler(this.button4_Click);
listOfButtons[4 * frame].Visible = true;
listOfButtons[4 * frame + 1].Visible = true;
listOfButtons[4 * frame + 2].Visible = true;
listOfButtons[4 * frame + 3].Visible = true;
InitializeComponent();
}
有什么想法吗?
答案 0 :(得分:3)
我没有看到您将按钮添加到任何父控件。 你应该这样做......
Form1.Controls.Add(listOfButtons[4 * frame]); // and so on...
答案 1 :(得分:1)
首先,在初始化控件之后,在InitalizeComponent之后执行所有这些操作。
然后将控件添加到表单上的某个容器中。 e.g。
yourForm.Controls.Add(controlsToAdd);
如果仍然无法查看可能是因为多种原因,例如您的按钮超出了表单范围,或者它们与其他控件重叠。尝试分配您在表单范围内动态添加修复位置的任何一个按钮。
答案 2 :(得分:0)
您应该将非设计师控件添加到Controls集合中,无论是表单还是其他控件。