C# 为什么我的动态创建的GroupBox在后续使用中会将其单选按钮放得太右?
我将根据用户的选择,向面板添加各种动态创建的控件。如果带有关联单选按钮的Groupbox是第一个控件,则它看起来很好: …但是如果不是这样,相关的单选按钮看起来是右对齐的,而不是左对齐的,如上所示,而且groupbox太宽,无法启动 下面是相关的代码(repainmockuppanel()是在用户选择随时查看其模型的外观时调用的,getGroupBox()是它调用的方法,应该是问题所在,但我看不到它C# 为什么我的动态创建的GroupBox在后续使用中会将其单选按钮放得太右?,c#,dynamic,position,radio-button,groupbox,C#,Dynamic,Position,Radio Button,Groupbox,我将根据用户的选择,向面板添加各种动态创建的控件。如果带有关联单选按钮的Groupbox是第一个控件,则它看起来很好: …但是如果不是这样,相关的单选按钮看起来是右对齐的,而不是左对齐的,如上所示,而且groupbox太宽,无法启动 下面是相关的代码(repainmockuppanel()是在用户选择随时查看其模型的外观时调用的,getGroupBox()是它调用的方法,应该是问题所在,但我看不到它 private void RepaintMockupPanel(Control padre)
private void RepaintMockupPanel(Control padre)
{
const string BTN = "BUTTON";
const string CKBX = "CHECKBOX";
const string EDTTXT = "EDITTEXT";
const string RADGRP = "RADIOGROUP";
const string SPNR = "SPINNER";
const string TXTVU = "TEXTVIEW";
const int LEFT_STARTING_POINT = 4;
const int STANDARD_PADDING = 4;
int currentLeft = LEFT_STARTING_POINT;
string currentSel;
string currentSettings;
ComboBox cmbx;
Label lbl;
try
{
TabPage tp = padre as TabPage;
string panelName = tp.Name.Replace("tabPage", "panel");
Panel p = tp.Controls[panelName] as Panel;
p.Controls.Clear();
for (int i = 0; i < p.Controls.Count; i++)
{
p.Controls[i].Dispose();
}
//cmbxRow0Element0 and lblRow0Element0 to cmbxRow11Element5 and lblRow11Element5
int rowNum = getRowNum(panelName);
for (int i = 0; i < WIDGETS_PER_TABPAGE; i++)
{
cmbx = tp.Controls[string.Format("cmbxRow{0}Element{1}", rowNum, i)] as ComboBox;
lbl = tp.Controls[string.Format("lblRow{0}Element{1}", rowNum, i)] as Label;
if (cmbx.SelectedIndex < 0) continue;
currentSel = cmbx.SelectedItem.ToString().ToUpper();
currentSettings = lbl.Text;
// Possible vals (Android on left, Windows equivalents on the right:
//Button ""
//CheckBox ""
//EditText TextBox
//RadioGroup GroupBox (w. RadioButtons nested within)
//Spinner ComboBox
//TextView Label
if ((currentSel.Length > 0) && (currentSettings.Length > 0))
{
if (currentSel.Equals(BTN))
{
Button btn = getButton(currentSettings, currentLeft);
p.Controls.Add(btn);
currentLeft += btn.Width + STANDARD_PADDING;
}
else if (currentSel.Equals(CKBX))
{
CheckBox ckbx = getCheckBox(currentSettings, currentLeft);
p.Controls.Add(ckbx);
currentLeft += ckbx.Width + STANDARD_PADDING;
}
else if (currentSel.Equals(EDTTXT))
{
TextBox txtbx = getTextBox(currentSettings, currentLeft);
p.Controls.Add(txtbx);
currentLeft += txtbx.Width + STANDARD_PADDING;
}
else if (currentSel.Equals(RADGRP))
{
GroupBox grpbx = getGroupBox(currentSettings, currentLeft);
p.Controls.Add(grpbx);
currentLeft += grpbx.Width + STANDARD_PADDING;
}
else if (currentSel.Equals(SPNR))
{
ComboBox cmbxDyn = getComboBox(currentSettings, currentLeft);
p.Controls.Add(cmbxDyn);
currentLeft += cmbxDyn.Width + STANDARD_PADDING;
}
else if (currentSel.Equals(TXTVU))
{
Label lblDyn = getLabel(currentSettings, currentLeft);
p.Controls.Add(lblDyn);
currentLeft += lblDyn.Width + STANDARD_PADDING;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
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))
// Adapted from Pierre's answer at http://stackoverflow.com/questions/23944419/why-is-only-the-first-radiobutton-being-added-to-the-groupbox
IList<string> grpbxVals = new List<string>(currentSettings.Split('~'));
GroupBox gb = new GroupBox { Height = 60, Location = new Point(curLeftVal, 0) };
gb.AutoSize = true;
int radButtonYVal = 0;
for (int i = 0; i < grpbxVals.Count() - 1; i++)
{
//gb.Controls.Add(new RadioButton { Text = grpbxVals[i], Location = new Point(curLeftVal, radButtonPosition) });
gb.Controls.Add(new RadioButton { Text = grpbxVals[i], Location = new Point(gb.Location.X+2, radButtonYVal) });
radButtonYVal += new RadioButton().Height;
}
return gb;
}
private void repainmockuppanel(控制面板)
{
常量字符串BTN=“按钮”;
常量字符串CKBX=“复选框”;
常量字符串EDTXT=“EDITTEXT”;
常量字符串RADGRP=“RADIOGROUP”;
常量字符串SPNR=“微调器”;
常量字符串TXTVU=“TEXTVIEW”;
const int LEFT_起点=4;
常量int标准_PADDING=4;
int currentLeft=左起点;
串电流选择;
字符串设置;
组合框cmbx;
标签lbl;
尝试
{
TabPage tp=padre作为TabPage;
字符串panelName=tp.Name.Replace(“tabPage”、“panel”);
面板p=tp。控制[panelName]作为面板;
p、 控件。清除();
对于(int i=0;i0)和&(currentSettings.Length>0))
{
如果(当前选择等于(BTN))
{
按钮btn=getButton(currentSettings,currentLeft);
p、 控件。添加(btn);
currentLeft+=btn.宽度+标准填充;
}
else如果(当前选择等于(CKBX))
{
复选框ckbx=getCheckBox(currentSettings,currentLeft);
p、 控件。添加(ckbx);
currentLeft+=ckbx.宽度+标准填充;
}
else if(当前选择等于(EDTXT))
{
TextBox txtbx=getTextBox(currentSettings,currentLeft);
p、 控件。添加(txtbx);
currentLeft+=txtbx.宽度+标准_填充;
}
否则如果(当前选择等于(RADGRP))
{
GroupBox grpbx=getGroupBox(currentSettings,currentLeft);
p、 控制。添加(grpbx);
currentLeft+=grpbx.宽度+标准填充;
}
否则如果(当前选择等于(SPNR))
{
ComboBox cmbxDyn=getComboBox(currentSettings,currentLeft);
p、 控件。添加(cmbxDyn);
currentLeft+=cmbxDyn.宽度+标准填充;
}
else if(当前选择等于(TXTVU))
{
Label lblDyn=getLabel(currentSettings,currentLeft);
p、 控件。添加(lblDyn);
currentLeft+=lblDyn.宽度+标准填充;
}
}
}
}
捕获(例外情况除外)
{
Show(例如ToString());
}
}
私有GroupBox getGroupBox(字符串currentSettings,int curLeftVal)
{
//“apple~orange~peach~True(必须查找“将组封装在黑盒子中”作为最后一个val(尽管忽略快速脏模型))
//改编自皮埃尔在http://stackoverflow.com/questions/23944419/why-is-only-the-first-radiobutton-being-added-to-the-groupbox
IList grpbxVals=新列表(currentSettings.Split(“~”);
GroupBox gb=新GroupBox{Height=60,Location=new Point(curLeftVal,0)};
gb.AutoSize=true;
int radButtonYVal=0;
对于(int i=0;i
问题确实出在getGroupBox()方法上
作为一个容器,GroupBox有它自己的画布,在画布上绘制它的子控件,因此当您创建一个X值为5的控件时,这意味着它从GroupBox的左边算起是5,而不是从表单的左边算起。表单上的绝对值是它自己的X值(在本例中是5)加上GroupBox的X值(我们假设它的左值为25)表示从左边算起30的绝对位置
这就是为什么您的示例显示了迄今为止按下的单选按钮:如果检查单选按钮的左边缘与其包含的GroupBox的左边缘之间的距离,它应该与GroupBox的左边缘与其容器的左边缘之间的距离大致相同。getGroupBox()方法确实是问题所在
private void button1_Click(object sender, EventArgs e)
{
for (int t = 0; t < 4;t++ )
tabControl1.TabPages.Add(CreateTabPage(t));
}
private TabPage CreateTabPage(int t)
{
TabPage result = new TabPage()
{
Text=string.Format("TabPage {0}",t)
};
FlowLayoutPanel flp = new FlowLayoutPanel()
{
Dock = DockStyle.Fill,
AutoScroll = true,
};
for (int i = 0; i < 10; i++)
{
flp.Controls.Add(CreateGroupBox(i));
}
result.Controls.Add(flp);
return result;
}
private Control CreateGroupBox(int i)
{
GroupBox result = new GroupBox()
{
Text = string.Format("GroupBox {0}", i),
Width = 150,
Height = 100
};
FlowLayoutPanel flp = new FlowLayoutPanel()
{
Dock = DockStyle.Fill,
WrapContents = false,
AutoScroll = true,
FlowDirection=FlowDirection.TopDown
};
CreateRadios(flp, i);
result.Controls.Add(flp);
return result;
}
private void CreateRadios(FlowLayoutPanel flp, int i)
{
for (int c = 0; c < 10; c++) {
flp.Controls.Add(new RadioButton()
{
Text = string.Format("RadioButton {0} in {1}", c, i)
});
}
}
int radButtonYVal = 4;
int leftVal = 4;
for (int i = 0; i < grpbxVals.Count() - 1; i++)
{
gb.Controls.Add(new RadioButton { Text = grpbxVals[i], AutoSize = true, Location = new Point(leftVal, radButtonYVal) });
radButtonYVal += new RadioButton().Height -4; // the "-4" is a kludge to scrunch the radiobuttons together a bit
}