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:
-
-
-
-
-
-
-
-
- public FormPickAMonth()
- {
- InitializeComponent();
-
-
-
-
- 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");
-
-
-
-
-
- DateTime date1 = DateTime.Now;
-
-
-
-
- monthvar = date1.Month;
-
-
-
-
-
-
-
-
-
-
-
- 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";
- }
-
- }
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.