【发布时间】:2023-02-02 19:17:25
【问题描述】:
我希望使用 button_click 事件在 Windows 窗体应用程序中加载多个组框。
每次单击按钮时,表单中都应出现一个组框。请参考以下屏幕截图了解我的预期输出。
我无法使组框动态定位,因为第二个组框应该与第一个组框有一定距离。我想到了手动计算坐标并使用点数组作为位置,但我觉得应该有更好的方法来解决这个问题。
我已经定义了“int count=0”变量来计算按钮被点击的次数。基于此,我命名了新的组框。但我认为 count++ 行中使用的逻辑存在一些问题。它不在 1 之后。因此我只得到一个组框“groupBox1”。当我再次点击按钮时没有任何反应。
我感谢您的帮助。
谢谢
Screenshot of my expected output is attached here
int count=0;
private GroupBox GetGroupBox(int a)
{
GroupBox groupBox = new GroupBox();
groupBox.Text = "groupBox"+(a.ToString());
groupBox.Width= 200;
groupBox.Height= 200;
groupBox.Location = new Point(50,400);
return groupBox;
}
private void button1_Click(object sender, EventArgs e)
{
count++;
this.Controls.Add(GetGroupBox(count));
}
【问题讨论】:
-
位置
groupBox.Location = new Point(50,400);不能是常量,groupBox.Location = new Point(50 + count * (groupBox.Width + 10), 400);