码迷,mamicode.com
首页 > Windows程序 > 详细

winform用户控件、timer控件、三级联动

时间:2017-05-22 13:26:17      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:pac   ram   下拉框   gif   16px   ade   cat   and   操作   

用户控件:

相当于自定义的一个panel 里面可以放各种其他控件,并可以在后台一下调用整个此自定义控件。

使用方法:在项目上右键、添加、用户控件,之后用户控件的编辑与普通容器控件类似。

如果要在后台往窗体中添加,将其实例化,然后添加到想要添加的容器的Control集合中。

timer控件:

组件中的最后一个控件,功能是可以根据用户自定义的时间间隔来触发时间,不会印象窗体本身的其他事件进行。

属性:

  Enable  设置控件是否启用

  Interval  设置事件的频率,以毫秒为单位

事件只有一个:Tick事件

例:使用timer控件获取当前时间并即时变动

private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = DateTime.Now.ToString("yyyy年MM月dd日hh时mm分ss秒");
        }

控件联动:

类似地区选择,当选的某个省份,后面的下拉框相对变成对应省份的区县

技术分享
技术分享
public Form2()
        {
            InitializeComponent();

            //绑定省
            comboBox1.DataSource = new ChinaData().Select("0001");
            comboBox1.DisplayMember = "AreaName";
            comboBox1.ValueMember = "AreaCode";

            //绑定市
            comboBox2.DataSource = new ChinaData().Select(comboBox1.SelectedValue.ToString());
            comboBox2.DisplayMember = "AreaName";
            comboBox2.ValueMember = "AreaCode";

            //绑定区县
            comboBox3.DataSource = new ChinaData().Select(comboBox2.SelectedValue.ToString());
            comboBox3.DisplayMember = "AreaName";
            comboBox3.ValueMember = "AreaCode";

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //绑定市
            comboBox2.DataSource = new ChinaData().Select(comboBox1.SelectedValue.ToString());
            comboBox2.DisplayMember = "AreaName";
            comboBox2.ValueMember = "AreaCode";
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //绑定区县
            comboBox3.DataSource = new ChinaData().Select(comboBox2.SelectedValue.ToString());
            comboBox3.DisplayMember = "AreaName";
            comboBox3.ValueMember = "AreaCode";
        }
技术分享

 

实体类

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace WindowsFormsApplication2
 7 {
 8     public class China
 9     {
10         private string _AreaCode;
11 
12         public string AreaCode
13         {
14             get { return _AreaCode; }
15             set { _AreaCode = value; }
16         }
17         private string _AreaName;
18 
19         public string AreaName
20         {
21             get { return _AreaName; }
22             set { _AreaName = value; }
23         }
24         private string _ParentAreaCode;
25 
26         public string ParentAreaCode
27         {
28             get { return _ParentAreaCode; }
29             set { _ParentAreaCode = value; }
30         }
31 
32     }
33 }
技术分享

 

数据操作类:

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Data.SqlClient;
 4 using System.Linq;
 5 using System.Text;
 6 
 7 namespace WindowsFormsApplication2
 8 {
 9     public class ChinaData
10     {
11         SqlConnection conn = null;
12         SqlCommand cmd = null;
13 
14         public ChinaData()
15         {
16             conn = new SqlConnection("server=.;database=Data0216;user=sa;pwd=123");
17             cmd = conn.CreateCommand();
18         }
19 
20         //通过一个父级编号,查询该父级编号对应的地区,放到一个集合中去。
21         public List<China> Select(string pcode)
22         {
23             List<China> clist = new List<China>();
24             cmd.CommandText = "select *from ChinaStates where ParentAreaCode = @a";
25             cmd.Parameters.Clear();
26             cmd.Parameters.AddWithValue("@a", pcode);
27             conn.Open();
28             SqlDataReader dr = cmd.ExecuteReader();
29             while (dr.Read())
30             {
31                 China c = new China();
32                 c.AreaCode = dr[0].ToString();
33                 c.AreaName = dr[1].ToString();
34                 c.ParentAreaCode = dr[2].ToString();
35 
36                 clist.Add(c);
37             }
38             conn.Close();
39             return clist;
40         }
41     }
42 }
技术分享

技术分享

winform用户控件、timer控件、三级联动

标签:pac   ram   下拉框   gif   16px   ade   cat   and   操作   

原文地址:http://www.cnblogs.com/zhulijun/p/6813871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
分享档案
周排行
mamicode.com排行更多图片
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!