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:
- // this is a very simple way to first populate a combo
- // box control with the 12 months of the year. it
- // then determines the current month from the system
- // clock so it will be highlighted in the combo box control.
- // it’s a small courtesy people expect when they need to
- // select a month from a winform control, which is typically
- // the current one.
- public FormPickAMonth()
- {
- InitializeComponent();
- // clear the combo box control denoted by “comboBox”
- // and then add all 12 months to the control.
- this.comboBox.Items.Clear();
- this.comboBox.Items.Add("January");
- this.comboBox.Items.Add("February");
- this.comboBox.Items.Add("March");
- this.comboBox.Items.Add("April");
- this.comboBox.Items.Add("May");
- this.comboBox.Items.Add("June");
- this.comboBox.Items.Add("July");
- this.comboBox.Items.Add("August");
- this.comboBox.Items.Add("September");
- this.comboBox.Items.Add("October");
- this.comboBox.Items.Add("November");
- this.comboBox.Items.Add("December");
- // instantiate a DateTime object called “date1”
- // to grab the current date from the system clock.
- DateTime date1 = DateTime.Now;
- // grab the month component from the “date1” object
- // and assign it to the “monthvar” variable.
- monthvar = date1.Month;
- // compare the number representing the current
- // month (1 through 12) to the numerical value
- // contained in the “monthvar” variable. upon a
- // successful comparison, assign the verbal
- // description of the month to the combo box
- // control denoted by “comboBox”. this assigned
- // verbal month description will be selected and
- // highlighted in the combo box as the winform
- // loads.
- if (monthvar == 1)
- {
- this.comboBox.Text = "January";
- }
- if (monthvar == 2)
- {
- this.comboBox.Text = "February";
- }
- if (monthvar == 3)
- {
- this.comboBox.Text = "March";
- }
- if (monthvar == 4)
- {
- this.comboBox.Text = "April";
- }
- if (monthvar == 5)
- {
- this.comboBox.Text = "May";
- }
- if (monthvar == 6)
- {
- this.comboBox.Text = "June";
- }
- if (monthvar == 7)
- {
- this.comboBox.Text = "July";
- }
- if (monthvar == 8)
- {
- this.comboBox.Text = "August";
- }
- if (monthvar == 9)
- {
- this.comboBox.Text = "September";
- }
- if (monthvar == 10)
- {
- this.comboBox.Text = "October";
- }
- if (monthvar == 11)
- {
- this.comboBox.Text = "November";
- }
- if (monthvar == 12)
- {
- this.comboBox.Text = "December";
- }
- }

Rick BarberPosted Jan 27, 2017, 6:23 AM
If you replace line 34 with this.comboBox.SelectedItem = DateTime.Now.ToString("MMMM"); you won't need the other 60+ lines of code.
Casper BarchagerPosted Dec 6, 2014, 1:50 PM
I needed this at one time. Good one.
Douglas MillerPosted Dec 6, 2014, 8:34 AM
Glad you like it!
Manish Kumar ChoudharyPosted Dec 6, 2014, 5:26 AM
Good one..