ComboBox的使用

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 Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            //通过设置DropDownList这个枚举类型来禁止用户向ComboBox输入
            cboMonth.DropDownStyle = ComboBoxStyle.DropDownList;

            for(int i = 1; i <= 12; i++)
            {
                cboMonth.Items.Add(i.ToString());

            }
            cboMonth.SelectedIndex = 0;
        }

        private void cboMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            int maxDay  = 0;
            
            switch(Convert.ToInt32(cboMonth.Text))
            {
                case 1:
                case 3:
                case 5:
                case 7:case 8:case 10: case 12:
                    maxDay = 31;
                    break;
                case 2:
                    maxDay = 28;
                    break;
                default:
                    maxDay = 30;
                    break;
            }

            cboDay.Items.Clear();
            for( int i = 1; i <= maxDay; i++)
            {
                cboDay.Items.Add(i.ToString());
            }

            cboDay.SelectedIndex = 0;

             
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str = string.Format("您选中了{0}月{1}日", cboMonth.Items[cboMonth.SelectedIndex], cboDay.Text);
            MessageBox.Show(str);
        }
    }
}

  

 

posted @ 2017-08-01 14:32  mCat  Views(318)  Comments(0Edit  收藏  举报