6

我在运行时动态添加两个控件,但是只显示首先创建的控件。

这是代码:

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

4 回答 4

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 用作面板或 GroupBox。请确保您还将 MyControl 添加到您的 Controls 集合中。

于 2013-02-20T21:21:48.850 回答
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); 
于 2013-02-20T21:43:31.517 回答
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
于 2013-02-20T22:04:57.613 回答
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 as 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;

            }
    }

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

于 2015-06-09T05:59:40.243 回答