User Tools

Site Tools


k16:k16.18:start

16.18 RadioButton

The class RadioButton (gb.qt4) implements a RadioButton component. RadioButton in the same container are mutually exclusive - only one RadioButton (from the group) in the container can be selected.

RB-IDE

Figure 16.18.1: Project: Calculation of partial sums (IDE)

It is therefore a good idea to use a panel ? as a container for a RadioButton group.

RadioButton

PropertyTypeDefaultDescription
AutoresizeBooleanFalseDetermines the value or determines whether the size of the RadioButton automatically adapts to the descriptive text.
TextStringNullDetermines the text or specifies the text that is displayed as a label on the RadioButton.
CaptionStringNullSynonym for Text
ValueBooleanFalseDetermines or determines whether the RadioButton is selected or not.

Table 16.18.1: RadioButton properties

The RadioButton. click event is triggered when the user clicks on a RadioButton or when the RadioButton. Value property has been changed.

16.18.1 Example 1 - RadioButton (Group - Read and set values)

In this example you will learn how to evaluate or change the state of a radio button in a group. Since 3 radio buttons are combined on a panel as a container to form a group, the three radio buttons can also be logically combined to form a group by setting the RadioButtonx. Group property to a common group name for each radio button. The following statement in the source code

RadioButton1.Group = "RBGruppe123"

ends with this error message:

Unbekanntes Symbol 'Group' in Klasse 'RadioButton' in FMain:13.

because the .group property is virtual, exists only in the IDE and no longer exists at runtime. Thus, the Group property - for each radio button individually - can only be set in the IDE!

Note that the following first command does not automatically focus on the selected RadioButton2; you must explicitly instruct this:

RadioButton2.Value = True  ' RadioButton2 is selected
RadioButton2.SetFocus

In example 1, the number of the selected radio button is displayed in a text box or you can enter the digits 1 to 3 as a valid entry and select the corresponding radio button with Enter:

RB2

Figure 16.18.1.1: Example 1

' Gambas class file
 
Private iRBNummer As Integer = 2
 
Public Sub _new()
  RadioButton2.Value = True  ' RadioButton2 is selected (start value)
End ' _new()
 
Public Sub Form_Open()
 
  FMain.Center
  FMain.Resizable = False'RadioButton2. Value = True here in the wrong place, because every *change* of the value of the property. 
'triggers the event RBGruppe123_Click() - and this is exactly what happens with RadioButton2.Value = True. 
' ---> _new ()
 
  RadioButton1.Tag = "1"
  RadioButton2.Tag = "2"
  RadioButton3.Tag = "3"
 
  RadioButton2.SetFocus
  txtResult.Text = RadioButton2.Tag 
 
End ' Form_Open()
 
Public Sub btnClose_Click()
  FMain.Close
End ' btnClose_Click()
 
'*********************************************************************************************************
 
Public Sub RBGruppe123_Click()
 
  iRBNummer = Last.Tag
  txtResult.Text = Str(iRBNummer)
  txtResult.SetFocus
 
  ' Alternative Anzeige:
  ' Select Last.Tag
  '   Case 1
  '     Message.Info("RadioButton 1 gedrückt")
  '   Case 2
  '     Message.Info("RadioButton 2 gedrückt")
  '   Case 3
  '     Message.Info("RadioButton 3 gedrückt")
  ' End Select
 
End ' RBGruppe123_Click()
 
Public Sub txtResult_Activate()
 
  If txtResult.Text Like "[1-3]" Then
     Select txtResult.Text      
       Case "1"
         RadioButton1.Value = True
       Case 2
         RadioButton2.Value = True
       Case 3
         RadioButton3.Value = True
     End Select
  Else
    Message.Error("Value " & txtResult.Text & " not allowed!")
    txtResult.Text = "1"
    Return
  Endif ' txtResult.Text Like "[1-3]" ?
 
End ' txtResult_Activate()

Note:
When the group property .Group is specified, only the common click event RBGroup123_Click() exists. You have to consider whether you want to work with the tag property (additional) set for each radio button in this procedure with Last.Tag differentiated, or whether you want to dispense with the logical group RBGroup123 and work with the individual events.

16.18.2 Example 2 - Calculation of partial sums in 3 variants

The procedure for calculating partial sums calculates the partial sum according to the same mathematical algorithm, but using three different approaches in relation to the (internal) control structure used, which is alternatively selected from the group of radio buttons by a radio button.

RB3

Figure 16.18.2.1: Example 2: Calculation of partial totals

The source code is only given here in excerpts; the complete project can be found in the download area.

Public Sub Form_Open()
  Form1.Center
  Form1.Resizable = False
' The text box 'partial total' is declared write-protected
  txtPartialsumme.ReadOnly = True
  Form1.Text = "Berechnung von Partialsummen mit a und e " & String.Chr(8712) & " " & String.Chr(8469)
...
  optVariante2.Value = True ' Variant 2 is selected (Start option)
  txtAnfangszahl.SetFocus
End ' Form_Open
 
Public Sub btnPartialsummeBerechnen_Click()
 
  If KontrolleEingabedaten() = "Data input error-free!" Then
     If optVariante1.Value = True Then
        Variante_1(iAnfangszahl, iEndzahl) ' control structure: FOR..TO..NEXT
     Else If
        optVariante2.Value = True 
        Variante_2(iAnfangszahl, iEndzahl) ' control structure: REPEAT..UNTIL
     Else
        Variante_3(iAnfangszahl, iEndzahl) ' control structure: DO..WHILE..LOOP
     Endif ' Variante 3   
  Endif ' KontrolleEingabedaten()
 
End ' btnPartialsummeBerechnen
…
 
Public Sub RBGruppe1_Click()
' If one of the 3 RadioButtons is clicked, the content of the textbox <txtPartialsum> is cleared.
' RBGruppe1 stands for the group "RBGruppe1", to which all 3 RadioButtons belong!
  txtPartialsumme.Clear()
End ' RBGruppe1_Click

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k16/k16.18/start.txt · Last modified: 29.09.2023 by emma

Page Tools