为什么只有第一个RadioButton被添加到GroupBox?

我试图动态创建Windows控件并将其添加到Panel。 对于Button和Checkbox,这个工作正常; 我遇到了GroupBox的问题,但里面有RadioButtons。

创建第一个RadioButton元素并将其添加到预期位置的GroupBox中,但是虽然表面上是创建的(通过代码逐步执行,但是它们是不可见的)。

我认为,如果随后的RadioButton被放在前面的那个上面,那么最后一个就是看到的那个。 这就是它的样子:

在此处输入图像描述

每个〜-delimited val应该是radioButton的文本值,但只显示一个。 我是否需要为后续的RadioButtons显式提供位置值,或者为什么会失败?

这是代码:

private GroupBox getGroupBox(string currentSettings, int curLeftVal) { // "apple~orange~peach~True (must look for "enclose group in a black box" as the last val (ignore for the quick-and-dirty mockup, though)) List grpbxVals = new List(currentSettings.Split('~')); GroupBox gb = new GroupBox(); gb.Height = 60; gb.Location = new Point(curLeftVal, PANEL_TOP_LOC); RadioButton radbtn = null; // "-1" because we're ignoring the final bool ("enclose in black box") for (int i = 0; i < grpbxVals.Count-1; i++) { radbtn = new RadioButton(); radbtn.Text = grpbxVals[i]; gb.Controls.Add(radbtn); } return gb; } 

UPDATE

皮埃尔在下面的答案中的想法似乎是明智的,但它仍然不是医生所要求的:

在此处输入图像描述

更新2

这非常有效(修改Pierre的代码):

 IList grpbxVals = new List(currentSettings.Split('~')); GroupBox gb = new GroupBox { Height = 60, Location = new Point(curLeftVal, 0) }; int radButtonPosition = 0; for (int i = 0; i < grpbxVals.Count() - 1; i++) { gb.Controls.Add(new RadioButton { Text = grpbxVals[i], Location = new Point(curLeftVal, radButtonPosition) }); radButtonPosition += new RadioButton().Height - 4; // the "-4" is a kludge } return gb; 

给我:

在此处输入图像描述

如果设置断点,您将看到您的combobox包含所有单选按钮。 它们的位置确实完全相同,所以它们一个在另一个上面显示。 问题不是将它们全部添加到组框中,而是全部显示它们。

要实现这一点,只需在每个添加操作上增加它们的位置即可全部显示:

 private GroupBox getGroupBox(string currentSettings, int curLeftVal) { // "apple~orange~peach~True (must look for "enclose group in a black box" as the last val (ignore for the quick-and-dirty mockup, though)) List grpbxVals = new List(currentSettings.Split('~')); GroupBox gb = new GroupBox(); gb.Height = 60; gb.Location = new Point(curLeftVal, PANEL_TOP_LOC); RadioButton radbtn = null; // "-1" because we're ignoring the final bool ("enclose in black box") int radButtonPosition = PANEL_TOP_LOC; for (int i = 0; i < grpbxVals.Count - 1; i++) { radbtn = new RadioButton(); radbtn.Text = grpbxVals[i]; radbtn.Location = new Point(curLeftVal, radButtonPosition ); radButtonPosition += radbtn.Height; gb.Controls.Add(radbtn); } return gb; } 

我正在定义一个名为radButtonPosition的变量,并将其初始化为您的groupbox的位置。 然后,我根据它设置radiobutton位置,然后每次添加一个radButtonPosition ,只需将radButtonPosition增加一个radiobutton的高度。

这里还有一个重构版本:

 private GroupBox CreateGroupboxWithRadiobuttons(string currentSettings, int curLeftVal) { IList grpbxVals = new List(currentSettings.Split('~')); GroupBox gb = new GroupBox { Height = 60, Location = new Point(curLeftVal, PANEL_TOP_LOC) }; int radButtonPosition = PANEL_TOP_LOC; for (int i = 0; i < grpbxVals.Count() - 1; i++) { gb.Controls.Add(new RadioButton {Text = grpbxVals[i], Location = new Point(curLeftVal, radButtonPosition)}); radButtonPosition += new RadioButton().Height; } return gb; } 

当然有很多方法提取来尊重SRP,但这是一个开始。

所有项目都是位置0,0试试这个

 int y=20; for (int i = 0; i < grpbxVals.Count-1; i++) { radbtn = new RadioButton(); radbtn.Text = grpbxVals[i]; radbtn.Location=new System.Drawing.Point(6, y); y+=radbtn.Height; gb.Controls.Add(radbtn); radbtn = null; } 

也可以在GroupBox中插入FlowLayoutPanel,然后将RadioButton添加到FlowLayoutPanel,以自动放置组件