0

I have a script like that, once user clicks on the radiobutton, messagebox do appear. But when i click da "No" dialogresult, messagebox appears twice. I dunno why does that happen. I don't want it to display twice times. I know there must be a so simple way to fix that. >.> I also tried to return the function but no luck.

 Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object,      ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
         Dim result As Integer = MessageBox.Show("message", "caption",      MessageBoxButtons.YesNo)
         If result = DialogResult.No Then
             RadioButton1.Checked = False
         ElseIf result = DialogResult.Yes Then
             memoryh4x(&H160ED98, 1, 4)
         End If
     End Sub
Can
  • 1
  • 1
  • 1
    cause if its no you check the radio button so it causes the event to fire again... keep clicking no... you will see... – Trevor Feb 28 '15 at 20:02

1 Answers1

0

You change the RadioButton Checked property and this cause the recall of the event handler a second time.

The simplest method to avoid this re-entrance to the event handler is through a global variable

Dim inCheckedChanged As Boolean = False
......

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object,      ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
     Try
        ' If we could display the message box..... 
        if inCheckedChanged = False then
           ' Block the reexecution of the MessageBox when clicking NO
            inCheckedChanged = True
            Dim result As Integer = MessageBox.Show("message", "caption",      MessageBoxButtons.YesNo)
            If result = DialogResult.No Then
                ' Now, changing the property reenters the event handler 
                ' but the global variable prevents to get a second messagebox
                RadioButton1.Checked = False
            ElseIf result = DialogResult.Yes Then
                memoryh4x(&H160ED98, 1, 4)
            End If
         End If
    Finally
         InCheckedChanged = False
    End Try
 End Sub

Notice that I put everything inside a Try/Finally block. In this way if something throws an exception the global variable InCheckedChanged is always reset to false before exiting from the event handler

Steve
  • 213,761
  • 22
  • 232
  • 286