首页 > 其他 > 详细

省市县联动

时间:2019-05-05 23:31:19      阅读:144      评论:0      收藏:0      [点我收藏+]
 1 //Program.cs
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Threading.Tasks;
 6 using System.Windows.Forms;
 7 
 8 namespace ComboxExc02
 9 {
10     static class Program
11     {
12         /// <summary>
13         /// 应用程序的主入口点。
14         /// </summary>
15         [STAThread]
16         static void Main()
17         {
18             Application.EnableVisualStyles();
19             Application.SetCompatibleTextRenderingDefault(false);
20             Application.Run(new Form1());
21         }
22     }
23 }
//Province.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ComboxExc02
{
    public class Province
    {
        private string ProvinceName;
        /// <summary>
        /// 区/县名称
        /// </summary>
        public string provinceName
        {
            get { return ProvinceName; }
            set { ProvinceName = value; }
        }

        public City[] Cities;
    }
}
//City.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ComboxExc02
{
    public class City
    {
        private string CityName;
        /// <summary>
        /// 区/县名称
        /// </summary>
        public string cityName
        {
            get { return CityName; }
            set { CityName = value; }
        }

        public District[] Districts;
    }
}
//District.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ComboxExc02
{
    public class District
    {
        private string DistName;
        /// <summary>
        /// 区/县名称
        /// </summary>
        public string distName
        {
            get { return DistName; }
            set { DistName = value; }
        }

    }
}
//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.IO;
namespace ComboxExc02
{
    public partial class Form1 : Form
    {
        public string FilePath = System.Environment.CurrentDirectory.ToString()+@"\CbxData.txt";
        string strData = string.Empty;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //读取所有文件中的文本数据
            strData = this.ReadFile(FilePath);

            //从文件数据读取省份信息并绑定控件
            List<Province> provinces = this.GetProvinceData(strData);
            this.cbxProvince.DataSource = provinces;
            this.cbxProvince.DisplayMember = "provinceName";
            //this.cbxProvince.SelectedIndex = 0;
            //cbxProvince_SelectedIndexChanged(sender, e);
        }

        #region 读取指定文件中的所有文本文字
        /// <summary>
        /// 读取指定文件中的所有文本文字
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>文件中所有的文本数据</returns>
        private string ReadFile(string filePath)
        {
            string strAllData = string.Empty;
            string[] strsAllData = File.ReadAllLines(filePath, Encoding.GetEncoding("gb2312"));//gb2312是读取为汉字格式
            foreach (string strData in strsAllData)
            {
                strAllData += strData.Trim();
            }
            return strAllData;
        }
        #endregion

        #region 由读取的文本数据解析出省份数据
        /// <summary>
        /// 由读取的文本数据解析出省份数据
        /// </summary>
        /// <param name="strAllData">已读取的文本数据</param>
        /// <returns>省份数据集合</returns>
        private List<Province> GetProvinceData(string strAllData)
        {
            List<Province> provinceList = new List<Province>();
            string[] strProvinces = strAllData.Split(;);
            foreach (string strProvince in strProvinces)
            {
                Province province = new Province();
                province.provinceName = strProvince.Split(|)[0];
                provinceList.Add(province);
            }
            return provinceList;
        }
        #endregion

        #region 根据省份数据查找出下属城市数据
        /// <summary>
        /// 根据省份数据查找出下属城市数据
        /// </summary>
        /// <param name="strAllData">已读取的文本数据</param>
        /// <returns>指定的省份带城市数据</returns>
        private Province GetCityDataByProvince(string strAllData,Province provinceData)
        {
            Province province = new Province();
            string[] strProvinces = strAllData.Split(;);
            foreach (string strProvince in strProvinces)
            {
                if(provinceData.provinceName == strProvince.Split(|)[0])
                {
                    province.provinceName = provinceData.provinceName;
                    string[] strCities = strProvince.Split(|)[1].ToString().Split(/);
                    List<City> cities = new List<City>();
                    foreach (string strCity in strCities)
                    {
                        City city = new City();
                        city.cityName = strCity.Split(:)[0];
                        cities.Add(city);
                    }
                    province.Cities = cities.ToArray();
                    break;
                }
            }
            return province;
        }
        #endregion

        #region 根据一个省份一个城市数据查找出下属地区数据
        /// <summary>
        /// 根据一个省份一个城市数据查找出下属地区数据
        /// </summary>
        /// <param name="strAllData">已读取的文本数据</param>
        /// <param name="provinceData">一个省份一个城市数据</param>
        /// <returns>指定的省份和城市带地区数据</returns>
        private Province GetDistrictDataByCityofProvince(string strAllData, Province provinceData)
        {
            Province province = new Province();
            string[] strProvinces = strAllData.Split(;);
            foreach (string strProvince in strProvinces)
            {
                if (provinceData.provinceName == strProvince.Split(|)[0])
                {
                    province.provinceName = provinceData.provinceName;
                    string[] strCities = strProvince.Split(|)[1].ToString().Split(/);
                    foreach (string strCity in strCities)
                    {
                        if (strCity.Split(:)[0]==provinceData.Cities[0].cityName)
                        {
                            province.Cities = new City[1];
                            province.Cities[0] = provinceData.Cities[0];
                            List<District> districts = new List<District>();
                            string[] strDistricts= strCity.Split(:)[1].ToString().Split(,);
                            foreach(string strDistrict in strDistricts)
                            {
                                District district = new District();
                                district.distName = strDistrict;
                                districts.Add(district);
                            }
                            province.Cities[0].Districts = districts.ToArray();
                            break;
                        }
                    }
                }
            }
            return province;
        }
        #endregion

        #region 选定省份后,显示对应城市数据
        private void cbxProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            Province province = new Province();
            province.provinceName = ((Province)this.cbxProvince.SelectedValue).provinceName.ToString();

            //获取所有指定省份的城市数据并绑定到控件
            province = this.GetCityDataByProvince(strData, province);
            this.cbxCity.DataSource = province.Cities;
            this.cbxCity.DisplayMember = "cityName";
        }
        #endregion

        #region 选定省份和城市后,绑定显示下属的区县数据
        private void cbxCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            Province province = new Province();
            province.provinceName = ((Province)this.cbxProvince.SelectedValue).provinceName.ToString();
            City city = new City();
            city.cityName = ((City)this.cbxCity.SelectedValue).cityName.ToString();
            province.Cities = new City[1];
            province.Cities[0] = city;

            //获取所有指定省份和城市后,查询下属区县数据并绑定到控件
            province = this.GetDistrictDataByCityofProvince(strData, province);
            this.cbxArea.DataSource = province.Cities[0].Districts;
            this.cbxArea.DisplayMember = "distName";
        }
        #endregion
    }
}

 

省市县联动

原文:https://www.cnblogs.com/palebluestarrysky/p/10816777.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!