INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Date input from User

Date input from User

(OP)
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.

RE: Date input from User

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!

RE: Date input from User

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 

RE: Date input from User

(OP)
.SetDate(Somedate) 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 smile

Have fun.

---- Andy

There is a great need for a sarcasm font.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close