logo

Toolbox Radio button

In an InForm application, radio buttons allow users to choose an option within a group. This page covers buttons in more detail.

Create a new Radio button control by clicking the Radio button icon ToolBox radio button in the Toolbox. The newly created control will be displayed in the preview window; allowing you to move it around to meet your design requirements. Edit the Radio button properties as necessary.

6) Radio button Properties:

ToolBox

Radio button examples: Preview window.

ToolBox

Note: Radio button properties will reflect the highlighted control selected in the preview window, enabling you to customize it as needed.

Radio buttons allow users to choose an option within a group. The user is presented with a circle that is either empty or checked, followed by a label indicating the setting being edited.

Radio buttons can be grouped. If placed on the main form, they are naturally exclusive, meaning only one can be selected at a time. However, if added to a frame, they can be selected independently from radio button controls outside the frame.

Design time:
At design time, you can set the Value property to True or False to determine the initial state of the radio button. To have it selected by default, set the Value property to True.

Run time:
At runtime, user manipulation triggers the ValueChanged event, allowing you to respond to changes made by the user. You can then read the Value property, which will return either True or False, indicating the current state of the radio button.

UserChoice%% = Control(ControlID).Value

SetCapation:
By using the SetCaption method, you can define a hot-key shortcut for your CheckBox control by placing an ampersand character before the letter you wish to be assigned as a shortcut (Alt+letter).

SetCaption Button1, "&OK"

Set Radio Button:
To change the value of a RadioButton control to True at runtime, use the SetRadioButtonValue method. This method ensures that other RadioButton controls in the same container are properly set to False first.

SetRadioButtonValue ControlID

Radio button examples - refer to the above image.

Specification: Let the Radio button example have the following design specification:

  1. Add two groups of three radio buttons with labels as shown.
  2. At runtime, set the radio buttons to JavsScript and the age range to "61-100".
  3. The Run button displays what is selected from each group.

Implimenation: The following provide solutions for the above:

Basic file RadioButtonTest.bas


': This program uses
': InForm - GUI library for QB64 - v1.5
': Fellippe Heitor, 2016-2024 - fellippe@qb64.org - @fellippeheitor
': https://github.com/FellippeHeitor/InForm
'-----------------------------------------------------------
Option _Explicit '<---
Dim Shared groupA As String '<---
Dim Shared groupB As String '<---

': Controls' IDs: ------------------------------------------------------------------
Dim Shared RadioButtonTest As Long
Dim Shared webFR As Long
Dim Shared ageFR As Long
Dim Shared htmlRB As Long
Dim Shared cssRB As Long
Dim Shared JavaScriptRB As Long
Dim Shared age0_30RB As Long
Dim Shared age31_60RB As Long
Dim Shared age61_100RB As Long
Dim Shared RunBT As Long

': External modules: ---------------------------------------------------------------
'$INCLUDE:'InForm\InForm.bi'
'$INCLUDE:'InForm\xp.uitheme'
'$INCLUDE:'RadioButtonTest.frm'

': Event procedures: ---------------------------------------------------------------
Sub __UI_BeforeInit

End Sub

Sub __UI_OnLoad
    SetRadioButtonValue JavaScriptRB '<---
    SetRadioButtonValue age61_100RB ' <---
End Sub

Sub __UI_BeforeUpdateDisplay
    'This event occurs at approximately 60 frames per second.
    'You can change the update frequency by calling SetFrameRate DesiredRate%

End Sub

Sub __UI_BeforeUnload
    'If you set __UI_UnloadSignal = False here you can
    'cancel the user's request to close.

End Sub

Sub __UI_Click (id As Long)
    Select Case id
        Case RadioButtonTest

        Case webFR

        Case ageFR

        Case htmlRB

        Case cssRB

        Case JavaScriptRB

        Case age0_30RB

        Case age31_60RB

        Case age61_100RB

        Case RunBT
            Dim resultStr As String '                                   <---
            resultStr = "Web design language = " + groupA + Chr$(10) '  <---
            resultStr = resultStr + "Your Age = " + groupB '            <---
            _MessageBox "Radio button test", resultStr '                <---

    End Select
End Sub

Sub __UI_MouseEnter (id As Long)
    Select Case id
        Case RadioButtonTest

        Case webFR

        Case ageFR

        Case htmlRB

        Case cssRB

        Case JavaScriptRB

        Case age0_30RB

        Case age31_60RB

        Case age61_100RB

        Case RunBT

    End Select
End Sub

Sub __UI_MouseLeave (id As Long)
    Select Case id
        Case RadioButtonTest

        Case webFR

        Case ageFR

        Case htmlRB

        Case cssRB

        Case JavaScriptRB

        Case age0_30RB

        Case age31_60RB

        Case age61_100RB

        Case RunBT

    End Select
End Sub

Sub __UI_FocusIn (id As Long)
    Select Case id
        Case htmlRB

        Case cssRB

        Case JavaScriptRB

        Case age0_30RB

        Case age31_60RB

        Case age61_100RB

        Case RunBT

    End Select
End Sub

Sub __UI_FocusOut (id As Long)
    'This event occurs right before a control loses focus.
    'To prevent a control from losing focus, set __UI_KeepFocus = True below.
    Select Case id
        Case htmlRB

        Case cssRB

        Case JavaScriptRB

        Case age0_30RB

        Case age31_60RB

        Case age61_100RB

        Case RunBT

    End Select
End Sub

Sub __UI_MouseDown (id As Long)
    Select Case id
        Case RadioButtonTest

        Case webFR

        Case ageFR

        Case htmlRB

        Case cssRB

        Case JavaScriptRB

        Case age0_30RB

        Case age31_60RB

        Case age61_100RB

        Case RunBT

    End Select
End Sub

Sub __UI_MouseUp (id As Long)
    Select Case id
        Case RadioButtonTest

        Case webFR

        Case ageFR

        Case htmlRB

        Case cssRB

        Case JavaScriptRB

        Case age0_30RB

        Case age31_60RB

        Case age61_100RB

        Case RunBT

    End Select
End Sub

Sub __UI_KeyPress (id As Long)
    'When this event is fired, __UI_KeyHit will contain the code of the key hit.
    'You can change it and even cancel it by making it = 0
    Select Case id
        Case htmlRB

        Case cssRB

        Case JavaScriptRB

        Case age0_30RB

        Case age31_60RB

        Case age61_100RB

        Case RunBT

    End Select
End Sub

Sub __UI_TextChanged (id As Long)
    Select Case id
    End Select
End Sub

Sub __UI_ValueChanged (id As Long)
    Select Case id
        Case htmlRB
            groupA = "HTML" '  <---
        Case cssRB
            groupA = "CSS" '   <---
        Case JavaScriptRB
            groupA = "Java Script" '  <---
        Case age0_30RB
            groupB = "0 - 30" '   <---
        Case age31_60RB
            groupB = "31 - 60" '  <---
        Case age61_100RB
            groupB = "61 - 100" '  <---
    End Select
End Sub

Sub __UI_FormResized

End Sub

'$INCLUDE:'InForm/InForm.ui'


Form file RadioButtonTest.frm


': This form was generated by
': InForm - GUI library for QB64 - v1.5
': Fellippe Heitor, 2016-2024 - fellippe@qb64.org - @fellippeheitor
': https://github.com/FellippeHeitor/InForm
'-----------------------------------------------------------
SUB __UI_LoadForm

    DIM __UI_NewID AS LONG, __UI_RegisterResult AS LONG

    __UI_NewID = __UI_NewControl(__UI_Type_Form, "RadioButtonTest", 300, 300, 0, 0, 0)
    __UI_RegisterResult = 0
    SetCaption __UI_NewID, "Radio button test"
    Control(__UI_NewID).Font = SetFont("segoeui.ttf", 12)
    Control(__UI_NewID).HasBorder = False

    __UI_NewID = __UI_NewControl(__UI_Type_Frame, "webFR", 230, 92, 15, 13, 0)
    __UI_RegisterResult = 0
    SetCaption __UI_NewID, "Select Web language"
    Control(__UI_NewID).HasBorder = True
    Control(__UI_NewID).Value = 3
    Control(__UI_NewID).BorderSize = 1

    __UI_NewID = __UI_NewControl(__UI_Type_Frame, "ageFR", 230, 91, 15, 129, 0)
    __UI_RegisterResult = 0
    SetCaption __UI_NewID, "Select your age"
    Control(__UI_NewID).HasBorder = True
    Control(__UI_NewID).Value = 3
    Control(__UI_NewID).BorderSize = 1

    __UI_NewID = __UI_NewControl(__UI_Type_RadioButton, "htmlRB", 152, 23, 10, 14, __UI_GetID("webFR"))
    __UI_RegisterResult = 0
    SetCaption __UI_NewID, "HTML"
    Control(__UI_NewID).HasBorder = False
    Control(__UI_NewID).Value = -1
    Control(__UI_NewID).CanHaveFocus = True

    __UI_NewID = __UI_NewControl(__UI_Type_RadioButton, "cssRB", 150, 23, 12, 38, __UI_GetID("webFR"))
    __UI_RegisterResult = 0
    SetCaption __UI_NewID, "CSS"
    Control(__UI_NewID).HasBorder = False
    Control(__UI_NewID).CanHaveFocus = True

    __UI_NewID = __UI_NewControl(__UI_Type_RadioButton, "JavaScriptRB", 150, 23, 12, 62, __UI_GetID("webFR"))
    __UI_RegisterResult = 0
    SetCaption __UI_NewID, "JavaScript"
    Control(__UI_NewID).HasBorder = False
    Control(__UI_NewID).CanHaveFocus = True

    __UI_NewID = __UI_NewControl(__UI_Type_RadioButton, "age0_30RB", 152, 23, 10, 10, __UI_GetID("ageFR"))
    __UI_RegisterResult = 0
    SetCaption __UI_NewID, "0 - 30"
    Control(__UI_NewID).HasBorder = False
    Control(__UI_NewID).CanHaveFocus = True

    __UI_NewID = __UI_NewControl(__UI_Type_RadioButton, "age31_60RB", 150, 23, 12, 35, __UI_GetID("ageFR"))
    __UI_RegisterResult = 0
    SetCaption __UI_NewID, "31 - 60"
    Control(__UI_NewID).HasBorder = False
    Control(__UI_NewID).CanHaveFocus = True

    __UI_NewID = __UI_NewControl(__UI_Type_RadioButton, "age61_100RB", 150, 21, 12, 60, __UI_GetID("ageFR"))
    __UI_RegisterResult = 0
    SetCaption __UI_NewID, "61 - 100"
    Control(__UI_NewID).HasBorder = False
    Control(__UI_NewID).CanHaveFocus = True

    __UI_NewID = __UI_NewControl(__UI_Type_Button, "RunBT", 80, 23, 101, 246, 0)
    __UI_RegisterResult = 0
    SetCaption __UI_NewID, "Run"
    Control(__UI_NewID).HasBorder = False
    Control(__UI_NewID).CanHaveFocus = True

END SUB

SUB __UI_AssignIDs
    RadioButtonTest = __UI_GetID("RadioButtonTest")
    webFR = __UI_GetID("webFR")
    ageFR = __UI_GetID("ageFR")
    htmlRB = __UI_GetID("htmlRB")
    cssRB = __UI_GetID("cssRB")
    JavaScriptRB = __UI_GetID("JavaScriptRB")
    age0_30RB = __UI_GetID("age0_30RB")
    age31_60RB = __UI_GetID("age31_60RB")
    age61_100RB = __UI_GetID("age61_100RB")
    RunBT = __UI_GetID("RunBT")
END SUB


Events

Methods

Properties editable at runtime

See also: