C# 根据Combobox控件来动态显示TabControl下的子元素
根据下来列表来动态显示TabControl下的元素
需要准备两个控件:Combobox (命名为:cbPrjType)和 TabControl (命名为:tabPrjType),TabControl下面有六个子元素分别是:tabPage1,tabPage2,tabPage3,tabPage4,tabPage5,tabPage6
我们可以想到加载界面的时候会给cbPrjType绑定元素,代码如下:
private void InitProjectTypeDic() //绑定下拉值
{
string strMsg = "";
CorpProjectAddClass cpas = new CorpProjectAddClass();
dsDic = cpas.GetDicDataSet(out strMsg); //查询定义的字典表
if (dsDic.Tables["XMBL_TbProjectTypeDic_SZ"] != null)
{
this.cbPrjType.Items.Clear(); //清空集合元素
this.cbPrjType.Items.Add(new MyItem("", "")); //为第一个option添加为空
foreach (DataRow dr in dsDic.Tables["XMBL_TbProjectTypeDic_SZ"].Rows)
{
this.cbPrjType.Items.Add(new MyItem(dr["ProjectTypeNum"].ToString(), dr["ProjectTypeName"].ToString()));
}
this.cbPrjType.DisplayMember = "Name"; //显示的属性(显示)
this.cbPrjType.ValueMember = "ID"; //选项中实际的值(隐藏)
this.cbPrjType.SelectedIndex = ; //默认选中第一个
}
}
其次给cbPrjType添加改变下拉事件SelectedIndexChanged:
private void cbPrjType_SelectedIndexChanged(object sender, EventArgs e)
{
string cbVal=""; //记录当前选中下拉的值
if (cbPrjType.SelectedItem != null && (cbPrjType.SelectedItem as MyItem).ID != "")
cbVal = (cbPrjType.SelectedItem as MyItem).Name; //获取选中下拉的值
else
return; this.tabPage1.Parent = null; //指定tabPage1的父元素为空(可实现隐藏作用)
this.tabPage2.Parent = null;
this.tabPage3.Parent = null;
this.tabPage4.Parent = null;
this.tabPage5.Parent = null;
this.tabPage6.Parent = null; JempType(cbVal); //传入选中的值,从而判断显示哪个tabPage
}
根据选中元素的值来进行判断:
private void JempType(string prjType)
{
switch (prjType)
{
case "城市道路工程":
this.tabPage1.Text = "城市道路工程"; //tabPage显示的名称
this.tabPage1.Parent = tabPrjType; //给tabPage指定父元素
tabPrjType.Enabled = true;
break; case "城市桥梁工程":
this.tabPage2.Text = "城市桥梁工程";
this.tabPage2.Parent = tabPrjType;
tabPrjType.Enabled = true;
break; case "排水管道":
this.tabPage3.Text = "排水管道";
this.tabPage3.Parent = tabPrjType;
tabPrjType.Enabled = true;
break;
case "供水管道":
this.tabPage3.Text = "供水管道";
this.tabPage3.Parent = tabPrjType;
tabPrjType.Enabled = true;
break;
case "中水管道":
this.tabPage3.Text = "中水管道";
this.tabPage3.Parent = tabPrjType;
tabPrjType.Enabled = true;
break;
case "燃气管道":
this.tabPage3.Text = "燃气管道";
this.tabPage3.Parent = tabPrjType;
tabPrjType.Enabled = true;
break;
case "热力管道":
this.tabPage3.Text = "热力管道";
this.tabPage3.Parent = tabPrjType;
tabPrjType.Enabled = true;
break; case "污水处理厂":
this.tabPage4.Text = "污水处理厂";
this.tabPage4.Parent = tabPrjType;
tabPrjType.Enabled = true;
break;
case "供水厂":
this.tabPage4.Text = "供水厂";
this.tabPage4.Parent = tabPrjType;
tabPrjType.Enabled = true;
break;
case "给水泵站":
this.tabPage4.Text = "给水泵站";
this.tabPage4.Parent = tabPrjType;
tabPrjType.Enabled = true;
break;
case "排水泵站":
this.tabPage4.Text = "排水泵站";
this.tabPage4.Parent = tabPrjType;
tabPrjType.Enabled = true;
break;
case "垃圾处理工程":
this.tabPage4.Text = "垃圾处理工程";
this.tabPage4.Parent = tabPrjType;
tabPrjType.Enabled = true;
break; case "城市隧道工程":
this.tabPage5.Text = "城市隧道工程";
this.tabPage5.Parent = tabPrjType;
tabPrjType.Enabled = true;
break; case "公共交通工程":
this.tabPage6.Text = "公共交通工程";
this.tabPage6.Parent = tabPrjType;
tabPrjType.Enabled = true;
break;
case "轨道交通工程":
this.tabPage6.Text = "轨道交通工程";
this.tabPage6.Parent = tabPrjType;
tabPrjType.Enabled = true;
break;
case "环节卫生工程":
this.tabPage6.Text = "环节卫生工程";
this.tabPage6.Parent = tabPrjType;
tabPrjType.Enabled = true;
break;
case "照明工程":
this.tabPage6.Text = "照明工程";
this.tabPage6.Parent = tabPrjType;
tabPrjType.Enabled = true;
break;
case "绿化工程":
this.tabPage6.Text = "绿化工程";
this.tabPage6.Parent = tabPrjType;
tabPrjType.Enabled = true;
break;
case "电力工程":
this.tabPage6.Text = "电力工程";
this.tabPage6.Parent = tabPrjType;
tabPrjType.Enabled = true;
break;
case "通信工程":
this.tabPage6.Text = "通信工程";
this.tabPage6.Parent = tabPrjType;
tabPrjType.Enabled = true;
break;
} }
如果想循环遍历TabContorl下所有的TextBox控件并赋值为空可以这么写:
foreach (TabPage page in tabControl1.TabPages)
{
foreach (Control control in page.Controls)
{
if (control is TextBox)
{
((TextBox)control) = "";
}
if (control is ComboBox)
{
((ComboBox)control).SelectedIndex = -;
}
}
}
获取选中下拉的问本值:
comboBox1.GetItemText(comboBox1.Items[comboBox1.SelectedIndex]);
实现效果如下:


C# 根据Combobox控件来动态显示TabControl下的子元素的更多相关文章
- ExtJS基础知识总结:自定义日历和ComboBox控件(二)
概述 1.ExtJS 5不支持日期选择框中只选择年月,为了满足ExtJs5可以实现选择年月的功能,查询网上资料,整理出来了相应的处理方式,最终实现的效果如下图: 2.ExtJS 控件丰富,如果需要实现 ...
- 加载ComboBox控件
/// <summary> /// 加载公司 /// </summary> /// <param name="cbbCompany">Combo ...
- C#中combobox 控件属性、事件、方法
一 .combobox 属性.事件.方法公共属性 名称 说明 AccessibilityObject 获取分配给该控件的 AccessibleObject. AccessibleDefaultActi ...
- .Net平台Winform两个ComboBox控件绑定同一个数据源
今天WINFROM编程遇到这么一个问题:是有关WINFORM中两个comboBox控件绑定同一个数据源的问题,在窗体的界面上有两个comboBox,我在Form1_Load中对他们做了数据绑定(具体代 ...
- WinForm编程时窗体设计器中ComboBox控件大小的设置
问题描述: 在VS中的窗体设计器中拖放一个ComboBox控件后想调整控件的大小.发现在控件上用鼠标只能拖动宽度(Width)无法拖动(Height). 解决过程: 1.控件无法拖动,就在属性窗口中设 ...
- Winform ComboBox控件高亮显示
//重绘下拉表单窗口,需要在窗口设计代码中加入下面这一句 this.cmdChannelName.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawF ...
- winform combobox控件绑定 分类: WinForm 2014-04-17 14:34 118人阅读 评论(0) 收藏
想要达到的效果:把数据库中的一列数据绑定到combobox控件中. 数据库表:T_Task//任务表 列名:Task_Name//名称 主键:Task_ID combobox控件名称:cbName 解 ...
- ComboBox控件绑定数据源
最近在研究机房收费系统的组合查询的方法时,看到了ComboBox控件可以进行数据绑定,我觉得这个功能真的很不错,可以给我省去很多的麻烦. 下面是我组合查询窗体界面 一.数据转换方法 现在我们开看一下我 ...
- WPF中实现多选ComboBox控件
在WPF中实现带CheckBox的ComboBox控件,让ComboBox控件可以支持多选. 将ComboBox的ItemsSource属性Binding到一个Book的集合, public clas ...
随机推荐
- 集群环境下定时调度的解决方案之Quartz集群
集群环境可能出现的问题 在上一篇博客我们介绍了如何在自己的项目中从无到有的添加了Quartz定时调度引擎,其实就是一个Quartz 和Spring的整合过程,很容易实现,但是我们现在企业中项目通常都是 ...
- uboot——git代码仓
1,注册GitHub帐号,创建GitHub项目代码仓库 https://www.cnblogs.com/LoTGu/p/6075994.html 参考其第二段,注册账号,设置仓库. 2,上传代码 测试 ...
- HTML第一篇
Hyper Text Markup Language 超文本标记语言:是一种创建网页的标准标记语言. <!DOCTYPE html> <html> <head> ...
- 选择困难症的福音——团队Scrum冲刺阶段-Day 7
选择困难症的福音--团队Scrum冲刺阶段-Day 7 今日进展 测试代码 将界面设计完后放app使用示意图于此 今日贡献量 严域俊 吴恒佚 曾程 刘辰 邓煜坤 3.5 3.5 3.3 3.6 3 贡 ...
- [Python] 怎么把HTML的报告转换为图片,利用无头浏览器
How to convert HTML Report to picture format in Email? So that we can see the automation report also ...
- downLoad
String root= ServletActionContext.getServletContext().getRealPath(File.separator).replace("\\&q ...
- Java-static关键字解析
static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键字的用法和平常容易误解的地方,最后列 ...
- MD5=======RBAC权限管理
经过网上查阅相关的说明原来,MD5全名Message-Digest Algorithm 5(信息-摘要算法)是一种不可逆的加密算法. MD5为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性 ...
- Python Day 14 迭代器、for循环原理、枚举、生成器
阅读内容 内容回顾 带参装饰器和wraps用法 迭代器知识引入 可迭代对象 迭代器对象 for循环迭代器 枚举对象 生成器 ##内容回顾 函数的嵌套定义:在函数内部定义另一 ...
- Linux 网卡Bond模式
网卡bond是通过把多张网卡绑定为一个逻辑网卡,实现本地网卡的冗余,带宽扩容和负载均衡. 有7种模式: mod 0/mod 1/mod 2/mod 3/mod 4/mod 5 mod=0 ,即:(ba ...