Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
i have combobox which is having months like jan, feb , march, i want to display current month on form load
Posted
Updated 30-Apr-14 20:57pm
v2
Comments
Richard MacCutchan 1-May-14 2:55am    
Get today's date, extract the month number and use that to select the correct month.

Don't necessarily do it in form load - try the Form.Shown event instead:
VB
MyMonthsComboBox.SelectedText = DateTime.Now.ToString("MMMM")
 
Share this answer
 
Comments
tanugarg 1-May-14 3:03am    
not working
OriginalGriff 1-May-14 3:30am    
Is that the error report you give when your car breaks down? Or do you give more details?
Remember that we can't see your screen, access your HDD, or read your mind.
So tell us what it does do, what it doesn't do, and help us to help you!
CHill60 1-May-14 6:50am    
It's not clear from your question exactly what text you have put into your combo box - have you used 3-letter abbreviations for the months, or used the full name? OriginalGriff's solution will work perfectly if you have used the full name.
If you have used 3 letter abbreviations then change the "MMMM" in the solution to "MMM"
If you have used a mixture then you should either be consistent (always a good idea) or use solution 3
Use following line to find current month number
int month = System.DateTime.Now.Month;

While adding months to combo, add with value as Month index.
Now assign above month to that value for combo box.
 
Share this answer
 
Comments
CHill60 1-May-14 6:55am    
That is not VB! It is also worth noting that when adding items to a comboBox you don't add an index - it is a natural attribute of the collection - so the OP should make sure that they add the months in the correct order.
It is also worth noting that the collection is zero-based not one-based
So to select the appropriate month they should use ComboBox1.SelectedIndex = DateTime.Now.Month - 1
Try
C#
DateTime.Today.ToString("MMMM")

For all formats, refer - Custom Date and Time Format Strings[^].
 
Share this answer
 
Comments
tanugarg 1-May-14 3:03am    
not working
Debug and check what is happening.
Try this:-

DateTime dt=DateTime.Now;

ComboBox.Text=dt.ToSting("MMM");
 
Share this answer
 
Comments
CHill60 1-May-14 7:01am    
Works (when you correct the spelling mistake - ToString!) but this is also C# and not VB.NET
You can try:-

SQL
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(GetMonthName(DateTime.Now.Month)));
ddl.SelectedItem.Selected = true;

private string GetMonthName(int monthNumber)
    {
        DateTime dtDate = new DateTime(2000, monthNumber, 1);
        Return dtDate.ToString("MMMM");
    }


Thanks
 
Share this answer
 
v3
Comments
CHill60 1-May-14 6:59am    
This is C# not VB.net. 'FindByValue' is not a member of 'System.Windows.Forms.ComboBox.ObjectCollection' - it's a method on a ListItem but the OP is using a ComboBox
You can use this


int iMonth = DateTime.Now.Month;
string sMonth = "";
if (iMonth < 10)
sMonth = '0' + iMonth.ToString();
else
sMonth = iMonth.ToString();
dddlMonth.SelectedValue = sMonth;
 
Share this answer
 
Comments
CHill60 1-May-14 7:08am    
Doesn't work because the OP has jan, feb, mar in the comboBox not 01,02,03.<br>
For your benefit you could replace all of the code in your solution with
string sMonth = DateTime.Now.Month.ToString("00");

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900