在向表单动态添加控件时,只会显示一个控件

时间:2013-02-20 21:08:41

标签: c# winforms

我在运行时动态添加两个控件,但只显示第一个控件。

以下是代码:

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所以忽略带有大写的语法错误。 有什么想法吗?

它们被添加到组框中。我已经尝试将它们添加到面板或只是表单,并出现相同的问题。我不需要事件处理程序,所以请不要引用该要求。

4 个答案:

答案 0 :(得分:8)

我很快就尝试将代码粘贴到Windows窗体构造函数中。它运行正常,但由于它的大小,标签略微重叠按钮。您可能想要自动调整它:

Label tempLab = new Label();
tempLab.Text = "Test Label";
tempLab.AutoSize = true;
Controls.Add(tempLab);
tempLab.Location = new Point(5,5);

Button tempBut = new Button();
tempBut.Text = "Test Button";
Controls.Add(tempBut);
tempBut.Location = new Point(20,20);

哦,顺便说一下。您提到您使用MyControl作为Panel或GroupBox。请确保您还将MyControl添加到Controls集合中。

答案 1 :(得分:2)

看来该位置没有尺寸变成扁平线,可以说是不可见的..这tempBut.Location = new Point(20,20);尝试更改为

this.tempBut.Location = new System.Drawing.Point(20,20); 
this.tempBut.Size = new System.Drawing.Size(30, 15); 

答案 2 :(得分:1)

希望这会有所帮助。我在面板中添加了一个MyTextBox数组。

         Point prevlocation = new Point(0,0);
         foreach (object key in keys)  //List of Objects or which make new controls
         {


            MyTextBoxControlArray[i] = new MyTextBoxUserControl(key);   //User control but could be any control like textbox etc
            MyTextBoxControlArray[i].Width = this.panel1.Width - 50;
            MyTextBoxControlArray[i].AutoSize = true;
            MyTextBoxControlArray[i].InfoLoad += new MyTextBoxUserControl.InfoLoadEventHandler(Form1_InfoLoad);

            if (i == 0)
            {
                //first control
                prevlocation.Y += 3;
                prevlocation.X += 3;
                MyTextBoxControlArray[i].Location = prevlocation;
            }
            else
            {
            //adjsuting height and width
                MyTextBoxControlArray[i].Location = new System.Drawing.Point(
                             prevlocation.X,
                             prevlocation.Y + MyTextBoxControlArray[i].Height+3);
            }

            prevlocation = MyTextBoxControlArray[i].Location;

            i++;
        }

        this.panel1.Controls.AddRange(MyTextBoxControlArray); //in panel i can add a array of controls , but this could be done one by one

答案 3 :(得分:0)

  string sql3 = "SELECT COUNT(*) from systeminfo";//counting no of element
        n = dm.countelement(sql3);
        int i, c = 1;
        int m = 100;

        for (i = 0; i < n; i++, c++)
        {
            sql3 = " SELECT Company_name FROM systeminfo LIMIT " + (i + 1) + " OFFSET " + i + "";
            string cname = dm.getlang(sql3);

            PictureBox pb = new PictureBox();
            Label lb = new Label();
           pb.Location = new System.Drawing.Point(m, 30 + (30 * i));
           lb.Location = new System.Drawing.Point(m-30, 30 + ((30 * i)-30));
           pb.Name = "p" + c;
           lb.Name = "l" + c;
           lb.Size = new System.Drawing.Size(100, 20);
           pb.Size = new System.Drawing.Size(30, 30);
           lb.Text = cname;
           lb.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
           lb.BackColor = Color.Transparent;
           pb.ImageLocation = @"..\image\image.jpg";
           pb.MouseDown += new         System.Windows.Forms.MouseEventHandler(this.picmap1_MouseDown_1);
           pb.MouseMove += new System.Windows.Forms.MouseEventHandler(this.picmap1_MouseMove_1);
           pb.MouseUp += new System.Windows.Forms.MouseEventHandler(this.picmap1_MouseUp_1);

            picmap1.Controls.Add(pb);
            picmap1.Controls.Add(lb);
            c++;

        }

private void picmap1_MouseMove_1(object sender,MouseEventArgs e)         {             var c = sender作为PictureBox;

        if (!_dragging || null == c) return;
        c.Top = e.Y + c.Top - _yPos;
        c.Left = e.X + c.Left - _xPos;
        foreach (Control d in picmap1.Controls)
            if (d is Label)
            {

                d.Top = e.Y + d.Top - _yPos;
                d.Left = e.X + d.Left - _xPos;

            }
    }

    private void picmap1_MouseUp_1(object sender, MouseEventArgs e)
    {
        var c = sender as PictureBox;
        if (null == c) return;
        _dragging = false;
    }

    private void picmap1_MouseDown_1(object sender, MouseEventArgs e)
    {

        if (e.Button != MouseButtons.Left) return;
        _dragging = true;
        _xPos = e.X;
        _yPos = e.Y;
        foreach (Control d in picmap1.Controls)
            if (d is Label)
            {

                _xPos = e.X;
                _yPos = e.Y;

            }
    }

这是移动鼠标拖动的动态添加控件的示例