c# 實現三級聯動

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 Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using AutoBuy.Frid;
using AutoBuy.Helper;
using System.Diagnostics;
using System.IO.Ports;
using AutoBuy.language;
namespace AutoBuy.Forms
{
    public partial class FrmConfig : FrmBase
    {
        public JObject AllData;
        public JObject Citydata;
        public JObject Countydata;
        public JObject Istoreshop;
        public List<string> Shopaddrurl = new List<string>();
        public bool Readerableflage  = true;
        public FrmConfig(bool enable)
        {
            Readerableflage = enable;
            InitializeComponent();

            this.shopaddrtxt.Text = geturl;
            string jsonText ={\"北京市\":{\"北京市1\":{\"越秀區\":{\"體驗店1\":\"www.11.com\",\"體驗店2\":\"www.12.com\"},\"天河區\":{\"體驗店1\":\"www.13.com\",\"體驗店2\":\"www.14.com\"}},\"北京市2\":{\"南山區\":{\"體驗店1\":\"www.15.com\",\"體驗店2\":\"www.16.com\"},\"寶安區\":{\"體驗店1\":\"www.17.com\",\"體驗店2\":\"www.18.com\"}}}}
            this.AllData = JObject.Parse(jsonText);

            foreach (KeyValuePair<string, JToken> keyValuePair in this.AllData)
            {
                this.Province.Items.Add(keyValuePair.Key);
                this.Province.Text = this.Province.Items[0].ToString();
            }
        }

        public bool CfgRetflag = false;
        public static bool FrmSetCfg(bool enable)
        {

            FrmConfig frmConfig = new FrmConfig(enable);
            frmConfig.ShowDialog();
            return frmConfig.CfgRetflag;
        }

        private void Province_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.City.Items.Clear();
            this.City.SelectedItem = null;
            Citydata = JObject.Parse(this.AllData[(string)this.Province.SelectedItem].ToString());
            foreach (KeyValuePair<string, JToken> keyValuePair in Citydata) { this.City.Items.Add(keyValuePair.Key); }
            this.City.Text = this.City.Items[0].ToString();
        }

        private void City_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.County.Items.Clear();
            this.County.SelectedItem = null;
            Countydata = JObject.Parse(this.Citydata[(string)this.City.SelectedItem].ToString());
            foreach (KeyValuePair<string, JToken> keyValuePair in Countydata)
            {
                this.County.Items.Add(keyValuePair.Key);
            }
            this.County.Text = this.County.Items[0].ToString();

        }
        private void County_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ShopAddr.Items.Clear();                            //清空店地址
            this.ShopAddr.SelectedItem = null;
            Shopaddrurl.Clear();
            Istoreshop = JObject.Parse(this.Countydata[(string)this.County.SelectedItem].ToString());
            foreach (KeyValuePair<string, JToken> shopname in Istoreshop)
            {
                this.ShopAddr.Items.Add(shopname.Key);
            }
            this.ShopAddr.Text = this.ShopAddr.Items[0].ToString();
        }

        private void btnSetAddrOK_Click(object sender, EventArgs e)
        {
            JObject Selectedshop = JObject.Parse(Countydata[this.County.SelectedItem].ToString()); //選中的區
            foreach (KeyValuePair<string, JToken> shopname in Istoreshop)
            {
                Shopaddrurl.Add(shopname.Value.ToString());
            }
            var index = this.ShopAddr.SelectedIndex;                   //選中的店url
            var url = Shopaddrurl[this.ShopAddr.SelectedIndex];

        }




       
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章