如何在运行时向面板添加控件?

时间:2014-06-20 10:01:54

标签: c# panel windows-forms-designer

我想在运行时向我的表单中的现有面板添加一些按钮,这样当按钮数量增加并且它们不适合表单时,我可以通过面板滚动它们。

我使用this.panel1.Controls.Add(this.button50[number]);但它没有显示面板上的按钮 添加后如何在面板上显示控件?

这是我的代码:

static int number = 49;
static int size = 163;
public void initialize()
{
    this.button50= new System.Windows.Forms.Button[50];

}
public void makeButton()
{
    if (number > 0)
    {
        this.button50[number] = new System.Windows.Forms.Button();
        this.button50[number].Font = new System.Drawing.Font("Nazanin"
        ,12F,System.Drawing.FontStyle.Bold, 
        System.Drawing.GraphicsUnit.Point, ((byte)(178)));

        size = 41 + size;
        this.button50[number].Location = new System.Drawing.Point(194, size);
        this.button50[number].Name = "buttonPlus"+number;
        this.button50[number].Size = new System.Drawing.Size(151, 34);
        this.button50[number].TabIndex = 24;
        this.button50[number].Text = "انتخاب دستی دروس";
        this.button50[number].UseVisualStyleBackColor = true;

        this.panel1.Controls.Add(this.button50[number]);

        number--;
    }

}
    //// a part of InitializeComponent() function
    //
    // panel1
    //
    this.panel1.Controls.Add(this.button50[number]);
    this.panel1.AutoScroll = true;
    this.panel1.Controls.Add(this.button10);
    this.panel1.Controls.Add(this.button3);
    this.panel1.Controls.Add(this.button8);
    this.panel1.Controls.Add(this.button9);
    this.panel1.Controls.Add(this.button5);
    this.panel1.Controls.Add(this.button7);
    this.panel1.Controls.Add(this.button6);
    this.panel1.Controls.Add(this.label2);
    this.panel1.Controls.Add(this.button4);
    this.panel1.Controls.Add(this.button2);
    this.panel1.Controls.Add(this.textBox1);
    this.panel1.Controls.Add(this.label1);
    this.panel1.Controls.Add(this.bindingNavigator1);
    this.panel1.Controls.Add(this.button1);
    this.panel1.Controls.Add(this.lable4);
    this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
    this.panel1.Location = new System.Drawing.Point(0, 0);
    this.panel1.Name = "panel1";
    this.panel1.Size = new System.Drawing.Size(549, 306);
    this.panel1.TabIndex = 0;

0 个答案:

没有答案