C#无法动态添加面板

时间:2015-10-29 23:07:39

标签: c# panel

我尝试向Form添加一个面板,但它从未出现过。但是,当我更改其类型时,例如在TextBox上它是apears。谁知道为什么?

HidePanel = new Panel();
HidePanel.ForeColor = Color.Red;
HidePanel.BackColor = Color.Green;//Form.BackColor;
HidePanel.Location = new System.Drawing.Point(531, 181); 
HidePanel.Name = "HidePanel";
HidePanel.Size = new System.Drawing.Size(200, 100); 
HidePanel.Visible = true;
HidePanel.TabIndex = 12;
HidePanel.BringToFront();
Form.Controls.Add(HidePanel);

1 个答案:

答案 0 :(得分:1)

您使用的是表格并且不正确,您应该使用而不是表单,尝试使用此代码。

HidePanel = new Panel();
HidePanel.ForeColor = Color.Red;
HidePanel.BackColor = Color.Green;//Form.BackColor;
HidePanel.Location = new System.Drawing.Point(531, 181); 
HidePanel.Name = "HidePanel";
HidePanel.Size = new System.Drawing.Size(200, 100); 
HidePanel.Visible = true;
HidePanel.TabIndex = 12;
HidePanel.BringToFront();
this.Controls.Add(HidePanel);

更新:

    Form2 frm = new Form2();
    Panel HidePanel = new Panel();
    HidePanel.ForeColor = Color.Red;
    HidePanel.BackColor = Color.Green;//Form.BackColor;
    HidePanel.Location = new System.Drawing.Point(531, 181);
    HidePanel.Name = "HidePanel";
    HidePanel.Size = new System.Drawing.Size(200, 100);
    HidePanel.Visible = true;
    HidePanel.TabIndex = 12;
    HidePanel.BringToFront();
    frm.Controls.Add(HidePanel);
    frm.Show();

我将此代码放在button1的click事件中,该事件在form1中声明。