Solved

DateTimePicker - setting the default month on DropDown

Posted on 2004-10-06
5
665 Views
Last Modified: 2012-08-13
WinForms application in VB.Net...

I have a DateTimePicker in my application.  When the user drops down the dtp to reveal the calendar, I want to somehow set the default calendar month to the current month - irrespective of the current value of the dtp.

I want to do this without actually changing the dtp's value.  In other words, after dropping down the dtp, if the user decides not to change the date and clicks off the dtp without selecting another date then I want the date to remain the same as before the dtp was dropped down.

I hope that makes sense.

Thanks
0
Comment
Question by:Sigh_Man
  • 3
5 Comments
 
LVL 1

Author Comment

by:Sigh_Man
ID: 12236792
To clarify...

if the current date value is 1 January 2000, I still want the dtp calendar to show (by default) this month, ie October 2004 on DropDown.
0
 
LVL 19

Assisted Solution

by:Desp
Desp earned 20 total points
ID: 12236937
Dim d As DateTime = DateTimePicker1.Value
DateTimePicker1.Value = Now.Month.ToString() + "-" + d.Day.ToString() + "-" + d.Year.ToString()
0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 12237039
I don't want to change the value - I just want the drop down calendar to display THIS MONTH rather than the month of the dtp's current value.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 80 total points
ID: 12238795
I don't think it can be done without changing the current value.  Here is a way to store the current value and then put it back if the user clicks off the DTP without changing the value.  If you dismiss the dropdown with the ESC key though, that registers as a change so it doesn't work then.

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private dtpValue As DateTime
    Private dtpChanged As Boolean

    Private Sub DateTimePicker1_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker1.DropDown
        dtpValue = DateTimePicker1.Value
        DateTimePicker1.Value = Now
        dtpChanged = False
    End Sub

    Private Sub DateTimePicker1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        dtpChanged = True
    End Sub

    Private Sub DateTimePicker1_CloseUp(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker1.CloseUp
        If Not dtpChanged Then
            DateTimePicker1.Value = dtpValue
        End If
    End Sub

End Class
0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 12242665
Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question