场景
有时候需要根据配置文件在窗体中使用代码动态生成控件。
比如读取xml配置文件中的节点数量,然后在窗体中生成指定数量的RadioGroup控件。
实现
新建一个窗体,在窗体的加载完之后的事件中
private void ChartOption_Load(object sender, EventArgs e)
{
//根据配置文件路径初始化图形选项Dialog
initChartOptionPane(Global.RADIO_GROUP_YAXIS_FILEPATH);
}
传入配置文件路径,然后生成控件。
public void initChartOptionPane(string xmlFilePath)
{
//不同组水平方向控件之间的距离 = 控件的宽度 + 组与组之间的距离
int horizontalDistance = Global.NOGROUP_HORIZONTAL_DISTANCE + Global.CHART_OPTION_WIDTH;
//获取Y轴的list
List<YAxisModel> yList = ChartOptionHelper.getYAxisList(xmlFilePath);
//获取配置文件中的radioGroup的选项
List<string> radioGrupItems = initRadioGroupItem(Global.RADIO_GROUP_ITEM_FILEPATH);
//生成对应组元素
for (int i = 0; i < yList.Count; i++)
{
//生成一个radioGroup
RadioGroup radioGroup = new RadioGroup();
foreach (string s in radioGrupItems)
{
//每一个单元按钮对应的选项item
RadioGroupItem item = new RadioGroupItem();
//设置选项的value值
item.Value = s;
//设置选项的描述值 即 要显示的值
item.Description = s;
//使选项启用
item.Enabled = true;
//将新增的选项添加到radiogroup的Items中
radioGroup.Properties.Items.Add(item);
}
//设置宽度
radioGroup.Width = Global.CHART_OPTION_WIDTH;
//加1 保留出空白选项的高度
radioGroup.Height = Global.RADIO_HEIGHT * (radioGrupItems.Count + 1);
//设置位置
radioGroup.Location = new Point(Global.NOGROUP_HORIZONTAL_DISTANCE + i * horizontalDistance, Global.GROUP_VERTICAL_DISTANCE);
this.Controls.Add(radioGroup);
//添加下限输入框
TextEdit minTextEdit = new TextEdit();
minTextEdit.Text = yList[i].Min.ToString();
minTextEdit.Width = Global.CHART_OPTION_WIDTH;
minTextEdit.Location = new Point(Global.NOGROUP_HORIZONTAL_DISTANCE + i * horizontalDistance, Global.GROUP_VERTICAL_DISTANCE * 2 + radioGroup.Height);
this.Controls.Add(minTextEdit);
//添加上限输入框
TextEdit maxTextEdit = new TextEdit();
maxTextEdit.Text = yList[i].Max.ToString();
maxTextEdit.Location = new Point(Global.NOGROUP_HORIZONTAL_DISTANCE + i * horizontalDistance, Global.GROUP_VERTICAL_DISTANCE * 3 + radioGroup.Height + minTextEdit.Height);
maxTextEdit.Width = Global.CHART_OPTION_WIDTH;
this.Controls.Add(maxTextEdit);
//添加颜色选择器
ColorPickEdit colorPickEdit = new ColorPickEdit();
colorPickEdit.Text = yList[i].Color;
colorPickEdit.Width = Global.CHART_OPTION_WIDTH;
colorPickEdit.Location = new Point(Global.NOGROUP_HORIZONTAL_DISTANCE + i * horizontalDistance, Global.GROUP_VERTICAL_DISTANCE * 4 + radioGroup.Height + minTextEdit.Height + maxTextEdit.Height);
this.Controls.Add(colorPickEdit);
}
}
注:
1.生成控件对象。
2.设置控件对象的显示值text属性以及宽度等属性。
3.设置控件的位置。
4.将控件添加进窗体。
主要实现代码:
for (int i = 0; i < yList.Count; i++)
{
//生成一个radioGroup
RadioGroup radioGroup = new RadioGroup();
foreach (string s in radioGrupItems)
{
//每一个单元按钮对应的选项item
RadioGroupItem item = new RadioGroupItem();
//设置选项的value值
item.Value = s;
//设置选项的描述值 即 要显示的值
item.Description = s;
//使选项启用
item.Enabled = true;
//将新增的选项添加到radiogroup的Items中
radioGroup.Properties.Items.Add(item);
}
//设置宽度
radioGroup.Width = Global.CHART_OPTION_WIDTH;
//加1 保留出空白选项的高度
radioGroup.Height = Global.RADIO_HEIGHT * (radioGrupItems.Count + 1);
//设置位置
radioGroup.Location = new Point(Global.NOGROUP_HORIZONTAL_DISTANCE + i * horizontalDistance, Global.GROUP_VERTICAL_DISTANCE);
this.Controls.Add(radioGroup);
//添加下限输入框
TextEdit minTextEdit = new TextEdit();
minTextEdit.Text = yList[i].Min.ToString();
minTextEdit.Width = Global.CHART_OPTION_WIDTH;
minTextEdit.Location = new Point(Global.NOGROUP_HORIZONTAL_DISTANCE + i * horizontalDistance, Global.GROUP_VERTICAL_DISTANCE * 2 + radioGroup.Height);
this.Controls.Add(minTextEdit);
//添加上限输入框
TextEdit maxTextEdit = new TextEdit();
maxTextEdit.Text = yList[i].Max.ToString();
maxTextEdit.Location = new Point(Global.NOGROUP_HORIZONTAL_DISTANCE + i * horizontalDistance, Global.GROUP_VERTICAL_DISTANCE * 3 + radioGroup.Height + minTextEdit.Height);
maxTextEdit.Width = Global.CHART_OPTION_WIDTH;
this.Controls.Add(maxTextEdit);
//添加颜色选择器
ColorPickEdit colorPickEdit = new ColorPickEdit();
colorPickEdit.Text = yList[i].Color;
colorPickEdit.Width = Global.CHART_OPTION_WIDTH;
colorPickEdit.Location = new Point(Global.NOGROUP_HORIZONTAL_DISTANCE + i * horizontalDistance, Global.GROUP_VERTICAL_DISTANCE * 4 + radioGroup.Height + minTextEdit.Height + maxTextEdit.Height);
this.Controls.Add(colorPickEdit);
}
效果
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。