C#中comboBox实现三级联动

实现效果:

Form1.cs代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;

namespace Select
{
 public partial class Form1 : Form
 {
 public Form1()
 {
  InitializeComponent();
 }
 Hashtable province = new Hashtable();
 Hashtable city = new Hashtable();
 private void Province()
 {
  province.Add("云南省",new string[] {"昆明市","玉溪市" });
  province.Add("四川省", new string[] { "成都市", "绵阳市" });
  city.Add("昆明市",new string[] {"盘龙区","五华区" });
  city.Add("玉溪市",new string[] {"红塔区","。。。区" });
  city.Add("成都市", new string[] { "。。。区", "。。。区" });
  city.Add("绵阳市", new string[] { "...区", "...区" });
 }

 private void Form1_Load(object sender, EventArgs e)
 {
  Province();
  foreach (string str in province.Keys)
  {
  comboBox1.Items.Add(str);
  }
  foreach (string str in city.Keys)
  {
  comboBox2.Items.Add(str);
  }
  comboBox1.SelectedIndex=0;
 }

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
  string[] citys = province[comboBox1.Text] as string[];
  comboBox2.Items.Clear();
  foreach (string s in citys)
  {
  comboBox2.Items.Add(s);
  }
  comboBox2.SelectedIndex = 0;
 }
 private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
 {
  string[] citys = city[comboBox2.Text] as string[];
  comboBox3.Items.Clear();
  foreach (string str in citys)
  {
  comboBox3.Items.Add(str);
  }
  comboBox3.SelectedIndex = 0;
 }

 private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
 {

 }
 }
}

更多相关的实现方法大家可以阅读下面的相关内容,感谢大家对我们的支持。

本文转载于:https://www.idaobin.com/archives/970.html

(0)

相关推荐

  • C# ComboBox控件“设置 DataSource 属性后无法修改项集合”的完美解决方法

    由于毕业后工作没有对接到专业问题,导致四五年没有碰过Winform程序了.突然由于工作问题,为了方便自己,所以想自己写写小winform小软件,用于自己使用.在使用ComboBox控件时,遇到了重新绑定赋值出问题的情况. 错误代码如下: if (CustomerBLL.select().Rows.Count > 0) { cbTcid.Items.Clear(); cbTcid.DataSource = CustomerBLL.select(); cbTcid.ValueMember = "

  • C# ComboBox的联动操作(三层架构)

    项目需求:根据年级下拉框的变化使得科目下拉框绑定次年级下对应有的值 我们用三层架构的模式来实现 1.我们想和数据库交互,我们首先得来先解决DAL数据库交互层 01.获得年级下拉框的数据 在GradeDAL类中 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient;

  • C#(WinForm) ComboBox和ListBox添加项及设置默认选择项

    Web控件DropDownList和WinForm控件ComboBox机制不一样. ComboBox没有对应的ListItem需要自己写一个: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WinListItem { /// <summary> /// 选择项类,用于ComboBox或者ListBox添加项 /// </summary>

  • C#实现ComboBox控件显示出多个数据源属性的方法

    本文实例讲述了C#实现ComboBox控件显示出多个数据源属性的方法.分享给大家供大家参考.具体如下: public partial class Form4 : Form { private Bitmap myBitmap; public Form4() { InitializeComponent(); DataTable dt = new DataTable(); DataColumn dc1 = new DataColumn("Name", typeof(System.String

  • C#实现绑定Combobox的方法

    本文实例讲述了C#实现绑定Combobox的方法.分享给大家供大家参考.具体实现方法如下: public class StaticVariable { public Dictionary<string, string> tabTypeArray; public Dictionary<string, string> transTimeArray; public Dictionary<string, string> fileDealTypeArray; public Sta

  • C#实现ComboBox自动匹配字符

    1. 采用CustomSource当做提示集合 将下列代码添加到窗口加载函数中即可.假设unitNameList是获取的想要添加到下拉列表中的字符串列表. 复制代码 代码如下: AutoCompleteStringCollection collection = new AutoCompleteStringCollection(); // 获取单位列表 List<string> unitNameList = this.getAllUnitName(); foreach (string unitn

  • C# 重写ComboBox实现下拉任意组件的方法

    一.需求 C#种的下拉框ComboBox不支持下拉复选框列表与下拉树形列表等,系统中需要用到的地方使用了第三方组件,现在需要将第三方组件替换掉. 二.设计 基本思路:重写ComboBox,将原生的下拉部分屏蔽,使用toolStripDropDown制作下拉弹出 三.问题解决 1. 问题:toolStripDropDown中放toolStripControlHost时会有边框产生,同时CheckedListBox的duck为full时底端会有很大空白 解决: toolStripControlHos

  • c#构造ColorComboBox(颜色下拉框)

    复制代码 代码如下: class ColorComboBox : ComboBox    {        /// <summary>        /// 当前选中色        /// </summary>        public Color SelectedColor        {            get { return Color.FromName(this.Text); }        }        /// <summary>     

  • C#用ComboBox控件实现省与市的联动效果的方法

    本文实例讲述了C#用ComboBox控件实现省与市的联动效果的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: using System;  using System.Collections.Generic;  using System.ComponentModel;  using System.Data;  using System.Drawing;  using System.Linq;  using System.Text;  using System.Windows.

  • C#实现带搜索功能的ComboBox

    带搜索的ComboBox就是给ComboBox一个依赖属性的ItemSource,然后通过数据源中是否包含要查询的值,重新给ComboBox绑定数据源. public class EditComboBox : ComboBox { private bool t = true;//首次获取焦点标志位 private ObservableCollection<object> bindingList = new ObservableCollection<object>();//数据源绑定

  • C# listview添加combobox到单元格的实现代码

    实现代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplicat

随机推荐