给大家分享了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
沃梦达教程
本文标题为:C#中comboBox实现三级联动


基础教程推荐
猜你喜欢
- C++使用easyX库实现三星环绕效果流程详解 2023-06-26
- C利用语言实现数据结构之队列 2022-11-22
- C语言 structural body结构体详解用法 2022-12-06
- 详解c# Emit技术 2023-03-25
- C/C++编程中const的使用详解 2023-03-26
- C++详细实现完整图书管理功能 2023-04-04
- 如何C++使用模板特化功能 2023-03-05
- C语言基础全局变量与局部变量教程详解 2022-12-31
- 一文带你了解C++中的字符替换方法 2023-07-20
- C++中的atoi 函数简介 2023-01-05