Windows Develop Bookmark and Share   
 index > Windows Forms General > Create Date user control
 

Create Date user control

Hi,

I have created a Date user control with 3dropdown boxes for "DAY", "MONTH" and"YEAR". I would like to validate them for the leap year etc. I needto implement the logic when the "MONTH" dropdown changed the "DAY" dropdown should display30 or 31 or 29 days.Could you please any sample codeor links to achieve this?

Thanks..
anate  Wednesday, July 15, 2009 3:05 PM
I *just* posted code to show how to bind the months (using localized months) and dates based on the month (that includes figuring out leap year and handling localized calendars).

It is posted on my blog:

http://msmvps.com/blogs/deborahk/archive/2009/07/14/dates-binding-to-month-names.aspx

http://msmvps.com/blogs/deborahk/archive/2009/07/14/dates-binding-to-day-numbers.aspx

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Wednesday, July 15, 2009 3:08 PM
To get the days of a month, you should also consider the YEAR,
you can get this by using methods in system.globalization

use the following function to fill thedate combobox on months selected index changed event:

Private Sub cboMonth_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboMonth.SelectedIndexChanged

        cboDay.Items.Clear()
        For i As Integer = 0 To CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(cboYear.SelectedValue, _
                                                                       cboMonth.SelectedIndex + 1) - 1

            cboDay.Items.Add(i + 1)

        Next

    End Sub

Vicky
Vikram Gorla  Thursday, July 16, 2009 5:24 AM
I *just* posted code to show how to bind the months (using localized months) and dates based on the month (that includes figuring out leap year and handling localized calendars).

It is posted on my blog:

http://msmvps.com/blogs/deborahk/archive/2009/07/14/dates-binding-to-month-names.aspx

http://msmvps.com/blogs/deborahk/archive/2009/07/14/dates-binding-to-day-numbers.aspx

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Wednesday, July 15, 2009 3:08 PM
Just in case you are not aware of it: The DateTimePicker control in Windows Forms is an excellent control for getting a date from the user. It is one they are familier with, it's localized, and it costs nothing to use.
Tergiver  Wednesday, July 15, 2009 9:07 PM
To get the days of a month, you should also consider the YEAR,
you can get this by using methods in system.globalization

use the following function to fill thedate combobox on months selected index changed event:

Private Sub cboMonth_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboMonth.SelectedIndexChanged

        cboDay.Items.Clear()
        For i As Integer = 0 To CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(cboYear.SelectedValue, _
                                                                       cboMonth.SelectedIndex + 1) - 1

            cboDay.Items.Add(i + 1)

        Next

    End Sub

Vicky
Vikram Gorla  Thursday, July 16, 2009 5:24 AM
Thanks for the reply. We decided not to use the DateTimePicker control because of the localization issues. It does support localization when we change the the language of the system through control panel. We do not want to change the language of the sytem. We want our application to have the flexibility to change the language from the application.
anate  Thursday, July 16, 2009 1:36 PM

You can change localization aspects (like CultureInfo) on a per-thread basis:

Thread.CurrentThread.CurrentCulture = new CultureInfo("whatever");

Tergiver  Monday, July 20, 2009 1:48 PM
Hi DeborahK,

Thanks for the blogs. I am getting "cannot implicitly convert type 'List<int>' to 'object'" error message when I bind the GetListOfDays to the comboBox.

Thank you.
anate  Monday, July 20, 2009 3:46 PM
Could you post your code?
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Monday, July 20, 2009 4:03 PM
Here is the code.



private void cboMonth_SelectedIndexChanged(object sender, System.EventArgs e)

{

cboDay.DataSource = GetListOfDays(Convert.ToInt32(cboYear.Text), Convert.ToInt32(cboMonth.Text));

}



private List<int> GetListOfDays(int yearNumber, int monthNumber)

{

Calendar currentCalendar = CultureInfo.CurrentCulture.Calendar;

int numberOfDays =

currentCalendar.GetDaysInMonth(yearNumber, monthNumber);

return Enumerable.Range(1, numberOfDays).ToList();

}

anate  Monday, July 20, 2009 5:01 PM

I assume cboDay is a normal combo box?

I have never had a problem assigning a list of any type to a combo box DataSource.

You could try adding (object) in the call. But it should not be required.

Does it generate the same error if you temporarily change it to this:

cboDay.DataSource = new List<int>();
I just tried this and it works fine as well (though obviously does not display anything in the combobox.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Monday, July 20, 2009 6:36 PM

You can use google to search for other answers

Custom Search

More Threads

• How to access a standard Control's HelpProvider extended attributes?
• Crop Image Possible?
• restrict mouse click
• How do I get my code to 'see' the Form's control?
• panel1.Visible problem....
• VB.NET - User input ????? Please help
• Using TreeView
• how to force arabic keyboard?
• Trouble with points and windows
• Draw StatusStrip SizingGrip