效果如下:
designer.cs代码如下:
using System.Collections.Generic;
namespace 删除
{
public partial class 省市区选择
{
private Dictionary<string, List<string>> provinceCityDictionary;
private Dictionary<string,List<string>> henanCities = new Dictionary<string, List<string>>();
private void InitializeComboBoxes()
{
// 将所有省份添加到第一个ComboBox
comboBoxProvinces.DataSource = new List<string>(provinceCityDictionary.Keys);
// 订阅省份ComboBox的SelectedIndexChanged事件
comboBoxProvinces.SelectedIndexChanged += comboBoxProvinces_SelectedIndexChanged;
comboBoxCities.SelectedIndexChanged += comboBoxCities_SelectedIndexChanged;
comboBoxProvinces.SelectedIndex = -1;
comboBoxCities.SelectedIndex = -1;
comboBoxqu.SelectedIndex = -1;
}
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(省市区选择));
this.comboBoxProvinces = new System.Windows.Forms.ComboBox();
this.comboBoxCities = new System.Windows.Forms.ComboBox();
this.comboBoxqu = new System.Windows.Forms.ComboBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// comboBoxProvinces
//
this.comboBoxProvinces.DropDownStyle = System.Windows.Forms.ComboBoxStyle.Simple;
this.comboBoxProvinces.FormattingEnabled = true;
this.comboBoxProvinces.Location = new System.Drawing.Point(12, 7);
this.comboBoxProvinces.Name = "comboBoxProvinces";
this.comboBoxProvinces.Size = new System.Drawing.Size(205, 366);
this.comboBoxProvinces.TabIndex = 0;
this.comboBoxProvinces.SelectedIndexChanged += new System.EventHandler(this.comboBoxProvinces_SelectedIndexChanged);
//
// comboBoxCities
//
this.comboBoxCities.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxCities.FormattingEnabled = true;
this.comboBoxCities.Location = new System.Drawing.Point(223, 7);
this.comboBoxCities.Name = "comboBoxCities";
this.comboBoxCities.Size = new System.Drawing.Size(223, 20);
this.comboBoxCities.TabIndex = 1;
this.comboBoxCities.SelectedIndexChanged += new System.EventHandler(this.comboBoxCities_SelectedIndexChanged);
//
// comboBoxqu
//
this.comboBoxqu.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxqu.FormattingEnabled = true;