Results 1 to 13 of 13

Thread: OptionButton Click Event Fire on Form Load?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    31

    OptionButton Click Event Fire on Form Load?

    I came across a weird issue I've never seen before. I have a form with two option buttons (aka radio buttons) in a control array. When the form loads, the click event fired, even though the Value property isn't set anywhere in the code.

    As a test, I changed it from a control array into two separately named controls, and now the click event doesn't fire.

    This solves my problem, but I thought it was a weird issue. Do control arrays reinitialize at load even when they are set at design time?

    Just curious...

    - Nick

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: OptionButton Click Event Fire on Form Load?

    No it doesn't get fired automatically. There must be something in the code which is triggering it... Test it for yourself... start a new project and create an array of opt button and in the click event type this

    Code:
    Private Sub Form_Load()
        '~~> Nothing in form load
    End Sub
    
    Private Sub Option1_Click(Index As Integer)
        MsgBox "Hello"
    End Sub
    You will see that the mesage box doesn't show on form load...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: OptionButton Click Event Fire on Form Load?

    It returns True because if you have an array of OptionButtons one of them must be true.
    Code:
    Option Explicit
    
    Private Sub Form_Load()
       Me.Show
       If Option1(0).Value = True Then
          MsgBox "Option1(0).Value = True"
       End If
       If Option1(1).Value = False Then
          MsgBox "Option1(1).Value = False"
       End If
       Print Option1(0).Value
       Print Option1(1).Value
    End Sub
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    31

    Re: OptionButton Click Event Fire on Form Load?

    Quote Originally Posted by koolsid
    No it doesn't get fired automatically. There must be something in the code which is triggering it
    The default option is set at design time, and no part of the code ever changes the option value. The event only changed some other properties based on the Index value. As I said, this is a weird one.

    -- Nick

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: OptionButton Click Event Fire on Form Load?

    @CDrive : But you are checking for the value in the form load... OP has a different query...

    When the form loads, the click event fired, even though the Value property isn't set anywhere in the code.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: OptionButton Click Event Fire on Form Load?

    Nick did you try the experiment that i mentioned above?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    31

    Re: OptionButton Click Event Fire on Form Load?

    For reference my code looked like this:

    Code:
    Private Sub optSetRange_Click(Index As Integer)
        Dim sSource As String
        Dim bEnabled As Boolean
    
        sSource = "optSetRange_Click"
        Debug.Print "Call stack - " & sSource
    
        bEnabled = (Index = 1)
        'Set Enabled property of several controls based on bEnabled
    
    End Sub
    I had inserted the Debug.Print statements in every Sub and Function in the form's code to trace another issue when I noticed that this event was being triggered at load, without the option being clicked.

    The only thing I can think of is that the line,

    Code:
        bEnabled = (Index = 1)
    was assigning the Index value instead of testing equality, and thereby re-triggering the event. If that were the case, though, I would expected the event to re-fire until the stack overflowed when clicked manually. I tested that, and it doesn't happen.

    Like I said, splitting the control array into separately named option buttons with their own click events resolved the issue, so I'm not going to dwell on it. I just thought it was odd.

    @koolsid - No I didn't try your experiment. I'm knee deep in the current code, so I'll take your word for it.

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: OptionButton Click Event Fire on Form Load?

    If you are interested, we can check if optSetRange_Click(Index As Integer) is being fired or not... simply replace the above code by a messagebox

    Private Sub optSetRange_Click(Index As Integer)
    msgbox "fired"
    End Sub

    If you see the message box at form load then definitely your above mentioned code is not responsible....
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: OptionButton Click Event Fire on Form Load?

    Here is the scenario which is probably what applies to you.

    1. If an option button is unselected and it gets the focus, it will select itself and fire the click event, regardless whether it is arrayed or not.

    2. You are getting the click event on form load because the option button receiving the event has a tab index of 0 (the first control to gain focus on the form) and had a value of unselected.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    31

    Re: OptionButton Click Event Fire on Form Load?

    Quote Originally Posted by LaVolpe
    2. You are getting the click event on form load because the option button receiving the event has a tab index of 0 (the first control to gain focus on the form) and had a value of unselected.
    That sounds like the most likely cause, since one of the option buttons is tab index 0.

  11. #11
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: OptionButton Click Event Fire on Form Load?

    Quote Originally Posted by LaVolpe
    Here is the scenario which is probably what applies to you.

    1. If an option button is unselected and it gets the focus, it will select itself and fire the click event, regardless whether it is arrayed or not.

    2. You are getting the click event on form load because the option button receiving the event has a tab index of 0 (the first control to gain focus on the form) and had a value of unselected.
    This is interesting because either one of these statements in the Form_Load event will fire the Option1_Click() event, even though both of them are already true by default.
    Code:
    Option Explicit
    
    Private Sub Form_Load()
       Option1(0).TabIndex = 0
    '   Option1(0).Value = True
    End Sub
    
    Private Sub Option1_Click(Index As Integer)
       If Option1(0) Then MsgBox "Option1(0) Clicked"
       If Option1(1) Then MsgBox "Option1(1) Clicked"
    End Sub
    Last edited by CDRIVE; Feb 27th, 2009 at 05:52 PM.
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  12. #12
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: OptionButton Click Event Fire on Form Load?

    CDrive, interesting indeed. If all I have on a form are the 2 option buttons, arrayed as you do, and Option(0) has both TabIndex=0 & Value=True, I do not get any click events in either scenario during form_load, as you are describing. Now if none had Value=True, then I would.

    P.S. Are you sure that Option1(1) was not TabIndex=0, in design view, during your tests?
    Last edited by LaVolpe; Feb 27th, 2009 at 07:07 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  13. #13
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: OptionButton Click Event Fire on Form Load?

    LaVolpe, at Design Time Option1(0).TabIndex = 0 and Value = False, but this fact brings me back to my statement in post#3. I think the point here is that more than just Clicking on an OptionButton can fire the Click event.
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

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