根据下来列表来动态显示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 = 0; //默认选中第一个 } }
其次给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 = -1; } } }
获取选中下拉的问本值:
comboBox1.GetItemText(comboBox1.Items[comboBox1.SelectedIndex]);
实现效果如下: