The ListView control in gb.qt4 (→ gb.gui.base) implements a list of selectable elements.
* To set the visible pointer - as a selection of a particular element in the ListView - assign the key of the element to be selected to the key property of an element.
The ListView class has these properties, among other things:
Property | Data type | Description |
---|---|---|
Current | _TreeView_Item | Returns the current element. In general, it is the element that has the focus or is selected (single selection mode). |
Item | _TreeView_Item | Returns the item to which the internal pointer points or NULL if the internal pointer cannot be reached. |
Count | Integer | Returns the number of all elements in the ListView. |
Editable | Boolean | Specifies whether the elements are editable by default. This means whether they can be renamed by the user when he clicks on them. |
Key | String | Returns the key of the current element. |
Mode | Integer | Sets the selection mode or returns the selection mode. The constants used are: Single, None or Multiple. |
Selection | String[] | Returns the keys of all selected elements in a string array (multi-selection mode). |
Sorted | Boolean | Displays or determines whether the elements are displayed sorted in the ListView. |
Table 17.4.0.0.1.1: Selected properties of the ListView class
The ListView class has these selected methods:
Method | Return type | Description |
---|---|---|
Exist (Key As String) | Boolean | Returns the value True if the element exists with the specified key. |
Add (Key As String, Text As String[, Picture As Picture, After As String]) | _TreeView_Item | Inserts an element into the ListView. |
MoveBack () | Boolean | If one of the other Move methods returns the value True, you can use this method to move the internal pointer to its original position. True is returned if the internal pointer was not at a valid position. |
MoveBelow () | Boolean | Moves the internal pointer to a visible element below the current one. True is returned if the move cannot be executed. |
MoveTo ([ Key As String]) | Boolean | Sets the internal cursor to the element in the ListView with the specified key. |
MoveBelow () | Boolean | Moves the internal pointer to a visible element below the current one. True is returned if the move cannot be executed. |
MoveCurrent () | Boolean | Sets the internal cursor to the selected element in the ListView. True is returned if the move cannot be executed. |
MoveFirst () | Boolean | Sets the internal cursor to the first element in the ListView. True is returned if the list is empty. |
MoveLast () | Boolean | Sets the internal cursor to the last element in the ListView. True is returned if the list is empty. |
Rename () | Boolean | Starts renaming the selected element. |
Table 17.4.0.2.1: Selected methods of the ListView class
The Add (…) method inserts a new element into the ListView:
Function Add ( Key As String, Text As String [ , Picture As Picture, After As String ] ) As _TreeView_Item
In the following example from the ListViewE project, the individual elements in the ListView are generated from the contents of a file:
Private Sub ListViewImport(Path As String, hListView As ListView) Dim cJSONCollection As JSONCollection Dim cCollection As JSONCollection Dim sKey, sText As String Dim picPicture As Picture hListView.Clear cJSONCollection = JSON.Decode(File.Load(Path), True) For Each cCollection In cJSONCollection sKey = cCollection["KEY"] sText = cCollection["TEXT"] picPicture = Picture[cCollection["PICTURE"]] hListView.Add(sKey, sText, picPicture, Null) ' Null, weil die Reihenfolge feststeht Next End ' ListViewExport(...)
The ListView class has the following events, among others:
Property | Description |
---|---|
Click () | The event is triggered when an element in the ListView is clicked. |
Select () | The event is triggered when the selection in the ListView changes. |
Compare (Key As String, OtherKey As String) | This event is triggered when comparing two elements in the ListView. The result of the comparison is stored in the Compare property. |
Table 17.4.0.3.1: Selected events of the ListView class
The next section introduces important properties and methods of the virtual class _TreeView_Item, since the properties Current and Item are of type _TreeView_Item.
The virtual class _TreeView_Item represents an entry in a ListView.
Property | Data type | Description |
---|---|---|
Count | Integer | Returns the number of elements. |
Editable | Boolean | Specifies whether this element can be edited. This means whether it can be renamed by the user when he clicks on it. |
Key | String | Returns the key. |
Picture | Picture | Returns the image or sets the image displayed next to the element. |
RichText | String | Returns the RichText or sets the RichText. |
Selected | Boolean | Specifies whether the element is selected. |
Text | String | Returns the displayed text on the element or sets the text to be displayed. |
Table 17.4.0.5.1: Selected properties of the virtual class _ListView_Item
The ListView only really shows its strength as a control element for displaying data when you generate the text as RichText and use it together with an image (data type Picture) in front of the text:
Figure 17.4.0.5.1: Using RichText in a ListView
Method | Description |
---|---|
Delete() | Deletes the (current) element from the ListView. |
Ensurevisibl () | Saves the (visible) display of the element in the ListView (scroll effect). |
Renam () | Starts renaming the selected element. |
Table 17.4.0.6.1: Selected methods of the virtual class _TreeView_Item
The use of different methods is illustrated by the following, extensively commented sample source code for deleting an (selected) element:
Public Sub btnDeleteElement_Click() ' Only one selected element may be deleted. If Not ListView1.Key Then Return ' First place the internal cursor on the selected element (start position) ListView1.MoveCurrent() ' Then place the internal cursor on the next *visible* - higher lying - element. ListView1.MoveAbove() ' *** ' The selected element is deleted ListView1.Remove(ListView1.Key) ' The internal cursor points after (***) to the element to be selected after deletion. ' If there is still an upper element after deletion, the external cursor is placed on it -> it is marked. If ListView1.Item Then ListView1.Key = ListView1.Item.Key ' Otherwise, the internal cursor is set to the first entry, if it exists and then marks the top element. Else If Not ListView1.MoveFirst() Then ListView1.Key = ListView1.Item.Key Endif End
Das Steuerelement ListView in gb.qt4 (→ gb.gui.base) implementiert eine Liste mit auswählbaren Elementen.
Die Klasse ListView verfügt u.a. über diese Eigenschaften:
Eigenschaft | Datentyp | Beschreibung |
---|---|---|
Current | _TreeView_Item | Gibt das aktuelle Element zurück. Im Allgemeinen ist es das Element, das den Fokus hat oder das markiert ist (Single-Selection-Modus). |
Item | _TreeView_Item | Gibt das Element zurück, auf das der interne Zeiger zeigt oder NULL, wenn der interne Zeiger nicht erreichbar ist. |
Count | Integer | Gibt die Anzahl aller Elemente in der ListView zurück. |
Editable | Boolean | Gibt an, ob die Elemente standardmäßig editierbar sind. Das heißt, ob sie vom Benutzer umbenannt werden können, wenn er auf sie klickt. |
Key | String | Gibt den Key des aktuellen Elements zurück. |
Mode | Integer | Setzt den Auswahlmodus oder gibt den Auswahlmodus zurück. Die verwendeten Konstanten sind: Single, None oder Multiple. |
Selection | String[ ] | Zurückgegeben werden die Keys aller markierten Elemente in einem String-Array (Multi-Selection-Modus). |
Sorted | Boolean | Zeigt an oder legt fest, ob die Elemente in der ListView sortiert angezeigt werden. |
Tabelle 17.4.0.1.1 : Ausgewählte Eigenschaften der Klasse ListView
Die Klasse ListView besitzt diese ausgewählten Methoden:
Methode | Rückgabetyp | Beschreibung |
---|---|---|
Exist ( Key As String ) | Boolean | Zurückgegeben wird der Wert True, wenn das Element mit dem angegebenen Key existiert. |
Add ( Key As String, Text As String [ , Picture As Picture, After As String ] ) | _TreeView_Item | Fügt ein Element in die ListView ein. |
MoveBack() | Boolean | Wenn eine der anderen Move-Methoden den Wert True zurück gibt, so können Sie diese Methode benutzen, um den internen Zeiger an seine ursprüngliche Position zu bewegen. Es wird True zurück gegeben, wenn der interne Zeiger nicht auf einer gültigen Position war. |
MoveBelow() | Boolean | Bewegt den internen Zeiger auf ein sichtbares Element unterhalb des aktuellen. Es wird True zurückgegeben, wenn die Verschiebung nicht ausgeführt werden kann. |
MoveTo ([ Key As String ]) | Boolean | Setzt den internen Cursor auf das Element in der ListView mit dem angegebenen Key. |
MoveBelow() | Boolean | Bewegt den internen Zeiger auf ein sichtbares Element unterhalb des aktuellen. Es wird True zurückgegeben, wenn die Verschiebung nicht ausgeführt werden kann. |
MoveCurrent() | Boolean | Setzt den internen Cursor auf das markierte Element in der ListView. Es wird True zurückgegeben, wenn die Verschiebung nicht ausgeführt werden kann. |
MoveFirst() | Boolean | Setzt den internen Cursor auf das erste Element in der ListView. Es wird True zurückgegeben, wenn die Liste leer ist. |
MoveLast() | Boolean | Setzt den internen Cursor auf das letzte Element in der ListView. Es wird True zurückgegeben, wenn die Liste leer ist. |
Rename() | Boolean | Startet das Umbenennen des markierten Elements. |
Tabelle 17.4.0.2.1 : Ausgewählte Methoden der Klasse ListView
Die Add(…)-Methode fügt ein neues Element in die ListView ein:
Function Add ( Key As String, Text As String [ , Picture As Picture, After As String ] ) As _TreeView_Item
Im folgenden Beispiel aus dem Projekt ListViewE werden die einzelnen Elemente in der ListView aus dem Inhalt einer Datei generiert:
Private Sub ListViewImport(Path As String, hListView As ListView) Dim cJSONCollection As JSONCollection Dim cCollection As JSONCollection Dim sKey, sText As String Dim picPicture As Picture hListView.Clear cJSONCollection = JSON.Decode(File.Load(Path), True) For Each cCollection In cJSONCollection sKey = cCollection["KEY"] sText = cCollection["TEXT"] picPicture = Picture[cCollection["PICTURE"]] hListView.Add(sKey, sText, picPicture, Null) ' Null, weil die Reihenfolge feststeht Next End ' ListViewExport(...)
Die Klasse ListView besitzt unter anderem die folgenden Ereignisse:
Ereignis | Beschreibung |
---|---|
Click() | Das Ereignis wird ausgelöst, wenn auf ein Element in der ListView geklickt wird. |
Select() | Das Ereignis wird ausgelöst, wenn sich die Auswahl in der ListView ändert. |
Compare ( Key As String, OtherKey As String ) | Dieses Ereignis wird ausgelöst, wenn zwei Elemente in der ListView verglichen werden. Das Ergebnis des Vergleichs wird in der Compare-Eigenschaft gespeichert. |
Tabelle 17.4.0.3.1 : Ausgewählte Ereignisse der Klasse ListView
Im nächsten Abschnitt werden Ihnen wichtige Eigenschaften und Methoden der virtuellen Klasse _TreeView_Item vorgestellt, da die Eigenschaften Current und Item vom Typ _TreeView_Item sind.
Die virtuelle Klasse _TreeView_Item repräsentiert einen Eintrag in einer ListView.
Eigenschaft | Datentyp | Beschreibung |
---|---|---|
Count | Integer | Gibt die Anzahl der Elemente zurück. |
Editable | Boolean | Gibt an, ob dieses Element editierbar ist. Das bedeutet, ob es vom Anwender umbenannt werden kann, wenn er darauf klickt. |
Key | String | Gibt den Key zurück. |
Picture | Picture | Gibt das Bild zurück oder setzt das Bild, das neben dem Element angezeigt wird. |
RichText | String | Gibt den RichText zurück oder setzt den RichText. |
Selected | Boolean | Gibt an, ob das Element ausgewählt ist. |
Text | String | Gibt den angezeigten Text auf dem Element zurück oder setzt den anzuzeigenden Text. |
Tabelle 17.4.0.5.1 : Ausgewählte Eigenschaften der virtuellen Klasse _ListView_Item
Seine Stärke als Steuerelement zur Anzeige von Daten zeigt die ListView erst dann so richtig, wenn Sie den Text als RichText erzeugen und zusammen mit einem Bild (Datentyp Picture) vor dem Text verwenden:
Abbildung 17.4.0.5.1: Verwendung von RichText in einer ListView
Methode | Beschreibung |
---|---|
Delete() | Löscht das (aktuelle) Element aus der ListView. |
Ensurevisible() | Sichert die (sichtbare) Anzeige des Elements in der ListView (Scroll-Effekt). |
Rename() | Startet das Umbenennen des markierten Elements. |
Tabelle 17.4.0.6.1 : Ausgewählte Methoden der virtuellen Klasse _TreeView_Item
Die Verwendung unterschiedlicher Methoden zeigt der folgende, ausführlich kommentierte Beispiel-Quelltext zum Löschen eines (markierten) Elements:
Public Sub btnDeleteElement_Click() ' Nur ein markiertes Element darf gelöscht werden If Not ListView1.Key Then Return ' Zuerst den internen Cursor auf das markierte Element setzen (Start-Position) ListView1.MoveCurrent() ' Dann den internen Cursor auf das nächste *sichtbare* - oberhalb liegenden - Element setzen ListView1.MoveAbove() ' *** ' Das markierte Element wird gelöscht ListView1.Remove(ListView1.Key) ' Der interne Cursor zeigt nach (***) auf das nach dem Löschen auszuwählende Element. ' Gibt es nach dem Löschen noch ein oberes Element, dann wird der externe Cursor darauf ' gesetzt → es wird markiert. If ListView1.Item Then ListView1.Key = ListView1.Item.Key ' Sonst wird der interne Cursor auf den ersten Eintrag gesetzt, ' wenn er existiert und dann das oberste Element markiert. Else If Not ListView1.MoveFirst() Then ListView1.Key = ListView1.Item.Key Endif End ' btnDeleteElement_Click()