根据下来列表来动态显示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下的子元素的更多相关文章

  1. ExtJS基础知识总结:自定义日历和ComboBox控件(二)

    概述 1.ExtJS 5不支持日期选择框中只选择年月,为了满足ExtJs5可以实现选择年月的功能,查询网上资料,整理出来了相应的处理方式,最终实现的效果如下图: 2.ExtJS 控件丰富,如果需要实现 ...

  2. 加载ComboBox控件

    /// <summary> /// 加载公司 /// </summary> /// <param name="cbbCompany">Combo ...

  3. C#中combobox 控件属性、事件、方法

    一 .combobox 属性.事件.方法公共属性 名称 说明 AccessibilityObject 获取分配给该控件的 AccessibleObject. AccessibleDefaultActi ...

  4. .Net平台Winform两个ComboBox控件绑定同一个数据源

    今天WINFROM编程遇到这么一个问题:是有关WINFORM中两个comboBox控件绑定同一个数据源的问题,在窗体的界面上有两个comboBox,我在Form1_Load中对他们做了数据绑定(具体代 ...

  5. WinForm编程时窗体设计器中ComboBox控件大小的设置

    问题描述: 在VS中的窗体设计器中拖放一个ComboBox控件后想调整控件的大小.发现在控件上用鼠标只能拖动宽度(Width)无法拖动(Height). 解决过程: 1.控件无法拖动,就在属性窗口中设 ...

  6. Winform ComboBox控件高亮显示

    //重绘下拉表单窗口,需要在窗口设计代码中加入下面这一句 this.cmdChannelName.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawF ...

  7. winform combobox控件绑定 分类: WinForm 2014-04-17 14:34 118人阅读 评论(0) 收藏

    想要达到的效果:把数据库中的一列数据绑定到combobox控件中. 数据库表:T_Task//任务表 列名:Task_Name//名称 主键:Task_ID combobox控件名称:cbName 解 ...

  8. ComboBox控件绑定数据源

    最近在研究机房收费系统的组合查询的方法时,看到了ComboBox控件可以进行数据绑定,我觉得这个功能真的很不错,可以给我省去很多的麻烦. 下面是我组合查询窗体界面 一.数据转换方法 现在我们开看一下我 ...

  9. WPF中实现多选ComboBox控件

    在WPF中实现带CheckBox的ComboBox控件,让ComboBox控件可以支持多选. 将ComboBox的ItemsSource属性Binding到一个Book的集合, public clas ...

随机推荐

  1. oday获取系统最高权限的代码

    import sys,sockettarget = sys.argv[1]shellcode = ("\x6a\x4f\x59\xd9\xee\xd9\x74\x24\xf4\x5b\x81 ...

  2. 标志寄存器在Debug中的表示

    在Debug中,标志寄存器是按照有意义的各个标志位单独表示的. 下面列出Debug对我们已知的标志位的表示.

  3. stark组件开发之列表页面应用示例

    已经解决的,自定义的扩展函数,功能.但是 不可能返回. 一个 固定的页面把!  应该是,点击那条 记录之后的编辑, 就会跳转到相应的,编辑页面.所以 这个标签的  <a href="/ ...

  4. vscode快捷键的中文版

    自己整理了一份vscode快捷键的中文版本

  5. boost asio 学习(二)了解boost::bind

    2.了解boost::bind使用boost::bind封装一个函数,考虑以下例子示例2a #include <iostream> #include <boost/bind.hpp& ...

  6. Apache ab性能测试结果分析

    Apache ab性能测试结果分析 测试场景:模拟10个用户,对某页发起总共100次请求. 测试命令: ab -n 100 -c 10 地址 测试报告: Server Software: 被测服务器软 ...

  7. kbmmw 做REST 服务签名认证的一种方式

    一般对外提供提供REST 服务,由于信息安全的问题, 都要采用签名认证,今天简单说一下在KBMMW 中如何 实现简单的签名服务? 整个签名服务,模仿阿里大鱼的认证方式,大家可以根据实际情况自己修改. ...

  8. href=#与href=javascript:void(0)的区别

    #"包含了一个位置信息,默认的锚点是#top 也就是网页的上端 而javascript:void(0)  仅仅表示一个死链接 这就是为什么有的时候页面很长浏览链接明明是#可是跳动到了页首,而 ...

  9. jq的事件对象

  10. noip第16课资料