zoukankan      html  css  js  c++  java
  • 省市区联动小功能

    通过读取XML文档数据,实现省市区联动,主要方便个人快速使用,仅做参考~

    添加三个ComboBox:cbx_Province、cbx_City、cbx_CityArea,代码如下:

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Xml;
    
    namespace ApplicationOne
    {
        public partial class Form1 : Form
        {
            XmlDocument doc = new XmlDocument();
            List<string> ListString = new List<string>();
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                try
                {
                    doc.Load("http://files.cnblogs.com/Interkey/%E7%9C%81%E5%B8%82%E5%8C%BA.xml");
                    XmlNode provinces = doc.SelectSingleNode("/ProvinceCity");
    
                    //本文地址:http://www.cnblogs.com/Interkey/p/3512910.html
                    cbx_Province.Items.Clear();
                    foreach (XmlNode province in provinces.ChildNodes)
                    {
                        cbx_Province.Items.Add(province.Name);
                    }
                    if (cbx_Province.Items.Count >= 0)
                    {
                        cbx_Province.SelectedIndex = 0;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
            private void cbx_Province_SelectedIndexChanged(object sender, EventArgs e)
            {
                this.cbx_City.Items.Clear();
                string xpath = string.Format("/ProvinceCity/{0}/City", cbx_Province.SelectedItem.ToString());
                XmlNodeList cities = doc.SelectNodes(xpath);
                //本文地址:http://www.cnblogs.com/Interkey/p/3512910.html
                foreach (XmlNode city in cities)
                {
                    cbx_City.Items.Add(city.Attributes["Name"].Value);
                }
                if (cbx_City.Items.Count >= 0)
                {
                    cbx_City.SelectedIndex = 0;
                }
            }
    
            private void cbx_City_SelectedIndexChanged(object sender, EventArgs e)
            {
                cbx_CityArea.Items.Clear();
                string xpath = string.Format("/ProvinceCity/{0}/City[@Name='{1}']/CityArea",
                    cbx_Province.SelectedItem.ToString(), cbx_City.SelectedItem.ToString());
                XmlNodeList CityAreas = doc.SelectNodes(xpath);
                //本文地址: http://www.cnblogs.com/Interkey/p/3512910.html
                foreach (XmlNode area in CityAreas)
                {
                    cbx_CityArea.Items.Add(area.Attributes["Name"].Value);
                }
    
                if (cbx_CityArea.Items.Count >= 0)
                {
                    cbx_CityArea.SelectedIndex = 1;
                }
            }
        }
    }

     效果图:

    效果图

    XML地址:http://files.cnblogs.com/Interkey/%E7%9C%81%E5%B8%82%E5%8C%BA.xml

    本文参考了以下内容:
    省市区三联动的winform程序

  • 相关阅读:
    js冒泡排序
    HTML5 canvas 计时器
    centos 6.4安装杀毒软件clamAV 0.98[转]
    PHP大文件下载
    如何在 Eclipse 中使用插件构建 PHP 开发环境[转]
    CentOS 单用户模式:修改Root密码和grub加密[转]
    CentOS 6.0 VNC远程桌面配置[转]
    gprof使用介绍 (gcc -pg) [转]
    VMware NAT端口映射外网访问虚拟机linux
    shell判断文件是否存在[转]
  • 原文地址:https://www.cnblogs.com/Interkey/p/3512910.html
Copyright © 2011-2022 走看看