为什么只有第一个RadioButton被添加到GroupBox? [英] Why is only the first RadioButton being added to the GroupBox?

查看:187
本文介绍了为什么只有第一个RadioButton被添加到GroupBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试动态创建Windows控件并将其添加到Panel中。对于按钮和复选框这已经很好了我遇到了一个GroupBox的问题,但是,RadioButtons里面。

I am trying to dynamically create Windows controls and add them to a Panel. For Button and Checkbox this has worked fine; I've run into a problem with a GroupBox, though, with RadioButtons inside it.

第一个RadioButton元素被创建并添加到预期位置的GroupBox,但是虽然表面上的创建(通过代码,似乎是这种情况),它们是不可见的。

The first RadioButton element is created and added to the GroupBox in the expected location, but susbequent ones, although ostensibly created (stepping through the code makes that appear to be the case), they are not visible.

我会认为,如果后续的RadioButtons被打开在以前的顶部,最后一个将是被看到的。这是它的样子:

I would think that if subsequent RadioButtons were being plopped atop the previous ones, the last one would be the one seen. This is what it looks like:

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

Each ~-delimited val should be the text value of a radioButton, yet only one displays. Do I need to explicitly provide the Location vals for the subsequent RadioButtons, or why is this failing?

这是代码:

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;
}



更新



皮埃尔下面的答案中的想法似乎是明智的,但医生还没有下令:

UPDATE

The idea in the answer below by Pierre seems sensible, but it's still not quite what the doctor ordered:

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

This works pretty well (modification of Pierre's code):

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;

给我:

推荐答案

p>如果设置断点,您将看到您的分组框包含所有的单选按钮。他们的位置确实是一样的,所以它们显示在另一个之上。问题是没有将它们全部添加到组框中,而是全部显示。

If you set a breakpoint, you'll see that your groupbox contains all the radiobuttons. Their location is indeed all the same, so they're displayed one above the other. The problem is not adding them all to the groupbox, but displaying them all.

为了实现这一点,只需在每个添加操作上增加其位置即可显示它们: p>

To achieve that, simply increment their location on each add operation to display them all :

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 并将其初始化到您的分组框的位置。然后我按照它设置单选按钮位置,然后在每次添加单元按钮的高度时,只需将 radButtonPosition 增加。

I'm defining a variable called radButtonPosition and initializing it to your groupbox's position. I'm then setting the radiobutton location according to it and then simply incrementing that radButtonPosition by the height of a radiobutton each time one is added.

这里还有一个重构的版本:

Here's also a little refactored version :

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,但这是一开始。

There's of course a LOT of method extraction to do to respect SRP, but that's a start.

这篇关于为什么只有第一个RadioButton被添加到GroupBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆