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 ...
随机推荐
- Jupyter Notebook 的快捷键
原文:http://blog.csdn.net/lawme/article/details/51034543 Jupyter Notebook 的快捷键 Jupyter Notebook 有两种键盘输 ...
- [leetcode]24. Swap Nodes in Pairs交换节点对
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- rabbit初学之连接测试2
com.rabbitmq.client.ShutdownSignalException: connection error 发现,port是5672,不是15672(15672是后台管理平台的端口)
- 常用screen参数
摘自:https://www.cnblogs.com/webnote/p/5749675.html screen -S yourname -> 新建一个叫yourname的sessionscre ...
- Hbase 性能改进
第一种性能改进方式:
- Python中添加中文注释报错SyntaxError: Non-UTF-8 code starting with '\xc1'
问题:在文本编辑器中编辑Python文件时添加中文注释,运行python文件时报错.SyntaxError: Non-UTF-8 code starting with '\xc1' 解决方法:在文本开 ...
- #10072. 「一本通 3.2 例 1」Sightseeing Trip(floyd求最小环+路径)
https://loj.ac/problem/10072 针对无向图 因为Floyd是按照结点的顺序更新最短路的,所以我们在更新最短路之前先找到一个连接点k,当前的点k肯定不存在于已存在的最短路f[i ...
- mui.fire()触发自定义事件
导读:添加自定义事件监听操作和标准js事件监听类似,可直接通过window对象添加,通过mui.fire()方法可触发目标窗口的自定义事件. 监听自定义事件 添加自定义事件监听操作和标准js事件监听类 ...
- C++探究foreach算法
for_each在algorithm.h 中 template<class _InIt, class _Fn1> inline _Fn1 for_each(_InIt _First, _I ...
- 4k项目--PHY通道绑定的两种模式
1.通道绑定有两种模式: • PMA bonding• PMA and PCS bonding GT通道是不支持通道绑定的 2.PMA绑定 PMA绑定减少了PMA之间的通道之间的Skew.并且在PMA ...