Results 1 to 5 of 5

Thread: adding "months" to a combobox [RESOLVED]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    adding "months" to a combobox [RESOLVED]

    I've been trying to do a loop to add the names of each month in a combo box. I haven't been successful. The best I've been able to do is get the numeric representation of this month 12 times

    I figured there would've been an enumerator ofr the months but apparently not.

    Any ideas how to loop through the months and display them. preferably starting with THIS month and ending with LAST month.

    eg.

    March
    .
    .
    .
    .
    .
    February

    so NEXT month, it looks like this:

    April
    .
    .
    .
    March

    as always, thanks.
    Last edited by Andy; Mar 23rd, 2004 at 03:00 PM.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    One way .
    VB Code:
    1. Dim months As New DateTimeFormatInfo
    2.         For i As Integer = 1 To 12
    3.             MsgBox(months.GetMonthName(i))
    4.         Next

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Two ways now :

    VB Code:
    1. For Each s As String In months.MonthNames
    2.             MsgBox(s)
    3. Next

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    aha! I missed the DateTimeFormatInfo().

    that makes all the difference in the world!!

    thanks pirate



    added:

    you must import this namespace to the project:

    System.Globalization
    Last edited by Andy; Mar 22nd, 2004 at 12:50 PM.

  5. #5
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    Wouldn't it be better if you preselect the current month? This way almost seems like you're wrapping into the new year...

    Taking into account the current culture:
    Code:
    		private void Page_Load(object sender, System.EventArgs e)
    		{
    			// Get the current month.
    			int month = DateTime.Now.Month;
    
    			// Get a string array of the current culture's month names.
    			System.Globalization.DateTimeFormatInfo formatInfo =
    				System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat;
    			string[] monthNames = formatInfo.MonthNames;
    
    			// Populate the combo.
    			for (int i = month; i < monthNames.Length; i++)
    				if (monthNames[i] != String.Empty)
    					DropDownList1.Items.Add(monthNames[i]);
    			for (int i = 0; i < month; i++)
    				if (monthNames[i] != String.Empty)
    					DropDownList1.Items.Add(monthNames[i]);
    		}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width