Link to home
Start Free TrialLog in
Avatar of BritishJeffrey
BritishJeffrey

asked on

Using DateTimePicker with a checkbox and up down control

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
Avatar of kaufmed
kaufmed
Flag of United States of America image

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

Avatar of BritishJeffrey
BritishJeffrey

ASKER

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
But is that really a big deal? If you send a "Right" to the DateTimePicker, but the text isn't "selectable", what has broken?
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

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.  
NP. Glad to help  : )