c# winform窗体实现控件内容的动态更新
在系统开发的过程中,往往会遇到需要动态的控制控件内部显示的控件数量、控件类型的情况,比如这样的。
1、问题描述:如何自定义的控制控件中数据显示的类型呢?
首先面对这个问题,我们得先了解winform的控制工具中有哪些控件是可以用来承载其他控件工具的,例如以下控件都可以用来承载其他控件工具,那么就可以使用这些控件来动态的实现工具栏的生成。
此处采用一个panel控件进行演示。
首先,我们添加一个winform窗体,然后往该窗体中拖入一个panel控件
在这个窗体中我们插入一个comboBox控件,用于控制panel中生成工具栏的数量,此外每个工具栏中还将包括不同的控件工具,例如包括 textbox、label、button、radioButton,checkbox等等,只要是你需要的控件工具都可以自行添加。
我们进入button按钮下,开始编写用于panel内部控件绘制的代码
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
Label panel_show_label = new Label()
{
Name = "resulr_list_table",
Text = "标签",
ForeColor = ColorTranslator.FromHtml("#000000"),
Height = 20,
Width = 40
};
panel_show_label.TextAlign = ContentAlignment.MiddleCenter;
panel_show_label.Location = new Point(35, 5);
panel1.Controls.Add(panel_show_label);
Label panel_show_id = new Label()
{
Name = "resulr_list_id",
Text = "关联ID",
ForeColor = ColorTranslator.FromHtml("#000000"),
Height = 20,
Width = 50
};
panel_show_id.TextAlign = ContentAlignment.MiddleCenter;
panel_show_id.Location = new Point(panel_show_label.Right + 10, 5);
panel1.Controls.Add(panel_show_id);
Label resulr_list_lev = new Label()
{
Name = "resulr_list_lev",
Text = "离开",
ForeColor = ColorTranslator.FromHtml("#000000"),
Height = 20,
Width = 40
};
resulr_list_lev.TextAlign = ContentAlignment.MiddleCenter;
resulr_list_lev.Location = new Point(panel_show_id.Right + 10, 5);
panel1.Controls.Add(resulr_list_lev);
Label resulr_list_notLev = new Label()
{
Name = "resulr_list_notLev",
Text = "未离开",
ForeColor = ColorTranslator.FromHtml("#000000"),
Height = 20,
Width = 60
};
resulr_list_notLev.TextAlign = ContentAlignment.MiddleCenter;
resulr_list_notLev.Location = new Point(resulr_list_lev.Right + 10, 5);
panel1.Controls.Add(resulr_list_notLev);
Label resulr_list_dlt = new Label()
{
Name = "resulr_list_dlt",
Text = "删除",
ForeColor = ColorTranslator.FromHtml("#000000"),
Height = 20,
Width = 40
};
resulr_list_dlt.TextAlign = ContentAlignment.MiddleCenter;
resulr_list_dlt.Location = new Point(resulr_list_notLev.Right + 10, 5);
panel1.Controls.Add(resulr_list_dlt);
Label resulr_list_see = new Label()
{
Name = "resulr_list_see",
Text = "可见",
ForeColor = ColorTranslator.FromHtml("#000000"),
Height = 20,
Width = 40
};
resulr_list_see.TextAlign = ContentAlignment.MiddleCenter;
resulr_list_see.Location = new Point(resulr_list_dlt.Right + 10, 5);
panel1.Controls.Add(resulr_list_see);
panel1.Width = resulr_list_see.Right + 40;
int resNumList = int.Parse(comboBox1.Text);
for (int i = 0; i < resNumList; i++)
{
Panel panel = new Panel()
{
Name = "reg_panel_" + i.ToString(),
Width = panel1.Width,
ForeColor = ColorTranslator.FromHtml("#000000"),
BackColor = ColorTranslator.FromHtml("#D3D3D3"),
Height = 30
};
Label index_label = new Label()
{
Name = "reg_index_" + i.ToString(),
Text = i.ToString(),
ForeColor = ColorTranslator.FromHtml("#000000"),
Height = 20,
Width = 30
};
index_label.TextAlign = ContentAlignment.MiddleCenter;
index_label.Location = new Point(0, 5);
panel.Controls.Add(index_label);
Label reg_label = new Label()
{
Name = "reg_label_" + i.ToString(),
Text = i.ToString(),
ForeColor = ColorTranslator.FromHtml("#000000"),
BackColor = ColorTranslator.FromHtml("#FFA500"),
Height = 20,
Width = 40
};
reg_label.TextAlign = ContentAlignment.MiddleCenter;
reg_label.Location = new Point(30 + 5, 5);
panel.Controls.Add(reg_label);
Label reg_id = new Label()
{
Name = "reg_id_" + i.ToString(),
Text = i.ToString(),
ForeColor = ColorTranslator.FromHtml("#000000"),
Height = 20,
Width = 40
};
reg_id.TextAlign = ContentAlignment.MiddleCenter;
reg_id.Location = new Point(panel_show_id.Location.X, 5);
panel.Controls.Add(reg_id);
//reg_id.Click += new System.EventHandler(panel1_EventChange);
RadioButton reg_lev = new RadioButton()
{
Name = "reg_lev_" + i.ToString(),
ForeColor = ColorTranslator.FromHtml("#000000"),
Height = 20,
Width = 40
};
reg_lev.Location = new Point(10 + resulr_list_lev.Location.X, 5);
panel.Controls.Add(reg_lev);
RadioButton reg_notLev = new RadioButton()
{
Name = "reg_notLev_" + i.ToString(),
ForeColor = ColorTranslator.FromHtml("#000000"),
Height = 20,
Width = 40
};
reg_notLev.Location = new Point(15 + resulr_list_notLev.Location.X, 5);
panel.Controls.Add(reg_notLev);
RadioButton reg_dlt = new RadioButton()
{
Name = "reg_dlt_" + i.ToString(),
ForeColor = ColorTranslator.FromHtml("#000000"),
Height = 20,
Width = 40
};
reg_dlt.Location = new Point(10 + resulr_list_dlt.Location.X, 5);
panel.Controls.Add(reg_dlt);
CheckBox reg_see = new CheckBox()
{
Name = "reg_see_" + i.ToString(),
ForeColor = ColorTranslator.FromHtml("#000000"),
Height = 20,
Width = 40
};
reg_see.Location = new Point(10 + resulr_list_see.Location.X, 5);
panel.Controls.Add(reg_see);
panel.Location = new Point(0, panel_show_label.Bottom + i * 35);
panel1.Controls.Add(panel);
}
}
需要注意的是,在工具栏中我们用到了三个RadioButton,所以需要在工具栏的生成还需要使用一个panel控件来容纳所需的控件工具。
Panel panel = new Panel()
{
Name = "reg_panel_" + i.ToString(),
Width = panel1.Width,
ForeColor = ColorTranslator.FromHtml("#000000"),
BackColor = ColorTranslator.FromHtml("#D3D3D3"),
Height = 30
};
最后当所有工具添加完成后再将该panel控件加入到panel1容器中。文章来源:https://www.toymoban.com/news/detail-568162.html
panel1.Controls.Add(panel);
本文仅就控件内控件工具的动态控制和跟新进行简单的介绍,相对复杂的控件处理可以在上述方法的基础上进行修改或改进。文章来源地址https://www.toymoban.com/news/detail-568162.html
到了这里,关于c# winform实现控件类型、数量的动态更新的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!