date picker in combo box

bapu

New member
Joined
Sep 17, 2006
Messages
3
Programming Experience
Beginner
Hi

I just reg. today and i am new in this fild, so I am doing one exe. this time with birthday, so user have to choose dob and i want to use combo box to display date month and year but it's not coming out nicely but yes if i use domain up down and numeric up down than it's comes out ok but i dont know how do this in combo box i mean i can get month and year in combo box but i dont know how get days in month

can anybody help me with this :confused:

wating for kind rep.

have a nice time
 
Given that there exists the DateTimePicker control I would suggest that you use it. It is preferable to using individual ComboBoxes for each component because it handles all the validation for you, provides a Date object through the Value property and allows the user to select a date from a drop-down calendar, by typing or by using arrow keys on the keyboard. Far superior.

If you really must use ComboBoxes then you can use the Date.DaysInMonth method to get the number of days in a particular month for a particular year.
 
about combo box

First of thanks lot for your rep.

Yes I have to use combo box bcos that's what mentioned in my assinment so I have to use combo box

I have used this code for month and year I attached code file here so if you have time than have a look and tell me where I am wrong

wating for your kind rep.

have a nice time :)
VB.NET:
[SIZE=2][FONT=Courier New, monospace][COLOR=#0000ff]Dim[/COLOR] I [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]Integer[/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=Courier New, monospace][COLOR=#0000ff]Private[/COLOR] [COLOR=#0000ff]Sub[/COLOR] frmCalender_Load([COLOR=#0000ff]ByVal[/COLOR] sender [COLOR=#0000ff]As[/COLOR] System.Object, [COLOR=#0000ff]ByVal[/COLOR] e [COLOR=#0000ff]As[/COLOR] System.EventArgs) [COLOR=#0000ff]Handles[/COLOR] [COLOR=#0000ff]MyBase[/COLOR].Load[/FONT][/SIZE]
[SIZE=2][FONT=Courier New, monospace][COLOR=#0000ff]For[/COLOR] I = 1 [COLOR=#0000ff]To[/COLOR] 12[/FONT][/SIZE]
[FONT=Courier New, monospace][SIZE=2]cboMonth.Items.Add(I)[/SIZE][/FONT]
[FONT=Courier New, monospace][SIZE=2]cboMonth.SelectedIndex = 0[/SIZE][/FONT]
[SIZE=2][FONT=Courier New, monospace][COLOR=#0000ff]Next[/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=Courier New, monospace][COLOR=#008000]'get the years for combo box [/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=Courier New, monospace][COLOR=#0000ff]Dim[/COLOR] Year [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]Integer[/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=Courier New, monospace][COLOR=#0000ff]For[/COLOR] Year = 1900 [COLOR=#0000ff]To[/COLOR] [COLOR=#0000ff]CInt[/COLOR](Val(Format(Now, "yyyy")))[/FONT][/SIZE]
[FONT=Courier New, monospace][SIZE=2]cboYear.Items.Add(Year)[/SIZE][/FONT]
[FONT=Courier New, monospace][SIZE=2]cboYear.SelectedIndex = 0[/SIZE][/FONT]
[SIZE=2][FONT=Courier New, monospace][COLOR=#0000ff]Next[/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=Courier New, monospace][COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]Sub[/COLOR][/FONT][/SIZE]
 
 
[SIZE=2][FONT=Courier New, monospace][COLOR=#0000ff]Private[/COLOR] [COLOR=#0000ff]Sub[/COLOR] cboMonth_SelectedIndexChanged([COLOR=#0000ff]ByVal[/COLOR] sender [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]Object[/COLOR], [COLOR=#0000ff]ByVal[/COLOR] e [COLOR=#0000ff]As[/COLOR] System.EventArgs) [COLOR=#0000ff]Handles[/COLOR] cboMonth.SelectedIndexChanged, cboYear.SelectedIndexChanged[/FONT][/SIZE]
[SIZE=2][FONT=Courier New, monospace]cboDate.ValueMember = Str([COLOR=#0000ff]Date[/COLOR].DaysInMonth((cboYear.SelectedIndex), cboMonth.SelectedIndex + 1))[/FONT][/SIZE]
[SIZE=2][FONT=Courier New, monospace][COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]Sub[/COLOR][/FONT][/SIZE]
 
Last edited by a moderator:
If you want to put the years from 1900 to the current year in a ComboBox then I'd suggest something along these lines:
VB.NET:
Const FIRST_YEAR As Integer = 1900
Dim currentYear As Integer = Date.Today.Year
Dim years(currentYear - FIRST_YEAR) As Integer

For index As Integer = 0 To years.GetUpperBound(0) Step 1
    years(index) = FIRST_YEAR + index
Next index

myComboBox.DataSource = years
You would do essentially the same thing for the month and day. Note that the year and month need only be done once, while the day will obvioulsy have to be done each time the month OR year changes. You need to do it when the year changes too because if you have selected Feb 29 and the year changes from a leap year then you can't leave the day as 29. Also, it's not the index of the year and month you're interested in. It's the value. You shouldn't be using the SelectedIndex of the year and month fields. You should be using the SelectedItem.
 
combo box

Hello Sir ,

Thanks lot and realy appriceate that and I will try this and inform you after
I am student and i am learning so I know i am going to make mistake but i will do it
thanks again

have a nice time :)
 
Back
Top