dreinmann
asked on
Temporarily Disable EventHandler?
I have a DateTimePicker that I want to do something on it's ValueChanged event, but then I want to change the Value back to 'Yesterday's Date' without firing another event.
I've tried RemoveHandler/AddHandler, but it doesn't seem to work.
Private Sub dtpRequest_ValueChanged(By Val sender As Object, ByVal e As System.EventArgs) Handles dtpRequest.ValueChanged
Dim request As String
request = CStr(dtpRequest.Value.Date )
If lbxRequestedDays.Items.Con tains(requ est) = True Then
MsgBox("This day has already been selected.", vbOKOnly, "Attention")
Else
lbxRequestedDays.Items.Add (request.T oString)
End If
lblDaysOff.Text = lbxRequestedDays.Items.Cou nt.ToStrin g & " days"
lblDaysOff.Refresh()
End Sub
I've tried RemoveHandler/AddHandler, but it doesn't seem to work.
Private Sub dtpRequest_ValueChanged(By
Dim request As String
request = CStr(dtpRequest.Value.Date
If lbxRequestedDays.Items.Con
MsgBox("This day has already been selected.", vbOKOnly, "Attention")
Else
lbxRequestedDays.Items.Add
End If
lblDaysOff.Text = lbxRequestedDays.Items.Cou
lblDaysOff.Refresh()
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry - you've already tried that...guess I should read the whole question :)
ASKER
Yes, this code will run through twice. If I take out the part with the RemoveHandler and value changing to yesterday, it only runs once:
Private Sub dtpRequest_ValueChanged(By Val sender As Object, ByVal e As System.EventArgs) Handles dtpRequest.ValueChanged
Dim request As String
request = CStr(dtpRequest.Value.Date )
If lbxRequestedDays.Items.Con tains(requ est) = True Then
MsgBox("This day has already been selected.", vbOKOnly, "Attention")
Else
lbxRequestedDays.Items.Add (request.T oString)
End If
RemoveHandler dtpRequest.ValueChanged, AddressOf dtpRequest_ValueChanged
dtpRequest.Value = Now().AddDays(-1).Date
AddHandler dtpRequest.ValueChanged, AddressOf dtpRequest_ValueChanged
lblDaysOff.Text = lbxRequestedDays.Items.Cou nt.ToStrin g & " days"
lblDaysOff.Refresh()
End Sub
Private Sub dtpRequest_ValueChanged(By
Dim request As String
request = CStr(dtpRequest.Value.Date
If lbxRequestedDays.Items.Con
MsgBox("This day has already been selected.", vbOKOnly, "Attention")
Else
lbxRequestedDays.Items.Add
End If
RemoveHandler dtpRequest.ValueChanged, AddressOf dtpRequest_ValueChanged
dtpRequest.Value = Now().AddDays(-1).Date
AddHandler dtpRequest.ValueChanged, AddressOf dtpRequest_ValueChanged
lblDaysOff.Text = lbxRequestedDays.Items.Cou
lblDaysOff.Refresh()
End Sub
Try this code out. If you click button one, you'll get a message box. If you click button two then button one, nothing happens. You can also add AddHandler to reassociate the event.
Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
RemoveHandler Button1.Click, AddressOf Button1_Click
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("one")
End Sub
End Class
Let me know if you have any questions.
~b