combox下拉框控件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace combox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //通过下拉框添加数据
            comboBox2.Items.Add("张三");
            comboBox2.Items.Add("李四");
            comboBox2.Items.Add("王五");

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //清除成员
            comboBox2.Items.Clear();
        }
    }
}
View Code

DropDownStyle:控制下拉框的外观

一般给combox控件起名字时候,用cbo+****

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 日期选择器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }       

        private void Form1_Load(object sender, EventArgs e)
        {
            //在程序加载的时候 将年份添加到下拉框当中
            //因为年份的加载到我们现在的时候,所以要先获得当前的年份
            int year = DateTime.Now.Year;
            for (int i = 1949; i <= year; i++)
            {
                cboYear.Items.Add(i + "");
            }
            
        }
        /// <summary>
        /// 当年份发生该改变的时候 加载月份
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cboYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            cboMonth.Items.Clear();//在每次点击选择年份的时候,应该把上一次的月份清空,不然会出现重复现象
            //在点击年份下拉框选好之后,把月份加入
            for (int i = 1; i <=12; i++)
            {
                cboMonth.Items.Add(i + "");
            }
        }
        /// <summary>
        /// 当月份发生改变的时候加载天数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cboMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            //在点击月份之前,清空天数
            cboDay.Items.Clear();
            //获取年份跟月份,以此来判断天数是多少
            int day = 0;
            string strYear = cboYear.SelectedItem.ToString().Split(new char[] { '' }, StringSplitOptions.RemoveEmptyEntries)[0];
            string strMonth = cboMonth.SelectedItem.ToString().Split(new char[] { '' }, StringSplitOptions.RemoveEmptyEntries)[0];
            int year = Convert.ToInt32(strYear);
            int month = Convert.ToInt32(strMonth);
            switch(month)
            {
                case 1:
                    
                case 3:
                    
                case 5:
                    
                case 7:
                    
                case 8:
                    
                case 10:
                case 12:day = 31;
                    break;
                case 2:
                    if((year%400==0)||(year%4==0&&year%100!=0))
                    {
                        day = 29;
                    }
                    else
                    {
                        day = 28;
                    }
                    break;
                default:day = 30;
                    break;
            }
            for (int i = 1; i <= day; i++)
            {
                cboDay.Items.Add(i + "");
            }


        }
    }
}
View Code

 

posted @ 2021-08-21 17:34  静态类  阅读(217)  评论(0编辑  收藏  举报