We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Using DateTimePicker with a checkbox and up down control

BritishJeffrey
on
Medium Priority
283 Views
Last Modified: 2012-05-11
I am using VB2010 and winforms.

I am working with a DateTimePicker control where the ShowCheckBox is true and ShowUpDown is true.  The format of the control is Short Date.  

I am having a problem when the control is originally unchecked, and then the user checks it to enable it and tries to use the up and down arrows, but nothing happens.  I am guessing this is because none of the fields (month, day or year) are selected, so it doesnt know what is supposed to be increased/decreased.  I cant see any events that i can use to either select the day field, or capture the up down events to manually increase/decrease the date.  In addition, when the user increases the day past the end of the month, i would like it to go to the next month/year (ie they increase date from 1/30/10 to 1/31/10 and then 2/1/10 instead of it going back to 1/1/10).  Really i need to capture the click on the up and down buttons to do this...

any suggestions?  If i cant do this, i will need to use 3 seperate controls (checkbox, textbox and a up down control) and lots of code.

Jeff
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
Try the ValueChanged event:
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
    If Me.DateTimePicker1.Enabled Then
      SendKeys.Send("{RIGHT}")
    End If
End Sub

Open in new window

Author

Commented:
Thanks for the suggestion kaufmed, but for some reason the builtin checkbox does not change the value of enabled, even though it effectively does change the enabled prroperty!  This means that whenever the valuechanged property is raised (either through the checkbox or when you change a value in the datetimepicker), the routine will be called and sendkeys will be sent.

Jeff
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
But is that really a big deal? If you send a "Right" to the DateTimePicker, but the text isn't "selectable", what has broken?
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
Heheh....  never mind. I see why that's a bad idea now. Let's switch things up a bit, shall we?
Public Class Form1
    Private sendKey As Boolean
    Private firstSend As Boolean

    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        If Me.DateTimePicker1.Checked Then
            If Not sendKey AndAlso firstSend Then
                SendKeys.Send("{RIGHT}")
                firstSend = False
            End If
        Else
            firstSend = True
            sendKey = False
        End If
    End Sub
End Class

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Wow, this works.  I thought i had tried every combination of booleans to get this to work, but i must have missed this combination.  Thanks so much.  
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
NP. Glad to help  : )
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.