Is there anyway i can select multiple dates in a Date Time Picker in Vb.net?
-
What are you actually trying to do? pls explain the scenario. BTW Its not possible to pick two dates at a time.Harsh– Harsh2012-01-24 10:14:09 +00:00Commented Jan 24, 2012 at 10:14
-
I am trying to get the collection of dates and want to use it for making salaries for employees. I found MonthCalendar control which seems to be perfect for this purpose but i cant find the option (method, property) to get the selectedDates and then use them. Any comments on that?raziiq– raziiq2012-01-24 10:17:27 +00:00Commented Jan 24, 2012 at 10:17
4 Answers
Use BoldedDates Property in your month calendar to get the array of DateTime objects. You can use this to get your collection and finally clear the bold dates after storing it, so that it can be reused.
1 Comment
Selecting Multiple dates in a DateTimePicker in vb.net?
If you want to select a range of dates using a DateTimePicker, then the best way if probably two DateTimePicker controls a from date and a to date.
You can then change the date of the opposite control when the first date is picked to make sure the FromDate is before the ToDate and vice versa
I found MonthCalendar control which seems to be perfect for this purpose but i cant find the option (method, property) to get the selectedDates and then use them.
If you want to use a MonthCalendar control you can specify a MaximumSelectionCount property and then the user can select dates by clicking on one and shift-clicking on another date
You can retrieve the dates the user selected by using the SelectionRange, or SelectionStart and SelectionEnd properties
How about if i want to select random dates, like 4th, 6th, 10th Feb.
This is more complex. A suggestion - you could use the BoldedDates array of the MonthCalendar and when a user clicks on a day, you bold it. You can then read the array after they have finished their selection? Maybe someone else has a suggestion for this?
1 Comment
Private listDates As List(Of DateTime)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Suppose that you have the following list of dates below
listDates = New List(Of DateTime)()
listDates.Add(DateTime.Now)
listDates.Add("12/22/2012")
listDates.Add(DateTime.Now.AddDays(-10))
End Sub
Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs) Handles Calendar1.DayRender
'Set Default properties
e.Day.IsSelectable = False
e.Cell.BackColor = System.Drawing.Color.Gray
'Now loop through the list of dates and make it
'Selectable
For Each d As DateTime In listDates
Calendar1.SelectedDates.Add(d)
If e.Day.IsSelected Then
e.Cell.BackColor = System.Drawing.Color.Green
e.Day.IsSelectable = True
End If
Next
End Sub
Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Calendar1.SelectionChanged
'Print the selected date
Response.Write("You have selected: " & Calendar1.SelectedDate.ToShortDateString())
End Sub
i found it, and try. it work, original source from here
Comments
There is no way to do this inside the existing DateTimePicker control.
The best alternative solution will depend on your specific situation. What's the context? If you're expecting the user to pick dates which are close together (and the range isn't too big) a reasonably simple solution is to use another control which will allow multiselect on a list.
For example a CheckedListBox or DataGridView.
Then populate it with a list of dates. In effect offering the user a picklist of dates.