How to Populate Combo Box Control and Set Default Selection in C#

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.   
  9. public FormPickAMonth()  
  10. {  
  11.    InitializeComponent();  
  12.   
  13.    // clear the combo box control denoted by “comboBox”  
  14.    // and then add all 12 months to the control.  
  15.   
  16.    this.comboBox.Items.Clear();  
  17.    this.comboBox.Items.Add("January");  
  18.    this.comboBox.Items.Add("February");  
  19.    this.comboBox.Items.Add("March");  
  20.    this.comboBox.Items.Add("April");  
  21.    this.comboBox.Items.Add("May");  
  22.    this.comboBox.Items.Add("June");  
  23.    this.comboBox.Items.Add("July");  
  24.    this.comboBox.Items.Add("August");  
  25.    this.comboBox.Items.Add("September");  
  26.    this.comboBox.Items.Add("October");  
  27.    this.comboBox.Items.Add("November");  
  28.    this.comboBox.Items.Add("December");  
  29.   
  30.   
  31.    // instantiate a DateTime object called “date1”  
  32.    // to grab the current date from the system clock.  
  33.   
  34.    DateTime date1 = DateTime.Now;  
  35.   
  36.    // grab the month component from the “date1” object  
  37.    // and assign it to the “monthvar” variable.  
  38.   
  39.    monthvar = date1.Month;  
  40.   
  41.    // compare the number representing the current  
  42.    // month (1 through 12) to the numerical value  
  43.    // contained in the “monthvar” variable. upon a  
  44.    // successful comparison, assign the verbal  
  45.    // description of the month to the combo box  
  46.    // control denoted by “comboBox”. this assigned  
  47.    // verbal month description will be selected and  
  48.    // highlighted in the combo box as the winform  
  49.    // loads.  
  50.   
  51.    if (monthvar == 1)  
  52.    {  
  53.       this.comboBox.Text = "January";  
  54.    }  
  55.    if (monthvar == 2)  
  56.    {  
  57.       this.comboBox.Text = "February";  
  58.    }  
  59.    if (monthvar == 3)  
  60.    {  
  61.       this.comboBox.Text = "March";  
  62.    }  
  63.    if (monthvar == 4)  
  64.    {  
  65.       this.comboBox.Text = "April";  
  66.    }  
  67.    if (monthvar == 5)  
  68.    {  
  69.       this.comboBox.Text = "May";  
  70.    }  
  71.    if (monthvar == 6)  
  72.    {  
  73.       this.comboBox.Text = "June";  
  74.    }  
  75.    if (monthvar == 7)  
  76.    {  
  77.       this.comboBox.Text = "July";  
  78.    }  
  79.    if (monthvar == 8)  
  80.    {  
  81.       this.comboBox.Text = "August";  
  82.    }  
  83.    if (monthvar == 9)  
  84.    {  
  85.       this.comboBox.Text = "September";  
  86.    }  
  87.    if (monthvar == 10)  
  88.    {  
  89.       this.comboBox.Text = "October";  
  90.    }  
  91.    if (monthvar == 11)  
  92.    {  
  93.       this.comboBox.Text = "November";  
  94.    }  
  95.    if (monthvar == 12)  
  96.    {  
  97.       this.comboBox.Text = "December";  
  98.    }  
  99.   
  100. }  
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.

 

Up Next
    Ebook Download
    View all
    Learn
    View all