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

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

用户控件:

相当于自定义的一个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 }
复制代码

 
 
 

关闭提示 关闭

 

确 认 取 消
 

转载于:https://www.cnblogs.com/zhulijun/p/6813871.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值