更新:把控件ID改了一下
C#窗体应用程序,省市县三级行政区ComboBox控件,联动下拉选择。
运行效果
三级行政区XML文件链接:http://pan.baidu.com/s/1kUEBdtH
参考文献:
http://www.cnblogs.com/Hisin/archive/2012/02/27/2370646.html
http://blog.csdn.net/make1828/article/details/40108177
<pre name="code" class="csharp">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Xml;
using System.Runtime.Serialization;
using System.IO;
using System.Net;
namespace MyTest
{
public partial class Form2 : Form
{
public XmlDocument Provincedoc;
public XmlDocument Citydoc;
public XmlDocument Districtdoc;
public XmlElement ProvincerootElem;
public XmlElement CityrootElem;
public XmlElement DistrictrootElem;
public XmlNodeList ProvinceNodes;
public XmlNodeList CityNodes;
public XmlNodeList DistrictNodes;
public string ProvinceName;
public string ProvinceID;
public string CityName;
public string CityID;
public Form2()
{
InitializeComponent();
Provincedoc = new XmlDocument();
Provincedoc.Load(System.Windows.Forms.Application.StartupPath + "/RegionFile/Provinces.xml"); //加载Xml文件
ProvincerootElem = Provincedoc.DocumentElement; //获取根节点
ProvinceNodes = ProvincerootElem.GetElementsByTagName("Province"); //获取Provinces子节点集合
Citydoc = new XmlDocument();
Citydoc.Load(System.Windows.Forms.Application.StartupPath + "/RegionFile/Cities.xml"); //加载Xml文件
CityrootElem = Citydoc.DocumentElement; //获取根节点
CityNodes = CityrootElem.GetElementsByTagName("City"); //获取Cities子节点集合
Districtdoc = new XmlDocument();
Districtdoc.Load(System.Windows.Forms.Application.StartupPath + "/RegionFile/Districts.xml"); //加载Xml文件
DistrictrootElem = Districtdoc.DocumentElement; //获取根节点
DistrictNodes = DistrictrootElem.GetElementsByTagName("District"); //获取Cities子节点集合
foreach (XmlNode Provincenode in ProvinceNodes)
{
string strProvinceName = ((XmlElement)Provincenode).GetAttribute("ProvinceName"); //获取name属性值
string strProvinceID = ((XmlElement)Provincenode).GetAttribute("ID");
comboBox_sheng.Items.Add(strProvinceName);
}
}
private void comboBox_sheng_SelectedIndexChanged(object sender, EventArgs e)
{
ProvinceName = comboBox_sheng.SelectedItem.ToString();
comboBox_xian.Text = "";
comboBox_shi.Text = "";
comboBox_xian.Items.Clear();
comboBox_shi.Items.Clear();
foreach (XmlNode Provincenode in ProvinceNodes)
{
if (((XmlElement)Provincenode).GetAttribute("ProvinceName").Equals(comboBox_sheng.SelectedItem.ToString()))
{
ProvinceID = ((XmlElement)Provincenode).GetAttribute("ID");
break;
}
}
foreach (XmlNode Citynode in CityNodes)
{
if (((XmlElement)Citynode).GetAttribute("PID").Equals(ProvinceID))
{
string strCityName = ((XmlElement)Citynode).GetAttribute("CityName");
comboBox_shi.Items.Add(strCityName);
}
}
comboBox_shi.SelectedItem = comboBox_shi.Items[0];
}
private void comboBox_shi_SelectedIndexChanged(object sender, EventArgs e)
{
CityName = comboBox_shi.SelectedItem.ToString();
comboBox_xian.Text = "";
comboBox_xian.Items.Clear();
foreach (XmlNode Citynode in CityNodes)
{
if (((XmlElement)Citynode).GetAttribute("CityName").Equals(CityName) && ((XmlElement)Citynode).GetAttribute("PID").Equals(ProvinceID))
{
CityID = ((XmlElement)Citynode).GetAttribute("ID");
break;
}
}
foreach (XmlNode Districtnode in DistrictNodes)
{
if (((XmlElement)Districtnode).GetAttribute("CID").Equals(CityID))
{
string strDistrictName = ((XmlElement)Districtnode).GetAttribute("DistrictName"); //获取name属性值
comboBox_xian.Items.Add(strDistrictName);
}
}
comboBox_xian.SelectedItem = comboBox_xian.Items[0];
}
private void comboBox_xian_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}