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 ...
随机推荐
- echo不换行的实现
1. echo的参数中, -e表示开启转义, /c表示不换行: echo -e "please input a value:/c" 2. -n不换行: echo -n " ...
- 图片转base64上传,视频同理。
body: <input type="file" id="img" type="file" onchange="up()&q ...
- 无监督学习算法-Apriori进行关联分析
关联分析 是无监督讯息算法中的一种,Apriori主要用来做_关联分析_,_关联分析_可以有两种形式:频繁项集或者关联规则.举个例子:交易订单 序号 商品名称 1 书籍,电脑 2 杯子,手机,手机壳, ...
- IntelliJ IDEA常用快捷键(Mac)
Mac 键盘符号和修饰键说明 ⌘ ——> Command ⇧ ——> Shift ⌥ ——> Option ⌃ ——> Control ↩︎ ——> Return/Ent ...
- adc指令
adc是带进位加法指令,它利用了CF位上记录的进位值. 指令格式: adc 操作对象1,操作对象2 功能:操作对象1 = 操作对象1 + 操作对象2 + CF 例如指令 adc ax,bx实现的功能 ...
- ipcam
ipcam也叫ip network camera,就是基于internet protocol的网络摄像机,同普通摄像头或者网眼的主要区别是ipcam实际上是一台视频服务器和摄像头的集成.ipcam只要 ...
- Alpha 冲刺 (6/10)
队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 测试服务器并行能力 学习MSI.CUDA ...
- 差分模版题(需理解才明白)AT2442 フェーン現象 (Foehn Phenomena)
https://www.luogu.org/problemnew/show/AT2442 #include <bits/stdc++.h> #define read read() #def ...
- 关于webconfig的记录恢复本
<?xml version="1.0"?> <!--注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置.可以使用 Visual S ...
- 什么是servlet?
一.servlet是什么? 是用java编写的应用在服务端的程序,具有独立于平台和协议的特性,主要功能在于交互式地浏览和修改数据,生成动态Web内容,例如页面等等.从实现上讲,Servlet可以响应任 ...