Like many people, I become annoyed when I see a Windows Forms control for a date, time or month that has not been filled in with a default value. The value is typically for the current period. In C#, it is derived from “DateTime.Now”. Once this is retrieved into a “DateTime” object, you may extract data such as day, week, day of week, day of month, month and so on.

In the C# coding example below, I use a code snippet from a Windows Forms I call “FormPickAMonth” to illustrate how the current month is set and then highlight it in a combo box control. If you are already versed in C# coding, then you can see this, it is rather straight forward:

  1. // this is a very simple way to first populate a combo
  2. // box control with the 12 months of the year. it
  3. // then determines the current month from the system
  4. // clock so it will be highlighted in the combo box control.
  5. // it’s a small courtesy people expect when they need to
  6. // select a month from a winform control, which is typically
  7. // the current one.
  8. public FormPickAMonth()
  9. {
  10. InitializeComponent();
  11. // clear the combo box control denoted by “comboBox”
  12. // and then add all 12 months to the control.
  13. this.comboBox.Items.Clear();
  14. this.comboBox.Items.Add("January");
  15. this.comboBox.Items.Add("February");
  16. this.comboBox.Items.Add("March");
  17. this.comboBox.Items.Add("April");
  18. this.comboBox.Items.Add("May");
  19. this.comboBox.Items.Add("June");
  20. this.comboBox.Items.Add("July");
  21. this.comboBox.Items.Add("August");
  22. this.comboBox.Items.Add("September");
  23. this.comboBox.Items.Add("October");
  24. this.comboBox.Items.Add("November");
  25. this.comboBox.Items.Add("December");
  26. // instantiate a DateTime object called “date1”
  27. // to grab the current date from the system clock.
  28. DateTime date1 = DateTime.Now;
  29. // grab the month component from the “date1” object
  30. // and assign it to the “monthvar” variable.
  31. monthvar = date1.Month;
  32. // compare the number representing the current
  33. // month (1 through 12) to the numerical value
  34. // contained in the “monthvar” variable. upon a
  35. // successful comparison, assign the verbal
  36. // description of the month to the combo box
  37. // control denoted by “comboBox”. this assigned
  38. // verbal month description will be selected and
  39. // highlighted in the combo box as the winform
  40. // loads.
  41. if (monthvar == 1)
  42. {
  43. this.comboBox.Text = "January";
  44. }
  45. if (monthvar == 2)
  46. {
  47. this.comboBox.Text = "February";
  48. }
  49. if (monthvar == 3)
  50. {
  51. this.comboBox.Text = "March";
  52. }
  53. if (monthvar == 4)
  54. {
  55. this.comboBox.Text = "April";
  56. }
  57. if (monthvar == 5)
  58. {
  59. this.comboBox.Text = "May";
  60. }
  61. if (monthvar == 6)
  62. {
  63. this.comboBox.Text = "June";
  64. }
  65. if (monthvar == 7)
  66. {
  67. this.comboBox.Text = "July";
  68. }
  69. if (monthvar == 8)
  70. {
  71. this.comboBox.Text = "August";
  72. }
  73. if (monthvar == 9)
  74. {
  75. this.comboBox.Text = "September";
  76. }
  77. if (monthvar == 10)
  78. {
  79. this.comboBox.Text = "October";
  80. }
  81. if (monthvar == 11)
  82. {
  83. this.comboBox.Text = "November";
  84. }
  85. if (monthvar == 12)
  86. {
  87. this.comboBox.Text = "December";
  88. }
  89. }
Today, it's a programming courtesy to select and highlight the current month in a combo box filled with all 12 months. Not only is it easier for the user, it also looks more professional. In addition to my software design services, please visit my website to learn more about my computer repair services.