Results 1 to 15 of 15

Thread: Weird OptionButton behavior

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    11

    Weird OptionButton behavior

    Hi,
    I have a pair of option buttons in a frame, within another frame on a Vb6 form. There are several other controls on the form in their own frames or directly on the form. What I have observed is that after the option buttons are disabled and then re-enabled, based on a combo box selection, tabbing through changes which option is selected. If I continue to tab or shift-tab after the value is changed, it leaves the current selection and tabs through the currently selected control as expected. When tabbing through before changing the value in the combo box it tabs through as expected. We have a very similar dialog which doesn't display this behavior. I have done a code comparison on it and can't find any reason why this is occurring. Note: In the one that is having weird behavior, the form is being loaded in a VB.NET form vs. the other is loaded by either another VB6 form or a C# form (I have confirmed this isn't the cause, I loaded the other VB6 form from the VB.NET form and it still doesn't exhibit the behavior). Any help with this would be appreciated.
    Troy
    Last edited by Troy Reagan; Feb 26th, 2018 at 09:19 AM.

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,910

    Re: Weird OptionButton behavior

    Hi Troy,

    I read through your paragraph and got lost in your thought process about half-way through. If you could post a small example project (attachment button on the "Go Advanced" tab), that would be really super. I (and I suspect others as well) will attempt to figure out what you intend, but an example would still be so much easier.

    Good Luck,
    Elroy

    EDIT1: Also, don't forget that the forum's TOS don't allow EXE files, so just give us the VBP, FRM, FRX, BAS, etcetera source files.

    EDIT2: Here's a little project I threw together trying to see what you're talking about:

    Name:  Option.png
Views: 826
Size:  4.5 KB

    And here's the code I put into Form1:

    Code:
    
    Option Explicit
    
    Private Sub Combo3_Click()
        Select Case Combo3.Text
        Case "Options Enabled"
            Option1.Enabled = True
            Option2.Enabled = True
        Case "Options Disabled"
            Option1.Enabled = False
            Option2.Enabled = False
        End Select
    End Sub
    
    Private Sub Form_Load()
        Combo3.AddItem "Options Enabled"
        Combo3.AddItem "Options Disabled"
    End Sub
    
    
    And the whole thing is attached as a project.

    Using the enable/disable repeatedly, and the tab key, I couldn't get it do do anything that I would see as wrong.
    Attached Files Attached Files
    Last edited by Elroy; Feb 26th, 2018 at 11:13 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    11

    Re: Weird OptionButton behavior

    Hi Elroy,
    I have uploaded a sample project. To see the behavior, run the app and select the command button. This will open up a form with the controls on it. Select "Test2" from the dropdown and the option buttons will disable. Re-select "Test1" in the dropdown and the option buttons will enable. Now tab through the option button controls. Note that the first time through it will tab from one radio button to the other changing which one is selected. If you keep tabbing through it again, it only tabs to the one currently selected whether you tab or shift-tab through the control. You can do the steps over and over and get the same results.
    Troy
    Attached Files Attached Files

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Weird OptionButton behavior

    I don't have VB6 so I can't run it, but I'll propose a theory anyways, just based on how I remember things working.
    It has to do with how Option Buttons work in the first place, and with tab order. When they first enabled, it has no "value" - I'm assuming neither of the options are selected initially - so when you tab into the container, it can't accept focus, so it goes to the next thing in the order, which is the first child, the first option button in this case. As soon as the option button gets focus, it's selecting it, which causes it to be lit up. When you then tab again, it goes to the next item in the tab order, which is the second option button, and the process repeats there, and it becomes selected. When you then cycle all the way around, and tab back into the container, it actually sees there are option buttons and will skip over the non selected OBs to get to the one selected. Another tab there sends it back out and on to the next control. It does this because if it tabbed into the first one, it would re-select that, which potentially changes a user selection... so it doesn't do that. remember that option buttons are mutually exclusive within a group. There can only be one selected at any given time.

    So how do you select a non selected option button in a group? If I remember right, arrow keys (to select) and the space bar (to select).

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,910

    Re: Weird OptionButton behavior

    Hi Troy,

    You're using some CSIGenCombo.ocx control. That's not something I've ever heard of before. It wasn't included in your project (and you actually can't include it because it's binary), so I'm unable to test under the same conditions as you.

    However, I bet your problems are something to do with that CSIGenCombo.ocx. The regular ComboBox won't do what you need? I learned long ago to stay far away from non-Microsoft third-party controls, as they've always caused me problems. The only time I'll use them is when they're open-source and I can include the source code into my project (as a custom User Control). That way, I can at least debug any problems (or consult with the author to debug them).

    Best Of Luck,
    Elroy
    Last edited by Elroy; Feb 27th, 2018 at 12:20 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,910

    Re: Weird OptionButton behavior

    Ahhh, techgnome and I were posting at the same time. And yes, I've had precisely the same problem that techgnome outlined. Basically, the rule-of-thumb is this: Don't ever use an OptionButton as TabIndex=0. As long as you follow that rule, you won't have the problem. However, as you tab through your controls, you will still have an OptionButton in a group auto-selected if one isn't already selected. But I don't think that's the problem you're outlining.

    Good Luck,
    Elroy

    EDIT1: If you're in the habit of making your OptionButton groups into Control Arrays, here's a nice piece of code for you:

    Code:
    
    Public Sub TurnOffAllOptions(opt As Object)
        ' This does NOT fire the clicked event.
        Dim o As OptionButton
        '
        For Each o In opt
            o.Value = False
        Next o
    End Sub
    
    
    Just pass the OptionButton array (no index specified) and it'll turn them all off. I sometimes have a small "Clear" button beside my OptionButton groups so the user can de-select everything.

    Name:  OptClear.png
Views: 749
Size:  3.5 KB
    Last edited by Elroy; Feb 27th, 2018 at 12:39 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    11

    Resolved Re: Weird OptionButton behavior

    So the mystery has been solved. I'm attaching another sample that uses a VB combo box and removed references to our 3rd party controls in case anyone is interested. The issue was that I was enabling the Option buttons before their containing frame, why it causes this behavior I don't know but if you enable the frame before the controls it works as expected. Thanks for taking a look though and I appreciate the suggestion on setting multiple control group controls.
    Attached Files Attached Files
    Last edited by Troy Reagan; Feb 27th, 2018 at 01:09 PM. Reason: Updating to resolved

  8. #8
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,671

    Re: Weird OptionButton behavior

    Quote Originally Posted by Troy Reagan View Post
    So the mystery has been solved. I'm attaching another sample that uses a VB combo box and removed references to our 3rd party controls in case anyone is interested. The issue was that I was enabling the Option buttons before their containing frame, why it causes this behavior I don't know but if you enable the frame before the controls it works as expected. Thanks for taking a look though and I appreciate the suggestion on setting multiple control group controls.
    Hello, I tested your sample and I don't see a change in behavior enabling the frame before or after.

    1) I select Test1 from the list, then the options are enabled.
    2) I click on "Print first page only", so that option is selected.
    3) Select Test2 from the list, then the frame is disabled.
    4) Select Test1 from the list, then the frame is re-enabled.
    5) Press the Tab key, then the focus goes to "All", and so changed the selected option.

  9. #9

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    11

    Re: Weird OptionButton behavior

    Quote Originally Posted by Eduardo- View Post
    Hello, I tested your sample and I don't see a change in behavior enabling the frame before or after.

    1) I select Test1 from the list, then the options are enabled.
    2) I click on "Print first page only", so that option is selected.
    3) Select Test2 from the list, then the frame is disabled.
    4) Select Test1 from the list, then the frame is re-enabled.
    5) Press the Tab key, then the focus goes to "All", and so changed the selected option.
    I was referring to in the code, if you edit the code so the frame is enabled first the value doesn't change the first time through.

    Tabbing through the control shouldn't change the value. If "Print first page only" was selected it should tab to the control but not change it to "All". If you tab through it again, the radio button selected doesn't change which is the behavior you should see by default every time you tab to it. I know some people have implemented, or at least tried to implement, this behavior by using key events but by default it shouldn't cause the value to change.

  10. #10
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,671

    Re: Weird OptionButton behavior

    Quote Originally Posted by Troy Reagan View Post
    I was referring to in the code, if you edit the code so the frame is enabled first the value doesn't change the first time through.

    Tabbing through the control shouldn't change the value. If "Print first page only" was selected it should tab to the control but not change it to "All". If you tab through it again, the radio button selected doesn't change which is the behavior you should see by default every time you tab to it. I know some people have implemented, or at least tried to implement, this behavior by using key events but by default it shouldn't cause the value to change.
    What I was saying is that the "wrong behavior" is the same whether you enable the frame first or not.
    So I don't see how it could be "resolved".

  11. #11
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,910

    Re: Weird OptionButton behavior

    Quote Originally Posted by Troy Reagan View Post
    Tabbing through the control shouldn't change the value.
    That's not quite true. If you tab through groups of OptionButtons, if none are initially selected, when you tab to one in the group, the OptionButton with the lowest TabIndex will be auto-selected (i.e., Value set to True). This is just the way they work.

    But, if one is already selected, then nothing should change.

    However, along with Eduardo, I fail to see how enabling/disabling things in different orders affects any of this. But, if you've found a way to make it work the way you want, I suppose that's what matters most.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  12. #12
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,671

    Re: Weird OptionButton behavior

    Deleted.

  13. #13

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    11

    Re: Weird OptionButton behavior

    Quote Originally Posted by Elroy View Post
    That's not quite true. If you tab through groups of OptionButtons, if none are initially selected, when you tab to one in the group, the OptionButton with the lowest TabIndex will be auto-selected (i.e., Value set to True). This is just the way they work.

    But, if one is already selected, then nothing should change.

    However, along with Eduardo, I fail to see how enabling/disabling things in different orders affects any of this. But, if you've found a way to make it work the way you want, I suppose that's what matters most.
    Correct, but in this case, it was tabbing from one to the other. It didn't matter if a selection was already made or not. Each time you disable and then re-enabled the option buttons by changing the value in the dropdown, it would change the selected radio button the first time you tabbed through the control. Strange behavior for not enabling controls in a specific order which is why it took a while to figure out. If it was just changing the value because nothing was initially selected, it would change it the first time through and then stay the same after that. In case it isn't clear to some, the sample demonstrates the weird behavior, it, to fix it you have to re-arrange the code so the containing frame is enabled before the option controls. When that is done it doesn't update the value no matter how many times you tab or shift-tab through the control after disabling and re-enabling them.

    If you replace the following event handler (changes are in bold italics) in Elroy's sample, you will see the same behavior as my sample (just make sure the frame is enabled after the option controls):

    Private Sub Combo3_Click()
    Select Case Combo3.Text
    Case "Options Enabled"
    Option1.Enabled = True
    Option2.Enabled = True
    Frame2.Enabled = True
    Case "Options Disabled"
    Frame2.Enabled = False
    Option1.Enabled = False
    Option2.Enabled = False
    End Select
    End Sub

  14. #14
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,910

    Re: Weird OptionButton behavior

    Ahhh, I see what you're saying now.

    I didn't test but here's what I think is happening. But first some ground-rules. When we tab through OptionButton-Groups, only the one that's selected gets the focus. The others in the group don't get the focus, even on subsequent tab keys. I think we can all agree on that.

    However, if you start disabling things and give the focus nowhere else to go but an unselected OptionButton, the focus will go there and also select that OptionButton. Here's an example. I just threw two OptionButtons onto Form1:

    Name:  Form1.png
Views: 737
Size:  2.0 KB

    And then I put this code into Form1:

    Code:
    
    Option Explicit
    
    Private Sub Form_Click()
        Option1.Enabled = False
    End Sub
    
    Now, when you execute that program, it'll auto-select Option1 because that's the lowest TabOrder, and the focus just automatically goes there.

    However, now here's the trick. When you click on the form, Option1 gets disabled, and there's only one other place for the focus to go: Option2.

    Therefore, the focus goes to Option2 and it gets auto-selected, even though Option1 was previously auto-selected.

    I do believe that's the phenomenon you're seeing. You're isolating the focus, not giving it anyplace to go.

    Best Regards,
    Elroy

    EDIT1: Actually, I did test, and it works exactly as I outlined.

    EDIT2: Solutions: 1) Don't have any of your OptionButtons as the lowest enabled TabOrder; and 2) Keep the TabOrders together on your OptionButton-groups; and 3) Don't start disabling OptionButtions while one-in-a-group has the focus. If you abide by those three rules, I don't think you'll have any problems. I think, in most cases, we sort of naturally abide by them, but it's obviously possible to violate them.
    Last edited by Elroy; Feb 28th, 2018 at 10:08 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  15. #15

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    11

    Re: Weird OptionButton behavior

    Quote Originally Posted by Elroy View Post
    Ahhh, I see what you're saying now.

    I didn't test but here's what I think is happening. But first some ground-rules. When we tab through OptionButton-Groups, only the one that's selected gets the focus. The others in the group don't get the focus, even on subsequent tab keys. I think we can all agree on that.

    However, if you start disabling things and give the focus nowhere else to go but an unselected OptionButton, the focus will go there and also select that OptionButton. Here's an example. I just threw two OptionButtons onto Form1:

    Name:  Form1.png
Views: 737
Size:  2.0 KB

    And then I put this code into Form1:

    Code:
    
    Option Explicit
    
    Private Sub Form_Click()
        Option1.Enabled = False
    End Sub
    
    Now, when you execute that program, it'll auto-select Option1 because that's the lowest TabOrder, and the focus just automatically goes there.

    However, now here's the trick. When you click on the form, Option1 gets disabled, and there's only one other place for the focus to go: Option2.

    Therefore, the focus goes to Option2 and it gets auto-selected, even though Option1 was previously auto-selected.

    I do believe that's the phenomenon you're seeing. You're isolating the focus, not giving it anyplace to go.

    Best Regards,
    Elroy

    EDIT1: Actually, I did test, and it works exactly as I outlined.

    EDIT2: Solutions: 1) Don't have any of your OptionButtons as the lowest enabled TabOrder; and 2) Keep the TabOrders together on your OptionButton-groups; and 3) Don't start disabling OptionButtions while one-in-a-group has the focus. If you abide by those three rules, I don't think you'll have any problems. I think, in most cases, we sort of naturally abide by them, but it's obviously possible to violate them.
    I agree with what you are saying but wanted to bring up one more point. In this scenario, we are using the dropdown to enable/disable the option buttons. In that case, the focus should be on that control not the option buttons. To be completely sure (in case I was mistaken in the order of events) I also used the modified version of your sample, I first set focus to the Combo2 drop down before disabling and re-enabling the options using Combo3, in that case the focus was never on the option buttons. If I tab through in this scenario the option button selected still changes the first time through. Not trying to drag this out, just wanted to clarify that I can still get it to happen without the focus being on those controls.

Tags for this Thread

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