为什么只有第一个单选按钮被添加到GroupBox?

时间:2014-05-29 23:03:01

标签: c# dynamic radio-button panel groupbox

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

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

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

enter image description here

每个〜-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<string> grpbxVals = new List<string>(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;
}

更新

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

enter image description here

更新2

这很好用(修改Pierre的代码):

IList<string> grpbxVals = new List<string>(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;

给我:

enter image description here

2 个答案:

答案 0 :(得分:1)

如果设置断点,您将看到您的组合框包含所有单选按钮。他们的位置确实都是一样的,所以他们一个接一个地显示出来。问题不是将它们全部添加到组框中,而是全部显示它们。

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

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<string> grpbxVals = new List<string>(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的位置。然后我根据它设置单选按钮位置,然后每次添加一个单独的radButtonPosition增加一个单选按钮的高度。

这里还有一点重构版本:

private GroupBox CreateGroupboxWithRadiobuttons(string currentSettings, int curLeftVal)
{
    IList<string> grpbxVals = new List<string>(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,但这是一个开始。

答案 1 :(得分:1)

所有项目都是位置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,以自动放置组件