Results 1 to 5 of 5

Thread: Disable RadioButton on Form unless criteria is met

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Disable RadioButton on Form unless criteria is met

    Hi All,

    I am a little bit stuck on this and not sure how to handle it... I have a form with approx 20 radiobuttons, once I click any radiobutton, that will trigger creating a dynamic button on another form. Where I am stuck is, how do I keep a single radiobutton disabled if lets say any of the other 19 buttons have been clicked? For example:

    RadioButton List Form1 -

    Code:
    RadioButton1 = APPLE
    RadioButton2 = ORANGE
    RadioButton3 = GRAPES
    RadioButton4 = PEARS
    RadioButton5 = EMPTY ORDER
    Form2 Textbox -

    Code:
    TextBox1 = "Order Number"
    Ok, so Form1 opens with selections, they choose APPLE, the button appears on Form2. Until TextBox1 changes on Form2, RadioButton5 (EMPTY ORDER) needs to remain disabled each time Form1 opens UNLESS RadioButton5 was the first and only selection. Meaning if I select Empty Order, I cannot select anything else until Order Number changes. If I select any Fruit, I cannot select Empty Order. Hopefully this comes across making sense.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,762

    Re: Disable RadioButton on Form unless criteria is met

    I'm assuming that the parent of the RadioButtons is the Form in my example, if not just change the reference to the Form name to the actual parent.

    What you can do is generate the CheckChanged event for each RadioButton and disable every RadioButton that is not the sender. For example:
    Code:
    Private Sub RadioButtons_CheckChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckChanged, RadioButton2.CheckChanged, RadioButton3.CheckChanged, etc.
        Dim checkedRadioButton As RadioButton = DirectCast(sender, RadioButton)
        For Each radiobutton As RadioButton In Controls.OfType(Of RadioButton)().Where(Function(rb) rb.Name <> checkedRadioButton.Name)
            radioButton.Enabled = False
        Next
    End Sub
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Re: Disable RadioButton on Form unless criteria is met

    Thanks dday9... I am not quite sure how to use this code for what I want. Will this work only one time while the form is open?

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,047

    Re: Disable RadioButton on Form unless criteria is met

    Can't say the description does make complete sense, though it's close. Radiobuttons work as a group. Only one can be selected at a time. Your description makes a bit more sense for checkboxes rather than radiobuttons. For example, you said,

    UNLESS RadioButton5 was the first and only selection
    If RadioButton5 is not the first selection, then it will be unselected, and only one CAN be selected. So, that doesn't really make sense, to me, unless you have each radiobutton in some kind of container such that each acts independently of the rest, in which case you should be using checkboxes instead.

    If all the radiobuttons are in a single group, then they don't need to do much of anything on the other form, because only one can be selected at a time, so you only need one button on the other form.

    For that reason, I'm not really sure where you are going, but you appear to be using radiobuttons in an odd way.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Re: Disable RadioButton on Form unless criteria is met

    Shaggy Hiker... you are correct, this is probably a very strange way to handle this. Right now I don't have any issue with the user selecting only one radiobutton as they are all in a group. At the moment, I am using this:
    Code:
            If Form1.PictureBox1.Controls.OfType(Of Button).Count >= 1 Then
                Me.RBEmptyOrder.Enabled = False
                Exit Sub
            End If
    All this is helping me for is IF when Form1 opens, it checks to see if any buttons have been created previously, if so then only that radiobutton is disabled meaning other selections have already been made. Now I need to go the opposite direction, IF RBEmptyOrder gets checked, then ALL others are locked unless the textbox in the other form has changed to another order. I get myself confused on this, so I hope I am getting this out with a little bit of sense.

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