We help IT Professionals succeed at work.

Detect active field before Button click

Mik Mak
Mik Mak asked
on
I'm working on a VB Compact Framework application, and have a Date-picker field containing (surprise:) dates.

We have six of theses date fields (EventDate1, EventDate2...EventDate6). Sometimes the user needs to put in a special date (like "31-12-2099") which is a code for a special event. So I've made a button to register this date "code" - but I don't know how to detect which of the EventDateX fields was activated right before the user pressed the special date button ?

Please advice
MIchael Special dates
Comment
Watch Question

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
Try something like this:
Public Class Form1

    Private LastEventDate As DateTimePicker = Nothing

    Private Sub Event_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles EventDate1.ValueChanged, EventDate2.ValueChanged, EventDate3.ValueChanged, EventDate4.ValueChanged, EventDate5.ValueChanged, EventDate6.ValueChanged
        LastEventDate = CType(sender, DateTimePicker)
    End Sub

    Private Sub btnSpecialDate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSpecialDate.Click
        If Not IsNothing(LastEventDate) Then
            ' ... use "LastEventDate" in here...
        End If
    End Sub

End Class

Open in new window

Mik MakConsultant

Author

Commented:
Thank you for the fast response. But will that sub detect that the datepicker had just been selected - ie the date hasn't been changed ?

The user should just need to click/mark the field, and then hit the special date button
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
You could do the exact same thing but with the GotFocus() event instead.
Mik MakConsultant

Author

Commented:
Excellent - I'll test it later and report back :)
Mik MakConsultant

Author

Commented:
Sorry for being slow, but how do I assign the value to the datepicker field in this - I'm unsure how I should use the LastEventDate to reference the field ?

    Private Sub btnSpecialDate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSpecialDate.Click
        If Not IsNothing(LastEventDate) Then
            ' ... use "LastEventDate" in here...
        End If
    End Sub
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
"LastEventDate" will be an actual reference to the last used DateTimePicker.

You can just use its Value() property:
Private Sub btnSpecialDate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSpecialDate.Click
        If Not IsNothing(LastEventDate) Then
            MessageBox.Show("Last Event Date Selected: " & LastEventDate.Value.ToString())
        End If
    End Sub

Open in new window

Mik MakConsultant

Author

Commented:
So I should be able to use something like this ?
    Private Sub btnSpecialDate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSpecialDate.Click
        If Not IsNothing(LastEventDate) Then
            LastEventDate.Value = "31-12-2099"
            LastEventDate = Nothing ' to reset the field reference ?
        End If
    End Sub
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Pass it an actual DateTime like this:

    LastEventDate.Value = New DateTime(2099, 12, 31)

*Though I'm not sure the year 2099 is valid!  Is that a typo?

Setting it to Nothing is valid as well.
Mik MakConsultant

Author

Commented:
2099 is not a type :) It works like a charm - thank you for your speedy help :)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.