三级行政区域联动,可删除或添加县级
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.Xml;
using System.IO;
using OperateIniFile;
using System.Runtime.InteropServices;
using System.Collections;
using System.Collections.Specialized;
namespace MyTest
{
public partial class 行政区三级联动 : Form
{
[DllImport("user32")]
private static extern int GetComboBoxInfo(IntPtr hwnd, out COMBOBOXINFO comboInfo);
struct RECT
{
public int left, top, right, bottom;
};
struct COMBOBOXINFO
{
public int cbSize;
public RECT rcItem;
public RECT rcButton;
public int stateButton;
public IntPtr hwndCombo;
public IntPtr hwndItem;
public IntPtr hwndList;
};
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 string DistrictName;
public string projectname;
public bool firstStart = true;
public 行政区三级联动()
{
InitializeComponent();
comboBox_xian.HandleCreated += (s, e) =>
{
COMBOBOXINFO combo = new COMBOBOXINFO();
combo.cbSize = Marshal.SizeOf(combo);
GetComboBoxInfo(comboBox_xian.Handle, out combo);
hwnd = combo.hwndList;
init = false;
};
//获取配置文件存储的行政区域信息
//IniFiles Setting = new IniFiles(System.Windows.Forms.Application.StartupPath + @"\Setting.ini");
//ProvinceName = Setting.ReadString("三级行政区", "省", "");
//String[] substr = ProvinceName.Split('\0');
//ProvinceName = substr[0];
//CityName = Setting.ReadString("三级行政区", "市", "");
//substr = CityName.Split('\0');
//CityName = substr[0];
//DistrictName = Setting.ReadString("三级行政区", "县", "");
//substr = DistrictName.Split('\0');
//DistrictName = substr[0];
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);
if (firstStart && ProvinceName == strProvinceName)
{
comboBox_sheng.SelectedItem = comboBox_sheng.Items[comboBox_sheng.Items.Count-1];
}
}
}
bool init;
IntPtr hwnd;
NativeCombo nativeCombo = new NativeCombo();
Dictionary<int, Rectangle> dict = new Dictionary<int, Rectangle>();
public class NativeCombo : NativeWindow
{
//this is custom MouseDown event to hook into later
public event MouseEventHandler MouseDown;
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x201)//WM_LBUTTONDOWN = 0x201
{
int x = m.LParam.ToInt32() & 0x00ff;
int y = m.LParam.ToInt32() >> 16;
if (MouseDown != null) MouseDown(null, new MouseEventArgs(MouseButtons.Left, 1, x, y, 0));
}
base.WndProc(ref m);
}
}
private void comboBox_xian_DropDown(object sender, EventArgs e)
{
comboBox_xian.Text = "";
if (!init)
{
//Register the MouseDown event handler <--- THIS is WHAT you want.
nativeCombo.MouseDown += 行政区三级联动_MouseDown;
nativeCombo.AssignHandle(hwnd);
init = true;
}
}
private void comboBox_xian_DrawItem(object sender, DrawItemEventArgs e)
{
Rectangle rect = new Rectangle(e.Bounds.Right - 20, e.Bounds.Top, e.Bounds.Height, e.Bounds.Height);
dict[e.Index] = rect;
Image myImage = Image.FromFile(System.Windows.Forms.Application.StartupPath + @"\XX.ico");
e.Graphics.DrawImage(myImage, rect);
Rectangle textRect = new Rectangle(0, e.Bounds.Top, e.Bounds.Width - e.Bounds.Height, e.Bounds.Height);
string itemText = comboBox_xian.Items[e.Index].ToString();
StringFormat strFormat = new StringFormat();
strFormat.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(itemText, new Font("宋体", 9), Brushes.Black, textRect, strFormat);
}
private void 行政区三级联动_MouseDown(object sender, MouseEventArgs e)
{
foreach (var kv in dict)
{
if (kv.Value.Contains(e.Location))
{
String txtname = comboBox_xian.Items[kv.Key].ToString();
DialogResult dr = MessageBox.Show("确定删除 " + txtname + " 吗?", "提示", MessageBoxButtons.OKCancel);
if (dr == DialogResult.OK)
{
foreach (XmlNode Districtnode in DistrictNodes)
{
if (((XmlElement)Districtnode).GetAttribute("DistrictName").Equals(txtname)
&& ((XmlElement)Districtnode).GetAttribute("CID").Equals(CityID))
{
DistrictrootElem.RemoveChild(((XmlElement)Districtnode));
break;
}
}
Districtdoc.Save(System.Windows.Forms.Application.StartupPath + "/RegionFile/Districts.xml");
DistrictNodes = DistrictrootElem.GetElementsByTagName("District"); //获取Cities子节点集合
comboBox_xian.Items.Remove(comboBox_xian.Items[kv.Key]);
}
}
}
}
private void comboBox_sheng_SelectedIndexChanged(object sender, EventArgs e)
{
ProvinceName = comboBox_sheng.SelectedItem.ToString();
//if (!firstStart)
//{
comboBox_xian.Items.Clear();
comboBox_xian.Text = "";
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);
if (firstStart && CityName == strCityName)
{
comboBox_shi.SelectedItem = comboBox_shi.Items[comboBox_shi.Items.Count-1];
}
}
}
}
private void comboBox_shi_SelectedIndexChanged(object sender, EventArgs e)
{
CityName = comboBox_shi.SelectedItem.ToString();
comboBox_xian.Items.Clear();
comboBox_xian.Text = "";
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);
if (firstStart && DistrictName == strDistrictName)
{
comboBox_xian.SelectedItem = comboBox_xian.Items[comboBox_xian.Items.Count - 1];
firstStart = false;
}
}
}
}
private void comboBox_xian_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button_OK_Click(object sender, EventArgs e)
{
bool flag = false;
foreach (Object Item in comboBox_xian.Items)
{
if (Item.ToString() == comboBox_xian.Text)
{
flag = true;
break;
}
}
if (!flag)
{
String DistrictID = (DistrictNodes.Count+1).ToString();
String DistrictName = comboBox_xian.Text;
String DistrictCID = CityID;
XmlElement curDistrictrootElem = Districtdoc.CreateElement("District");//创建一个<book>节点
curDistrictrootElem.SetAttribute("ID", DistrictID);//设置该节点的genre属性
curDistrictrootElem.SetAttribute("DistrictName", DistrictName);//设置该节点的ISBN属性
curDistrictrootElem.SetAttribute("CID", DistrictCID);//设置该节点的ISBN属性
curDistrictrootElem.InnerText = DistrictName;
DistrictrootElem.AppendChild(curDistrictrootElem);//把curDistrictrootElem添加到<Districts>根节点中
Districtdoc.Save(System.Windows.Forms.Application.StartupPath + "/RegionFile/Districts.xml");
DistrictNodes = DistrictrootElem.GetElementsByTagName("District"); //获取Cities子节点集合
comboBox_xian.Items.Add(DistrictName);
}
}
private void 行政区三级联动_FormClosed(object sender, FormClosedEventArgs e)
{
//将行政区域信息存入配置文件
//if (comboBox_xian.Text != "")
//{
// IniFiles Setting = new IniFiles(System.Windows.Forms.Application.StartupPath + @"\Setting.ini");
// Setting.WriteString("三级行政区", "省", comboBox_sheng.Text);
// Setting.WriteString("三级行政区", "市", comboBox_shi.Text);
// Setting.WriteString("三级行政区", "县", comboBox_xian.Text);
//}
}
}
}