简体   繁体   English

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

[英]Why is only the first RadioButton being added to the GroupBox?

I am trying to dynamically create Windows controls and add them to a Panel. 我试图动态创建Windows控件并将其添加到面板中。 For Button and Checkbox this has worked fine; 对于“按钮和复选框”,它工作正常; I've run into a problem with a GroupBox, though, with RadioButtons inside it. 但是,我在GroupBox中遇到了RadioButtons的问题。

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. 将创建第一个RadioButton元素并将其添加到GroupBox的预期位置,但是一些可疑的元素虽然表面上已创建(逐步浏览代码使得情况确实如此),但它们是不可见的。

I would think that if subsequent RadioButtons were being plopped atop the previous ones, the last one would be the one seen. 我认为,如果将后续的RadioButton放置在先前的RadioButton上,那么最后一个将是看到的那个。 This is what it looks like: 看起来是这样的:

在此处输入图片说明

Each ~-delimited val should be the text value of a radioButton, yet only one displays. 每个以〜分隔的val应该是单选按钮的文本值,但只能显示一个。 Do I need to explicitly provide the Location vals for the subsequent RadioButtons, or why is this failing? 我是否需要为后续的RadioButton明确提供Location val,还是为什么会失败?

Here is the code: 这是代码:

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)在下面的答案中提出的想法似乎很明智,但仍然不完全符合医生的要求:

在此处输入图片说明

UPDATE 2 更新2

This works pretty well (modification of Pierre's code): 效果很好(修改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;

Gives me: 给我:

在此处输入图片说明

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. 问题不在于将它们全部添加到组框,而是将它们全部显示。

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

I'm defining a variable called radButtonPosition and initializing it to your groupbox's position. 我正在定义一个名为radButtonPosition的变量,并将其初始化到组框的位置。 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. 然后,我根据它设置radButtonPosition按钮的位置,然后每次添加一个radButtonPosition按钮的高度时,就简单地将radButtonPosition增加一个。

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

There's of course a LOT of method extraction to do to respect SRP, but that's a start. 当然,要遵循SRP,需要做很多方法提取工作,但这只是一个开始。

All the items are a Location 0,0 Try this 所有项目都是一个位置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;
    }

Also can insert a FlowLayoutPanel inside the GroupBox, then add the RadioButton to the FlowLayoutPanel, for a automatic placement of the components 也可以将FlowLayoutPanel插入GroupBox内,然后将RadioButton添加到FlowLayoutPanel中,以自动放置组件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么当RadioButton文本值短时,自动调整GroupBox及其RadioButton子级的大小不会减小GroupBox的宽度? - Why does Autosizing a GroupBox and its RadioButton Children not reduce the GroupBox's width when the RadioButton text values are short? 优化Relevent GroupBox中选中的单选按钮的检索 - Optimised Retrieval of the Checked RadioButton in Relevent GroupBox C#默认选中GroupBox中的RadioButton - C# default checked RadioButton in a GroupBox DropDownListboxes - 为什么要添加这些数据? - DropDownListboxes - why is this data being added? 为什么要添加新项目? - Why is a new item being added? 为什么不添加这些表单元格? - Why are these table cells not being added? 为什么我的排序行为仅在列表中只有一个项目时发生,并且只有在第一次添加集合时才发生? - Why does my sort behavior fire with only one item in the list, and only the first time the collection is added to? 如果Label在GroupBox中,为什么Label控件不会在foreach循环中被命中? - Why Label controls aren't being hit in a foreach loop if the Label is in a GroupBox? C#-如果在GroupBox中选择了单选按钮,请在其他GroupBox中取消选择其他单选按钮 - C# - If A Radiobutton Selected In GroupBox, Unselect Other Radiobuttons In Other GroupBoxes 在MVVM之后确定WPF中来自groupbox的已检查Radiobutton - Determining checked Radiobutton from groupbox in WPF following MVVM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM