ComboBox控件:显示一个可编辑的文本框,其中包含一个允许值下拉的列表。

    属性:
    Items:组合框中的项。
    DropDownStyle:控制组合框的外观和功能。

    事件:
    SelectedIndexChanged:SelectedIndex属性值更改时发生。

    image.png

    1. public partial class Form1 : Form
    2. {
    3. public Form1()
    4. {
    5. InitializeComponent();
    6. }
    7. int i = 0;
    8. //点击butAdd给comboBox2增加下拉选项
    9. private void butAdd_Click(object sender, EventArgs e)
    10. {
    11. comboBox2.Items.Add(i);
    12. i++;
    13. }
    14. private void butClear_Click(object sender, EventArgs e)
    15. {
    16. comboBox2.Items.Clear();
    17. }
    18. }

    日期选择器:
    image.png

    1. public partial class Form1 : Form
    2. {
    3. public Form1()
    4. {
    5. InitializeComponent();
    6. }
    7. private void Form1_Load(object sender, EventArgs e)
    8. {
    9. //程序加载的时候,将年份添加到下拉框中
    10. //获得当前年份
    11. int year = DateTime.Now.Year;
    12. for (int i = year; i >= 1949; i--)
    13. {
    14. cboYear.Items.Add(i + "年");
    15. }
    16. }
    17. /// <summary>
    18. /// 当年份发生改变的时候,加载月份
    19. /// </summary>
    20. /// <param name="sender"></param>
    21. /// <param name="e"></param>
    22. private void cboYear_SelectedIndexChanged(object sender, EventArgs e)
    23. {
    24. //每次cboYear.ItemselectedIndex属性值更改时都会给cboMonth添加12个月
    25. //所以应该清除
    26. cboMonth.Items.Clear();
    27. for (int i = 1; i <= 12; i++)
    28. {
    29. cboMonth.Items.Add(i + "月");
    30. }
    31. }
    32. /// <summary>
    33. /// 当月份发生改变的时候,加载日期
    34. /// </summary>
    35. /// <param name="sender"></param>
    36. /// <param name="e"></param>
    37. private void cboMonth_SelectedIndexChanged(object sender, EventArgs e)
    38. {
    39. int day = 0;
    40. //每次cboMonth.ItemselectedIndex属性值更改时都会给cboDay添加日期
    41. //所以应该清除
    42. cboDay.Items.Clear();
    43. //利用.SelectedItem属性ToString方法获得cboMonth,cboYear的选定的文本 再利字符串Split方法获得数字or直接中substring方法截掉最后一位
    44. //string s = cboMonth.SelectedText;//获取不了
    45. //MessageBox.Show(s);
    46. string stryear = cboYear.SelectedItem.ToString();
    47. string strmonth = cboMonth.SelectedItem.ToString();
    48. //获得年月份的数字:
    49. //使用substring方法截掉最后一位
    50. string strMonth = strmonth.Substring(0, strmonth.Length-1);
    51. string strYear = stryear.Substring(0, stryear.Length - 1);
    52. //使用Split方法获得数字
    53. //string[] strArray = strmonth.Split(new char[] { '月' }, StringSplitOptions.RemoveEmptyEntries);
    54. //string strMonth = strArray[0];
    55. //string strMonth = strmonth.Split(new char[] { '月' }, StringSplitOptions.RemoveEmptyEntries)[0];
    56. //string strYear = stryear.Split(new char[] { '年' }, StringSplitOptions.RemoveEmptyEntries)[0];
    57. //将年月份字符串转换成int类型
    58. int year = Convert.ToInt32(strYear);
    59. int month = Convert.ToInt32(strMonth);
    60. //利用switch获得day
    61. switch (month)
    62. {
    63. case 1:
    64. case 3:
    65. case 5:
    66. case 7:
    67. case 8:
    68. case 10:
    69. case 12:day = 31;break;
    70. case 2:
    71. if ((year%400==0)||(year%4==0&&year%100!=0))
    72. {
    73. day = 29;
    74. }
    75. else
    76. {
    77. day = 28;
    78. }
    79. break;
    80. default:day = 30;
    81. break;
    82. }
    83. //给cboDay下拉框赋值
    84. for (int i = 1; i <= day; i++)
    85. {
    86. cboDay.Items.Add(i + "日");
    87. }
    88. }
    89. }