Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date input from User 2

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,530
US
VS 2013, VB.NET Windows Form.
I need an input from the user, which is a Date. I don't want anybody to type the date, so I tried MonthCalendar as a way for a user to enter a date. I put a Label on a Form. Initially the label is empty and MonthCalendar is not Visible, so when user clicks on a label, today's date will be (pre) selected in a MonthCalendar and the 'calendar' is shown to the user. That seams to be the default setting for MonthCalendar anyway (am I right?). Once the data is selected from MonthCalendar, I want the date from MonthCalendar to be in a Label and the calendar to be gone (Visible to False). That seams to work, but...
The next step is - I already have a Date in a Label (ket's say 5/5/2005), so when user clicks on a Label, I want the MonthCalendar to show this date / have this date to be 'pre-selected' in MonthCalendar. How do I do that?

There are some other problems with MonthCalendar: it seams to be designed to work with Start and End dates, with the default 'span' of 7 days. I just want 1 date, and that could be in 1995 or 2044.

I did try DateTimePicker. I could make it show up when I needed it, but I could not make it gone (Visible = False) when I am done. Actually it was 'gone' before I was done selecting the Date: I did Visible = False when the Date was selected, but that also fired when you selected different Month or Year. So the DTP was gone before I had a chance to select a Date from other months / years.

Any suggestions as what/how to use to accomplish what I want?


Have fun.

---- Andy

There is a great need for a sarcasm font.
 
You can use MonthCalendar, it's just a bit tricky.

First, to display the date selected and in the label, use the SetDate method:

MonthCalendar1.SetDate(Convert.ToDateTime(Label1.Text))

As for the date range, you can fix that by using the SetSelectionRange method and using the same date for both of the parameters:

MonthCalendar1.SetSelectionRange(Convert.ToDateTime(Label1.Text), Convert.ToDateTime(Label1.Text))

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Code:
' In the Designer...
MonthCalendar1.MaxSelectionCount = 1

Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
    Dim dt As Date
    If (Not Date.TryParse(Label1.Text, dt)) Then dt = DateTime.Now
    MonthCalendar1.SetDate(dt)
    MonthCalendar1.Visible = Not MonthCalendar1.Visible ' Click the label again to dismiss the calendar
End Sub

Private Sub MonthCalendar1_DateSelected(sender As Object, e As DateRangeEventArgs) Handles MonthCalendar1.DateSelected
    Label1.Text = MonthCalendar1.SelectionStart.ToString("M/dd/yyyy")
    MonthCalendar1.Visible = False
End Sub
 
[tt].SetDate(Somedate)[/tt] was the answer. Thank you jebenson.
And thank you Dave, I had almost the same code, but I like your approach and I will use it :)

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top