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 ...
随机推荐
- 《CSAPP》符号和符号表
符号和符号表 每个可重定位目标模块m都有一个符号表,它包含m所定义和引用的符号的信息. 有三种不同的符号: 由m定义并能被其他模块引用的全局符号.对应非静态的C函数以及不带C static属性的全局变 ...
- Jenkins之定时任务
H的用法: H 10 * * * ,这里H不是小时的意思,符号H(代表“Hash”,后面用“散列”代替) 符号H 在一定范围内可被认为是一个随机值,但实际上它是任务名称的一个散列而不是随机函数,每个 ...
- mysql 查询结果集按照指定的字段值顺序排序
mysql 查询结果如果不给予指定的order by ,那么mysql会按照主键顺序(innodb引擎)对结果集加以排序,那么最后的排序可能就不是你想要的排序结果. 举个例子,我要按照前端传过来的mo ...
- Java日志框架-logback的介绍及配置使用方法(纯Java工程)(转)
说明:内容估计有些旧,2011年的,但是大体意思应该没多大变化,最新的配置可以参考官方文档. 一.logback的介绍 Logback是由log4j创始人设计的又一个开源日志组件.logback当前分 ...
- Java14-java语法基础(十三)接口
Java14-java语法基础(十三)接口 一.接口 1.接口的作用 Java出于安全性.简化程序结构的考虑,不支持多继承而仅支持单继承.然而实际问题中很多情况下仅仅依靠单继承并不能将复杂的问题描述清 ...
- ORM常用操作
一般操作 专业官网文档 必会13条查询 <> all(): 查询所有结果 <> filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 <> get ...
- 线程属性 pthread_attr_t
参考资料: https://blog.csdn.net/hudashi/article/details/7709413 Posix线程中的线程属性pthread_attr_t主要包括scope属性.d ...
- [JAVA]JAVA章2 IOC与AOP是啥
使用Spring框架的过程中,其实就是为了使用IOC(依赖注入),和AOP(面向切面编程),这两个是Spring的灵魂. 主要用到的设计模式有工厂模式和代理模式. IOC就是典型的工厂模式,通过ses ...
- 模板学习实践三 functor
#include <iostream>#include <typeinfo> void foo(){ std::cout << "foo() called ...
- jenkins可选插件为空的解决方式
我是安装的jenkins,文件名字是这个jenkins.msi,点finish安装完成,然后浏览器就自动打开了jenkins页面,输入密码后,便进入了如下页面 之后我是选择的跳过插件安装,在可选插件里 ...