码迷,mamicode.com
首页 > 其他好文 > 详细

timer控件、三级联动

时间:2016-12-12 22:48:45      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:random   计时   base   namespace   space   close   pac   window   end   

timer控件:

实现时间日期自增长:

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;

namespace timer控件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

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

抽奖设置,点击开始开始抽奖,开始按钮变为结束,点击结束,抽奖结束,结束按钮变为开始:

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;

namespace timer控件
{
    public partial class Form2 : Form
    {
        List<long> nu = new List<long>();
        Random r = new Random();

        public Form2()
        {
            InitializeComponent();
            nu.Add(13969374807);
            nu.Add(13864311111);
            nu.Add(13864322222);
            nu.Add(13864333333);
            nu.Add(13864344444);

        }
        //抽奖
        bool s = false;
        private void button1_Click(object sender, EventArgs e)
        {
            if (s)//结束抽奖
            {
                button1.Text = "开始";
                s = false;
                timer1.Enabled = false;
            }
            else//开始抽奖
            {
                button1.Text = "停止";
                s = true;
                timer1.Enabled = true;            
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label2.Text= nu[r.Next(0,nu.Count)].ToString();
        }
    }
}

作弊:
在if结束抽奖中加一句代码:

label2.text="13969374807";

 

三级联动:

创建实体类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 三级联动.App_Code
{
    public class china
    {
        public string areacode { get; set; }
        public string areaname { get; set; }
        public string parentareacode { get; set; }

    }
}

创建数据访问类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace 三级联动.App_Code
{
    public class chinadata
    {
        SqlConnection conn = null;
        SqlCommand cmd = null;

        public chinadata()
        {
            conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");
            cmd = conn.CreateCommand();       
        }

        public List<china> select(string pcode)
        {
            List<china> clist = new List<china>();
            cmd.CommandText = "select*from ChinaStates where ParentAreaCode=@a";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@a",pcode);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    china c = new china();
                    c.areacode = dr[0].ToString();
                    c.areaname = dr[1].ToString();
                    c.parentareacode = dr[2].ToString();
                    clist.Add(c);
                }            
            }
            conn.Close();
            return clist;
        }



    }
}

form1:使用selectedindexchanged事件,引用命名空间

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 三级联动.App_Code;

namespace 三级联动
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            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"; } } }

技术分享

 

timer控件、三级联动

标签:random   计时   base   namespace   space   close   pac   window   end   

原文地址:http://www.cnblogs.com/wy1992/p/6165847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!