首页 > 代码库 > winform 实现选择城市列表

winform 实现选择城市列表

先上图


 #region 选择城市
        /// <summary>
        /// 点击字母事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void item_Click(object sender, EventArgs e)
        {
            LinkLabel lbl = sender as LinkLabel;
            lbl.Font = new Font(new FontFamily("宋体"), 10, FontStyle.Bold);
            foreach (Control item in pan_CityTitle.Controls)
            {
                if (item is LinkLabel && item != lbl)
                    item.Font = new Font(new FontFamily("宋体"), 10, FontStyle.Regular);
            }
            flowLayoutPanel.Controls.Clear();
            CreatCheckCityControl(lbl.Tag != null ? lbl.Tag.ToString() : string.Empty);
        }
       
        /// <summary>
        /// 绘制城市控件
        /// </summary>
        private void CreatCheckCityControl(string lettey)
        {
            if (lettey == string.Empty)
            {
                List<FlyCity> list = dicList["RM"];
                CreatControl(list);
            }
            else
            {
                for (int i = 0; i < lettey.Length; i++)
                {
                    string temp = lettey.Substring(i, 1);
                    if (!dicList.Keys.Contains(temp))
                    {
                        continue;
                    }
                    List<FlyCity> list = dicList[temp];
                    CreatControl(list);
                }
            }
        }
        private void CreatControl(List<FlyCity> list)
        {
            foreach (FlyCity fly in list)
            {
                Button button = new Button()
                {
                    Text = fly.CityName,
                    Tag = fly,
                    Width = 76,
                    Height = 25,
                    ForeColor = Color.FromArgb(89, 89, 89),
                    FlatStyle = FlatStyle.Flat,
                };
                button.FlatAppearance.BorderColor = Color.White;
                button.Click += new EventHandler(button_Click);
                button.MouseHover += new EventHandler(button_MouseHover);
                button.MouseLeave += new EventHandler(button_MouseLeave);
                flowLayoutPanel.Controls.Add(button);
            }
        }
        /// <summary>
        /// 隐藏控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void FlyReserveForm_MouseUp(object sender, MouseEventArgs e)
        {
            if (!pan_City.Capture)
            {
                this.pan_City.Visible = false;
            }
            if (!calendar.Capture)
            {
                this.calendar.Hide();
            }
        }
        /// <summary>
        /// 点击选择目的地
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnToCity_Click(object sender, EventArgs e)
        {
            sType = 1;
            pan_City.Visible = true;
            pan_City.Location = new Point(446, 100);
        }
        /// <summary>
        /// 点击选择出发城市
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnFromCity_Click(object sender, EventArgs e)
        {
            sType = 0;
            pan_City.Visible = true;
            pan_City.Location = new Point(133, 100);
        }
        /// <summary>
        /// 从xml获取城市列表
        /// </summary>
        private void LoadCityArray()
        {
            List<FlyCity> list = new List<FlyCity>();
            XmlHelper help = new XmlHelper(Environment.CurrentDirectory + "\\arrays.xml");
            DataSet ds = help.GetData("resources");
            foreach (DataRow item in ds.Tables[0].Rows)
            {
                FlyCity fly = new FlyCity();
                string city = item[0].ToString().Substring(0, item[0].ToString().IndexOf(","));
                string cityCode = item[0].ToString().Substring(item[0].ToString().IndexOf(",") + 1, item[0].ToString().Length - item[0].ToString().IndexOf(",") -1);
                fly.CityName = city;
                fly.CityCode = cityCode;
                list.Add(fly);
            }
            dicList = ChineseConvert.InsertDic(list);
        }
        

        void button_Click(object sender, EventArgs e)
        {
            if ((sender as Button).Tag == null)
                return;
            FlyCity fly = (sender as Button).Tag as FlyCity;
            if (sType == 0)
            {
                lblFromCity.Text = fly.CityName;
                lblFromCity.Tag = fly;
            }
            else
            {
                lblToCity.Text = fly.CityName;
                lblToCity.Tag = fly;
            }
            pan_City.Visible = false;
        }
        
        private void Pan_TitleClick()
        {
            foreach (Control item in pan_CityTitle.Controls)
            {
                if(item is LinkLabel)
                    item.Click += new EventHandler(item_Click);
            }
        }

        
        void button_MouseLeave(object sender, EventArgs e)
        {
            (sender as Button).BackColor = Color.White;
            (sender as Button).ForeColor = Color.FromArgb(89, 89, 89);
        }

        void button_MouseHover(object sender, EventArgs e)
        {
            (sender as Button).BackColor = Color.FromArgb(41, 100, 180);
            (sender as Button).ForeColor = Color.White;
        }
        #endregion