C#窗体--combox、button

combox控件

用途:用于下拉菜单选择:日期,选项等。

生日选择器实例:

主要技术:
1. 判断瑞年
2. 判断天数
3. 添加月份、年份
**重点内容**




  //首先输入年份在1949年至今
        private void Form1_Load(object sender, EventArgs e)
        {
            int year = DateTime.Now.Year;
            for (int i = year ; i >=1949; i--)
            {
                cmbYear.Items.Add(i + "年");   
            }


        }

        //在输入年份年后添加月份
        private void cmbYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbMouth.Items.Clear(); //清空原来添加的月份
            for (int  i= 1; i<=12 ; i ++)
            {
                cmbMouth.Items.Add(i + "月");
            }


        }

        //输入月份后添加日期
        private void cmbMouth_SelectedIndexChanged(object sender, EventArgs e)
        {

           cmbDay.Items.Clear();//清空这个控件中的值

            //获得当前选中的年份:
           string strYear= cmbYear.SelectedItem.ToString().Split('年')[0];

            //获得当前选中的月份
           string strMouth = cmbMouth.SelectedItem.ToString().Replace("月", "");

           int year = Convert.ToInt32(strYear);//把年份转换为int类型
           int month = Convert.ToInt32(strMouth);//把月份转换为int类型
           int Day = 0;

            //判断瑞年为29-28天
           if (month==2)
           {
               if (year%400==0||year%4==0&&year%100!=0)
               {
                   Day = 29;
               }
               else
               {
                   Day = 28;
               }

           }
            //判断4-6-9-11月份为30天,其余为31天
           else
           {
               switch (month )
               {
                   case 4:
                   case 6:
                   case 9:
                   case 11:
                       Day = 30;
                       break;
                   default:
                       Day = 31;
                       break;
               }
           }

           for (int i = 1; i <=Day ; i++)
           {
               cmbDay.Items.Add(i);
           }


        }

Button

用处:确定按钮,命令按钮,用户点击后事件。

实例:弹出事件:
这里写图片描述

 private void button1_Click(object sender, EventArgs e)
        {
           // MessageBox.Show("哈哈,我 厉害了"); //只显示文本

           // MessageBox.Show("哈哈,我有厉害","系统提示"); //显示文本,标题

            //MessageBox.Show("哈哈,我有厉害", "系统提示",MessageBoxButtons.AbortRetryIgnore ); //显示文本、标题、几个选择按钮

            //MessageBox.Show("哈哈,我有厉害", "系统提示",MessageBoxButtons.AbortRetryIgnore,MessageBoxIcon.Asterisk );//文本、标题、按钮、文本中有图标并且有声音


            MessageBox.Show("哈哈,我有厉害", "系统提示", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button1) ;//和上面一样,后面的button1表示默认选择了第一个按钮
        }

猜你喜欢

转载自blog.csdn.net/aimin_com/article/details/80676944