User Tools

Site Tools


k16:k16.4:start

16.4 ToggleButton (gb. qt4)

This class implements a ToogleButton - a switchable button that has two states: engaged and disengaged. A ToggleButton can display a text, an image or both.

16.4.1 Properties

The properties of a ToggleButton differ only in some of the properties of a button and these are specified in the table:

tablelayout

ProperetyData typeDefaultDescription
ValueBooleanFalseIf the value is set to True, the ToggleButton is activated - the switch clicks into place.
RadioBooleanFalseDetects or determines whether a ToggleButton should behave like a RadioButton if at least 2 ToggleButtons are present in a container component.

Table 16.4.1.1: Selected ToggleButton properties

16.4.2 Events

The click event is also the dominant event for a ToggleButton. It is triggered when the user clicks with the mouse on the toggle button or when the value of the. value property changes.

16.4.3 Example 1

Example 1 shows the use of a toggle button in the source code section, which can be used to set two different print modes. The ToggleButton displays two different icons and tooltip texts depending on the switching status. The source code contains only the relevant sections of a project for printing images, which is fully described in chapter 23.6.1:

Public Sub Form_Open()
...
  togbtnColorModus.Picture = Picture["icon:/16/fill"]
  togbtnColorModus.Tooltip = "FarbModus: Farbdruck"
  myPrinter.GrayScale = False
...
End ' Form_Open()
 
 
Public Sub togbtnColorModus_Click()
 
  If togbtnColorModus.Value = True Then
     togbtnColorModus.Picture = Picture["icon:/16/properties"]
     togbtnColorModus.Tooltip = "FarbModus: Graustufen"
     printerImage.GrayScale = True
  Else
     togbtnColorModus.Picture = Picture["icon:/16/fill"]
     togbtnColorModus.Tooltip = "FarbModus: Farbe"
     printerImage.GrayScale = False
  Endif ' togbtnColorModus.Value = True ?
 
End ' togbtnColorModus_Click()

16.4.4 Example 2

If you use the TextArea component to display single-color text, the text is wrapped after a line end character. If the paragraph is longer, you may have to scroll right through the text to read it completely. You can prevent this behavior by using the TextArea.Wrap = True property to specify that the text is wrapped before the word which can no longer be displayed completely in the relevant line. Attention: There is no automatic hyphenation!

Source code extract:

Public Sub Form_Open()
...
  ToggleWrapping.Text = "Switch on wrapping" 
 
...
End ' Form_Open()
 
PUBLIC SUB ToggleWrapping_Click()
 
  If ToggleWrapping.Value Then 
     ToggleWrapping.Text = "Switch off wrapping" 
     TextArea.Wrap = True
  Else 
     ToggleWrapping.Text = "Switch on wrapping" 
     TextArea.Wrap = False
  Endif ' ToggleWrapping.Value = True ?
 
END ' ToggleWrapping_Click()

It is not a good plan to use “Wrapping on” or “WordWrapping off” as a label, for example, because it is not possible to decide whether it is a status or a request.

16.4.5 Example 3

With the property ToggleButton. Radio you can specify that a group of at least 2 ToggleButtons in a container component behaves like a RadioButton: Only a ToggleButton from the group can be activated.

Four toggle buttons are placed on a panel. The first three form a group and the first button is engaged. The ToggleButton number 4, on the other hand, has its own ToggleButton life, independent of the states of the first three and is engaged when the program is started:

[1] Public Sub Form_Open()
[2] ...
[3]   ToggleButton1.Radio = True
[4]   ToggleButton2.Radio = True
[5]   ToggleButton3.Radio = True
[6]   ToggleButton1.Value = True
[7]   
[8]   ToggleButton4.Value = True 
[9] 
...
[10] End ' Form_Open()

b1b2b3b4

Figure 16.4.5.1: ToggleButton on a panel

Comments:

  • Only one of the first three toggle buttons is engaged at a time. If you change the property values in lines 3 to 5 from True to False, you can switch the first three ToggleButtons separately again (→ Figure 4) which actions you assign to each ToggleButton is determined by the tasks to be solved.

Download

16.4 ToggleButton (gb.qt4)

Diese Klasse implementiert einen ToogleButton – eine Schaltfläche zum Umschalten, die zwei Zustände kennt: eingerastet und nicht eingerastet. Ein ToggleButton kann einen Text, ein Bild oder beides anzeigen.

16.4.1 Eigenschaften

Die Eigenschaften eines ToggleButton unterscheiden sich nur in einigen von den Eigenschaften eines Button und diese werden in der Tabelle angegeben:

ToggleButtonDatentypDefaultBeschreibung
.ValueBooleanFalseWird der Wert auf True gesetzt, dann wird der ToggleButton aktiviert – der Schalter rastet ein.
.RadioBooleanFalseErmittelt oder legt fest, ob sich ein ToggleButton wie ein RadioButton verhalten soll, wenn mindestens 2 ToggleButton in einer Container-Komponente vorhanden sind.

Tabelle 16.4.1.1: Ausgewählte ToggleButton-Eigenschaften

16.4.2 Ereignisse – Events

Auch bei einem ToggleButton ist das Klick-Ereignis das dominante Ereignis. Es wird ausgelöst, wenn der Benutzer mit der Maus auf den ToggleButton (Schaltfläche) klickt oder wenn sich der Wert der Eigenschaft .Value ändert.

16.4.3 Beispiel 1

Beispiel 1 zeigt im Quelltext-Ausschnitt den Einsatz eines ToggleButton, mit dem zwei unterschiedliche Druckmodi eingestellt werden können. Der ToggleButton zeigt je nach Schaltzustand zwei unterschiedliche Icon sowie verschiedene ToolTipp-Texte an. Der Quelltext enthält nur die relevanten Abschnitte aus einem Projekt zum Drucken von Bildern, das im Kapitel 23.6.1 vollständig beschrieben wird:

Public Sub Form_Open()
...
  togbtnColorModus.Picture = Picture["icon:/16/fill"]
  togbtnColorModus.Tooltip = "FarbModus: Farbdruck"
  myPrinter.GrayScale = False
...
End ' Form_Open()
 
 
Public Sub togbtnColorModus_Click()
 
  If togbtnColorModus.Value = True Then
     togbtnColorModus.Picture = Picture["icon:/16/properties"]
     togbtnColorModus.Tooltip = "FarbModus: Graustufen"
     printerImage.GrayScale = True
  Else
     togbtnColorModus.Picture = Picture["icon:/16/fill"]
     togbtnColorModus.Tooltip = "FarbModus: Farbe"
     printerImage.GrayScale = False
  Endif ' togbtnColorModus.Value = True ?
 
End ' togbtnColorModus_Click()

16.4.4 Beispiel 2

Setzen Sie die Komponente TextArea zur Darstellung von einfarbigem Text ein, dann wird der Text nach einem Zeilen-Ende-Zeichen umgebrochen. Das kann bei einem längeren Absatz dazu führen, dass Sie nach rechts durch den Text scrollen müssen, um ihn vollständig lesen zu können. Verhindern können Sie dieses Verhalten, in dem Sie über die Eigenschaft TextArea.Wrap = True festlegen, dass der Text vor dem Wort umgebrochen wird, welches nicht mehr vollständig in der betreffenden Zeile angezeigt werden kann. Achtung: Es erfolgt keine automatische Silbentrennung!

Quelltext-Ausschnitt:

Public Sub Form_Open()
...
  ToggleWrapping.Text = "Wrapping einschalten" 
 
...
End ' Form_Open()
 
PUBLIC SUB ToggleWrapping_Click()
 
  If ToggleWrapping.Value Then 
     ToggleWrapping.Text = "Wrapping ausschalten" 
     TextArea.Wrap = True
  Else 
     ToggleWrapping.Text = "Wrapping einschalten" 
     TextArea.Wrap = False
  Endif ' ToggleWrapping.Value = True ?
 
END ' ToggleWrapping_Click()

Es ist kein guter Plan, als Beschriftung zum Beispiel “Wrapping ein” oder “WordWrapping off” zu wählen, weil nicht entscheidbar ist, ob es sich um eine Zustandsangabe oder um eine Aufforderung handelt.

16.4.5 Beispiel 3

Mit der Eigenschaft ToggleButton.Radio können Sie festlegen, dass eine Gruppe von mindestens 2 ToggleButton in einer Container-Komponente sich wie ein RadioButton verhält: Nur ein ToggleButton aus der Gruppe kann eingeschaltet werden.

Vier ToggleButton werden auf einem Panel platziert. Die ersten drei bilden eine Gruppe und der erste Button ist eingerastet. Der ToggleButton Nummer 4 dagegen führt sein eigenes ToggleButton-Leben, unabhängig von den Zuständen der ersten drei und ist beim Programmstart eingerastet:

[1] Public Sub Form_Open()
[2] ...
[3]   ToggleButton1.Radio = True
[4]   ToggleButton2.Radio = True
[5]   ToggleButton3.Radio = True
[6]   ToggleButton1.Value = True
[7]   
[8]   ToggleButton4.Value = True 
[9] 
...
[10] End ' Form_Open()

b1b2b3b4

Abbildung 16.4.5.1: ToggleButton auf einem Panel

Kommentare:

  • Von den ersten drei ToggleButton ist stets nur einer eingerastet.
  • Ändern Sie die Eigenschaftswerte in den Zeilen 3 bis 5 von True auf False, dann können Sie die ersten drei ToggleButton wieder separat schalten (→ Bild 4)
  • Welche Aktionen Sie den einzelnen ToggleButton zuweisen, wird von den zu lösenden Aufgaben bestimmt.

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.4/start.txt · Last modified: 02.07.2018 (external edit)

Page Tools