欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

省市县联动

程序员文章站 2022-04-09 09:21:30
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; ... ......
 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
    }
}