[英]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;
}
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): 效果很好(修改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.