• 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 }
    复制代码

  • 相关阅读:
    Java中的数据类型
    java中变量的分类
    软考视频总结(二)——细化学习A
    软考视频总结(一)——蜕变
    Eclipse汉化
    IIS 服务或万维网发布服务,或者依赖这两种服务的某个服务未能启动。
    jQuery--编辑表格
    MVC之查询demo
    初识MVC之建项
    附加数据库 对于服务器 XXX失败
  • 原文地址:https://www.cnblogs.com/zhulijun/p/6813871.html
Copyright © 2020-2023  润新知