Question Visual Basic Datetimepicker issue

nnick

Member
Joined
Apr 14, 2009
Messages
5
Programming Experience
Beginner
This function is supposed to alert the user if there is an attempt to change the date to another date (other than Today). When the user clicks Yes in the Messagebox another message box pops up to let the user know that date is changed and the new date chosen is displayed on the datetimepicker box as soon as the user clicks OK.
Basically the code works but the small issue is in the If statement when the "response = MsgBoxResult.No". When the user clicks on "No" in the message box to indicate they want to keep today's date, after the first click of "No" the message box does not disappear unless you click on "No" again. when the message box indicating that appears "Keeping today's date...." you again have to click "OK" twice to make it disappear and then it shows the first message box again and you click "No" twice and then "OK" twice to return control to the main forms window.
This problem can be removed by commenting the "Datetimepicker1.value = datetime.today" line but the date does not default to Today and the datetimepicker box shows the date that was selected.
Here's the code:

' ****************************************************
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
Dim msg = "Do you want to Overwrite The Date"
Dim title = "Pick Another Date"
Dim style = MsgBoxStyle.YesNo

Dim response = MsgBox(msg, style, title)

If response = MsgBoxResult.Yes Then
DateTimePicker1.Format = DateTimePickerFormat.Short
MsgBox("The Date Has Been Changed to " & _ DateTimePicker1.Value.ToShortDateString)
Exit Sub
End If

If response = MsgBoxResult.No Then
DateTimePicker1.Value = DateTime.Today
MsgBox("Keeping Today's Date " & Today.ToShortDateString)
Exit Sub
End If
End Sub
 

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
1. Please use CODE tags - it makes reading posts much easier.

2. You see two sets of messageboxes because of your logic. When you programatically set the date, the ValueChanged event is firing again.
 

nnick

Member
Joined
Apr 14, 2009
Messages
5
Programming Experience
Beginner
Thanks for your reply InertiaM. Do you have any ideas how I can keep my date unchanged without having to programatically set the date?
 

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
Remove the event handler before you programatically the date, then add it again afterwards.

Code:
        RemoveHandler DateTimePicker1.ValueChanged, AddressOf DateTimePicker1_ValueChanged
        DateTimePicker1.Value = Date.Now.AddDays(14)
        AddHandler DateTimePicker1.ValueChanged, AddressOf DateTimePicker1_ValueChanged
 

nnick

Member
Joined
Apr 14, 2009
Messages
5
Programming Experience
Beginner
InertiaM, Here's what I have done to make the change you suggested. When "Step Into" the code to execute line by line, the function "datetimepicker1_valuechanged" is still being called twice. I have tried the Handler statements in different sections of the IF statement but the same result. What am I missing here? where is the second call to the function coming from?


Code:
    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        Dim msg = "Do you want to Overwrite The Date"
        Dim title = "Pick Another Date"
        Dim style = MsgBoxStyle.YesNo

        Dim response = MsgBox(msg, style, title)

        'If DateTimePicker1.Value <> Today Then
        If response = MsgBoxResult.Yes Then
            DateTimePicker1.Format = DateTimePickerFormat.Short
            MsgBox("The Date Has Been Changed to " & DateTimePicker1.Value.ToShortDateString)
            AddHandler DateTimePicker1.ValueChanged, AddressOf DateTimePicker1_ValueChanged
            Exit Sub
        End If

        If response = MsgBoxResult.No Then
            RemoveHandler DateTimePicker1.ValueChanged, AddressOf DateTimePicker1_ValueChanged
            DateTimePicker1.Value = Date.Today
            AddHandler DateTimePicker1.ValueChanged, AddressOf DateTimePicker1_ValueChanged
            MsgBox("Keeping Today's Date " & Today.ToShortDateString)

            Exit Sub
        End If
        'End If
        'GetTxID()
    End Sub
 

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
No idea - I cant see why it shouldnt work but it obviously doesnt (I tested it as well).

How about a workaround? Disable the DTP, and then add a button. Put your "do you want to pick another date" question on the button Click event, and if the answer is yes, enable the DTP. This will also stop the question being asked multiple times if the user keeps selecting different dates.
 
Top Bottom