object0812 2023-08-16 16:54 采纳率: 12.5%
浏览 85
已结题

Winform panel 动态添加控件

关于使用 Winform ,向 panel 控件中动态添加一个 TextBox 控件,设置了 panel 的区域大小后,点击4次添加后,当 TextBox 控件填满了 panel 区域后,再次点击添加,第4次的控件与第5次添加的控件中间会有空行,如果连续点击添加没有空行出现,但是停顿后,再次点击添加,中间就又会出现空行

img

img


```c#
private int nextLocation1 = 0;
private int nextLocation2 = 0;
private int nextLocation3 = 0;
private int btnCilck = 0;

private void button1_Click(object sender, EventArgs e)
{
     //for (int i = 0; i < 60; i++)
      //{
         AddControl(btnCilck.ToString());
      //}

      btnCilck++;
}

public void AddControl(string a)
{
     TextBox textBox = new TextBox();
      textBox.Text = a;
      textBox.Name = $"textBox{nextLocation1}";
      textBox.Location = new Point(10, nextLocation1);
      textBox.Size = new Size(150, 20);
      nextLocation1 += 30;
      panel1.Controls.Add(textBox);
}

```

  • 写回答

2条回答 默认 最新

  • threenewbee 2023-08-16 17:22
    关注

    nextLocation1 += 30;
    这里下断点调试下

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月17日
  • 创建了问题 8月16日