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.
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
Property | Type | Default | Description |
---|---|---|---|
Autoresize | Boolean | False | Determines the value or determines whether the size of the RadioButton automatically adapts to the descriptive text. |
Text | String | Null | Determines the text or specifies the text that is displayed as a label on the RadioButton. |
Caption | String | Null | Synonym for Text |
Value | Boolean | False | Determines 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.
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:
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.
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.
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
Die Klasse RadioButton (gb.qt4) implementiert eine RadioButton-Komponente. RadioButton im gleichen Container schließen sich gegenseitig aus – nur ein RadioButton (aus der Gruppe) im Container kann ausgewählt sein.
Abbildung 16.18.1: Projekt: Berechnung von Partialsummen (IDE)
Es ist daher eine gute Idee ein Panel ① als Container für eine RadioButton-Gruppe einzusetzen.
Komponente RadioButton
Eigenschaft | Typ | Default | Beschreibung |
---|---|---|---|
Autoresize | Boolean | False | Ermittelt den Wert oder legt fest, ob sich die Größe des RadioButton automatisch an den beschreibenden Text anpasst. |
Text | String | Null | Ermittelt den Text oder legt den Text fest, der auf dem RadioButton als Beschriftung angezeigt wird. |
Caption | String | Null | Synonym für Text |
Value | Boolean | False | Ermittelt oder legt fest, ob der RadioButton ausgewählt ist oder nicht. |
Tabelle 16.18.1: Eigenschaften RadioButton
Das RadioButton.Click-Ereignis wird ausgelöst, wenn der User auf einen RadioButton klickt oder die Eigenschaft RadioButton.Value geändert wurde.
In diesem Beispiel erfahren Sie, wie man den Zustand eines RadioButtons in einer Gruppe auswertet oder verändert. Da 3 Radiobutton auf einem Panel als Container zu einer Gruppe zusammengefasst werden, kann man die drei RadioButton auch logisch zu einer Gruppe zusammenfassen, indem man für jeden RadioButton die Eigenschaft RadioButtonx.Group auf einen gemeinsamen Gruppennamen festlegt. Die folgende Anweisung im Quelltext
RadioButton1.Group = "RBGruppe123"
endet jedoch mit dieser Fehlermeldung:
Unbekanntes Symbol 'Group' in Klasse 'RadioButton' in FMain:13.
weil die .Group-Eigenschaft virtuell und nur in der IDE vorhanden ist sowie zur Laufzeit nicht mehr existiert. Somit kann die .Group-Eigenschaft – für jeden RadioButton einzeln – nur in der IDE gesetzt werden!
Beachten Sie, dass der folgende erste Befehl nicht automatisch auch den Focus auf den ausgewählten RadioButton2 setzt; das müssen Sie explizit anweisen:
RadioButton2.Value = True ' RadioButton2 wird ausgewählt RadioButton2.SetFocus
Im Beispiel 1 wird die Nummer des ausgewählten RadioButtons in einer TextBox angezeigt oder Sie können die Ziffern 1 bis 3 als zulässige Eingabe dort eingeben und mit Enter den entsprechenden RadioButton auswählen:
Abbildung 16.18.1.1: Beispiel 1
' Gambas class file Private iRBNummer As Integer = 2 Public Sub _new() RadioButton2.Value = True ' RadioButton2 wird ausgewählt (Startwert) End ' _new() Public Sub Form_Open() FMain.Center FMain.Resizable = False … ' RadioButton2.Value = True hier am falschen Platz, weil jede *Änderung* des Wertes der Eigenschaft .Value ' das Ereignis RBGruppe123_Click() auslöst - und genau das passiert mit 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("Wert " & txtResult.Text & " nicht zulässig!") txtResult.Text = "1" Return Endif ' txtResult.Text Like "[1-3]" ? End ' txtResult_Activate()
Hinweis:
Mit der Festlegung der Gruppeneigenschaft .Group existiert nur noch das gemeinsame Click-Ereignis RBGruppe123_Click(). Sie müssen abwägen, ob Sie in dieser Prozedur mit der für jeden RadioButton (zusätzlich) gesetzten Tag-Eigenschaft mit Last.Tag differenziert arbeiten wollen oder auf die logische Gruppe RBGruppe123 verzichten und mit den Einzel-Ereignissen werkeln.
Die Prozedur zur Berechnung von Partialsummen berechnet die Partialsumme zwar nach dem gleichen mathematischen Algorithmus, jedoch nach drei unterschiedlichen Ansätzen in Bezug auf die (intern) verwendete Kontrollstruktur, die durch einen RadioButton alternativ aus der Gruppe der RadioButton ausgewählt wird.
Abbildung 16.18.2.1: Beispiel 2: Berechnung von Partialsummen
Der Quelltext wird hier nur in Auszügen angegeben; das vollständige Projekt finden Sie im Download-Bereich.
Public Sub Form_Open() Form1.Center Form1.Resizable = False ' Die Textbox Partialsumme wird als schreibgeschützt deklariert txtPartialsumme.ReadOnly = True Form1.Text = "Berechnung von Partialsummen mit a und e " & String.Chr(8712) & " " & String.Chr(8469) ... optVariante2.Value = True ' Variante 2 ist ausgewählt (Startoption) txtAnfangszahl.SetFocus End ' Form_Open Public Sub btnPartialsummeBerechnen_Click() If KontrolleEingabedaten() = "Dateneingabe fehlerfrei!" Then If optVariante1.Value = True Then Variante_1(iAnfangszahl, iEndzahl) ' Kontrollstruktur FOR..TO..NEXT Else If optVariante2.Value = True Variante_2(iAnfangszahl, iEndzahl) ' Kontrollstruktur REPEAT..UNTIL Else Variante_3(iAnfangszahl, iEndzahl) ' Kontrollstruktur DO..WHILE..LOOP Endif ' Variante 3 Endif ' KontrolleEingabedaten() End ' btnPartialsummeBerechnen … Public Sub RBGruppe1_Click() ' Wenn einer der 3 RadioButton angeklickt wird, dann wird der Inhalt der Textbox <txtPartialsumme> geleert. ' RBGruppe1 steht für die Gruppe "RBGruppe1", zu der alle 3 RadioButton gehören! txtPartialsumme.Clear End ' RBGruppe1_Click