【问题标题】:When dynamically adding controls to a form only one will show向表单动态添加控件时,只会显示一个
【发布时间】:2015-06-09 13:59:40
【问题描述】:

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

代码如下:

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

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

【问题讨论】:

  • 是否每个控件都放在另一个控件之上?
  • 不,我什至将坐标更改为显着分开。
  • 除了这些控件之外,表单上还有其他内容吗?
  • 如果你要尝试this.MyControl.Controls.Add(this.But) ...,etc..,你一定会错过一些东西..看看这个 MSDN 示例如何做到这一点..我刚刚尝试过,它确实有效,所以不确定你是什么做错了Adding Controls at RunTime
  • 该位置似乎没有大小,可以说是一条不可见的平线。这个tempBut.Location = new Point(20,20);尝试更改为这个this.tempBut.Location = new System.Drawing.Point(20,20); this.tempBut.Size = new System.Drawing.Size(30, 15);

标签: c# winforms


【解决方案1】:

我很快尝试将您的代码粘贴到 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 集合中。

【讨论】:

    【解决方案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); 
    

    【讨论】:

      【解决方案3】:

      希望这会有所帮助。我正在将一组 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
      

      【讨论】:

        【解决方案4】:
          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(对象发送者,MouseEventArgs e) { var c = 作为 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;
        
                    }
            }
        

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-12-02
          • 2017-02-02
          • 2023-03-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多