Results 1 to 9 of 9

Thread: Checked Change Fires two events? (Resolved)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354

    Checked Change Fires two events? (Resolved)

    This codes seems to fire two events, which I guess is because there is an event fired when I leave one radio button and then another event fired when I enter a new radio button.
    VB Code:
    1. Private Sub radQ1_CheckedChanged(ByVal sender As Object, _
    2.         ByVal e As System.EventArgs) Handles _
    3.         radQ1.CheckedChanged, radQ2.CheckedChanged, _
    4.         radQ3.CheckedChanged, radQ4.CheckedChanged
    5.  
    6.         MsgBox("You just changed the radio button.", _
    7.             MsgBoxStyle.OKOnly, "Event Fired")
    8.         FillDataGrid()
    9.  
    10.     End Sub
    Is there a way to do this that will fire only one event? Or is there a better way to do what I am doing, which is basically filling a datagrid based on which quarter the user chooses (4 radio buttons).
    Last edited by BukHix; Jun 24th, 2004 at 08:18 AM.

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Try casting sender into a RadioButton, and only call your fill routine if the control that caused the event is selected. If is not selected, then just ignore it.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Continuing what crptcblade explained , this might be the code you need :
    VB Code:
    1. Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged
    2.  
    3.         Select Case DirectCast(sender, RadioButton).Name
    4.             Case "RadioButton1"
    5.                 'do this
    6.  
    7.             Case "RadioButton2"
    8.                 'do this
    9.         End Select
    10.  
    11.     End Sub

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    Thanks! Am I missing something though? I am still getting the message twice for every change.
    VB Code:
    1. Private Sub radQ1_CheckedChanged(ByVal sender As Object, _
    2.         ByVal e As System.EventArgs) Handles _
    3.         radQ1.CheckedChanged, radQ2.CheckedChanged, _
    4.         radQ3.CheckedChanged, radQ4.CheckedChanged
    5.  
    6.         Select Case DirectCast(sender, RadioButton).Name
    7.             Case "radQ1"
    8.                 MsgBox("You just changed the radio button.")
    9.                 'FillDataGrid()
    10.             Case "radQ2"
    11.                 MsgBox("You just changed the radio button.")
    12.                 'FillDataGrid()
    13.             Case "radQ3"
    14.                 MsgBox("You just changed the radio button.")
    15.                 'FillDataGrid()
    16.             Case "radQ4"
    17.                 MsgBox("You just changed the radio button.")
    18.                 'FillDataGrid()
    19.         End Select
    20.     End Sub

  5. #5
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    The reason it fire's twice is because when you check a radiobutton another is automatically de-checked. so both of them will fire the event.

    Your code above for checking which one is correct. You just need a IF statement before showing you messagebox.

    VB Code:
    1. Private Sub radQ1_CheckedChanged(ByVal sender As Object, _
    2.         ByVal e As System.EventArgs) Handles _
    3.         radQ1.CheckedChanged, radQ2.CheckedChanged, _
    4.         radQ3.CheckedChanged, radQ4.CheckedChanged
    5.  
    6.         Select Case DirectCast(sender, RadioButton).Name
    7.             Case "radQ1"
    8.                 IF radQ1.Checked Then MsgBox("You just changed the radio button.")
    9.                 'FillDataGrid()
    10.             Case "radQ2"
    11.                 IF radQ2.Checked Then MsgBox("You just changed the radio button.")
    12.                 'FillDataGrid()
    13.             Case "radQ3"
    14.                 IF radQ3.Checked Then MsgBox("You just changed the radio button.")
    15.                 'FillDataGrid()
    16.             Case "radQ4"
    17.                 IF radQ4.Checked Then MsgBox("You just changed the radio button.")
    18.                 'FillDataGrid()
    19.         End Select
    20.     End Sub
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    That's right . I've just figured it out ! . To avoid a lot of hassle , use Click event .
    VB Code:
    1. Private Sub radQ1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles radQ1.Click, radQ2.Click, radQ3.Click, radQ4.Click
    2.  
    3.         Select Case DirectCast(sender, RadioButton).Name
    4.             Case "radQ1"
    5.                 MsgBox("You just changed the radio button.")
    6.                 'FillDataGrid()
    7.             Case "radQ2"
    8.                 MsgBox("You just changed the radio button.")
    9.                 'FillDataGrid()
    10.             Case "radQ3"
    11.                 MsgBox("You just changed the radio button.")
    12.                 'FillDataGrid()
    13.             Case "radQ4"
    14.                 MsgBox("You just changed the radio button.")
    15.                 'FillDataGrid()
    16.         End Select
    17.  
    18.     End Sub

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    Perfect. Thank you.

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    btw , you better use MessageBox.Show method not MsgBox .

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    Actually I removed the message boxes completely. Their only purpose was so that I could count how many times the event was being triggered.

    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width