Table of Contents

17.3 ListBox

The component ListBox (gb. qt4) implements a list of selectable text elements:

LB1

Figure 17.3.1: A double click on the text element starts the selected WebSite here

17.3.1 ListBox - Properties and Methods

Selected properties and methods of a ListBox are listed and described in a table:

PropertyTypeDescription
Count IntegerIndicates the number of elements in the list box.
CurrentListBox. ItemReturns the current selected item in the list box.
IndexIntegerSpecifies the index of the currently selected element in the list box.
ListArraySaves the contents of the list box in a string array or fills the list box list with the contents of a string array.
ModeIntegerDetermines or sets the selection mode (constants: Select.None (0), Select.Single (1) or Select.Multiple (2).
SortedBooleanDetermines whether the elements in the list box are sorted and displayed (!). The list of (text) elements is not sorted automatically!
TextStringReturns the selected text in the list box or sets the text.

Table 17.3.1.1: Selected properties of the ListBox component

The class ListBox.Item represents:

Example 1: A TextBox is assigned the selected text in a ListBox:

txtElement.Text = listBoxURL.Current.Text

Example 2: Only selected text elements from a ListBox are copied to an array:

lsbListe.Mode =  Select.Multiple
…
For iIndex = 0 To lsbListe.Count - 1
  If lsbListe[iIndex].Selected = True Then arrayListe.Add(lsbListe[iIndex].Text)
Next ' iIndex

A description of selected methods of a ListBox can be found in the following table:

MethodDescription
. Add (Item As String[, Index As Integer]Inserts an item into the ListBox list. If the optional value Index is set, the insertion takes place at the position defined by the index, otherwise at the end of the list.
.Clear()Delets the contents of the ListBox list.
.Find(Item As String)Finds an element in the ListBox and returns the corresponding index (type Integer) or -1 if the element is not found in the ListBox.
.Popup()Assigns a self-defined popup menu to a ListBox.
.Remove(Index As Integer)Deletes the indexed element from the ListBox list.
.SelectAll()Selects and highlights all elements in the ListBox depending on the mode property.
.Unselect()Cancels the selection of the selected elements in the ListBox.

Table 17.3.1.2: Overview of selected methods of the ListBox class

17.3.2 Events ListBox

Special events of the ListBox component and additional comments can be found here:

EventDescription
ActivateIs triggered when the user double-clicks on a text element in the ListBox.
ClickIs triggered when an element in the ListBox is clicked and thus selected.
SelectIs triggered when the selection changes.

Table 17.3.2.1: Overview of the 3 events of the component ListBox

17.3.3 Project ListBox

The ListBox used in the presented project is used to display a website after clicking on a text element. The project is characterized by the following features:

LB2

Figure 17.3.3.1: ListBox with context menu

Only if you have set the ListBox.Mode property to Select.Multiple, it is possible to select several text elements with the mouse while holding down the CTRL key. Figure 17.3.3.3.1 to select all elements in the ListBox.

LB3

The source text is completely specified. When reading, you can be sure that almost all properties, methods and events from the above three tables have been used.

[1] ' Gambas class file
[2] 
[3] Private $hFile As File
[4] Private $sFilePath As String 
[5] Private $sProjectPath As String = Application.Path
[6] Private $iCount As Integer = 1
[7] 
[8] Public Sub Form_Open()
[9]   
[10]   FMain.Center
[11]   FMain.Resizable = False  
[12]   
[13]   $sFilePath = $sProjectPath &/ "url.lis"
[14]   If Exist($sFilePath) Then
[15]      ImportLBList($sFilePath, lsbURL)
[16]   Else
[17]      lsbURL.Add("http://www.gambas-buch.de") 
[18]   Endif ' Exist($sFilePath) ?
[19]   
[20]   lsbURL.Sorted = False ' Notwendig wegen UP und DOWN
[21]   lsbURL[0].Selected = True
[22]   Refresh
[23]   
[24] End ' Form_Open()
[25] 
[26] Public Sub btnInsertElement_Click()
[27]   lsbURL.Add("Text | Link" & CStr($iCount))
[28]   Inc $iCount
[29]   Refresh
[30]   lsbURL.Index = lsbURL.Count - 1
[31]   Object.Lock(txtElement)
[32]     txtElement.Text = lsbURL.Current.Text
[33]   Object.Unlock(txtElement)
[34]   txtElement.SetFocus
[35]   txtElement.SelectAll
[36] End ' btnInsertSenderURL_Click()
[37] 
[38] Public Sub btnDeleteElement_Click()
[39]   Dim iCurrentIndex As Integer
[40]   
[41]   iCurrentIndex = lsbURL.Index
[42]   If lsbURL.Index < 0 Then Return  
[43]   
[44]   If lsbURL.Count > 1 Or lsbURL.Count <= lsbURL.Count - 2 Then
[45]      If Message.Question("Element löschen?", "Löschen", "Abbrechen") <> 1 Then Return
[46]      lsbURL.Remove(lsbURL.Index)
[47]   Else
[48]   If Message.Question("Letztes Element löschen?", "Löschen", "Abbrechen") <> 1 Then Return
[49]      lsbURL.Remove(lsbURL.Index)
[50]   Endif ' lsbURL.Count > 1 ?
[51]   
[52]   If lsbURL.Index >= lsbURL.Count Then Dec lsbURL.Index
[53]   txtElement.Text = lsbURL[lsbURL.Index].Text
[54]   Refresh
[55] 
[56] End '  btnDeleteRadioURL_Click()
[57] 
[58] Public Sub btnUp_Click()
[59]   Dim iCurrentIndex As Integer
[60] 
[61]   iCurrentIndex = lsbURL.Index
[62]   If iCurrentIndex > 0 Then
[63]      Swap lsbURL[iCurrentIndex].Text, lsbURL[iCurrentIndex - 1].Text
[64]      lsbURL.Index = iCurrentIndex - 1
[65]   Endif ' iCurrentIndex > 0 ?
[66] 
[67] End ' btnUp_Click()
[68] 
[69] Public Sub btnDown_Click()
[70]   Dim iCurrentIndex As Integer
[71] 
[72]   iCurrentIndex = lsbURL.Index
[73]   If iCurrentIndex < (lsbURL.Count - 1) Then
[74]      Swap lsbURL[iCurrentIndex].Text, lsbURL[iCurrentIndex + 1].Text
[75]      lsbURL.Index = iCurrentIndex + 1
[76]   Endif ' iCurrentIndex < (lstValue.Count – 1) ?
[77] 
[78] End ' btnDown_Click()
[79] 
[80] Public Sub btnDeleteList_Click()
[81]   If Message.Question("Komplette Liste löschen?", "Löschen", "Abbrechen") <> 1 Then Return
[82]   lsbURL.Clear
[83]   Refresh
[84] End ' btnDeleteList_Click()
[85] 
[86] Public Sub cboxMultiSelect_Click()
[87]   If cboxMultiSelect.Value = cboxMultiSelect.True Then
[88]      lsbURL.Mode = Select.Multiple
[89]   Else
[90]      lsbURL.Mode = Select.Single
[91]      If lsbURL.Count > 0 Then lsbURL[0].Selected = True
[92]   Endif ' cboxMultiSelect.Value = cboxMultiSelect.True ?
[93] End ' cboxMultiSelect_Click()
[94] 
[95] Public Sub btnCopyList_Click()
[96]   Dim iIndex As Integer
[97]   Dim sElement As String
[98]   Dim aListe2 As New String[]
[99]   
[100]   If cboxMultiSelect.Value = cboxMultiSelect.True Then 
[101]      For iIndex = 0 To lsbURL.Count - 1
[102]        If lsbURL[iIndex].Selected = True Then
[103]           aListe2.Add(lsbURL[iIndex].Text)
[104]        Endif ' lsbURL[iIndex].Selected = True ?
[105]      Next ' iIndex
[106]   Else
[107]      Message.Info("MultiSelect nicht markiert?\nKein Element markiert?")
[108]      Return
[109]   Endif ' cboxMultiSelect.Value = cboxMultiSelect.True ?
[110] 
[111] '  For Each sElement In aListe2 ' Nur zur Kontrolle ...
[112] '      Print sElement
[113] '  Next ' sElement
[114]   
[115] End ' btnCopyList_Click()
[116] 
[117] Public Sub lsbURL_Click()
[118]   If Not lsbURL.Current Then Return
[119]   Object.Lock(txtElement)
[120]     txtElement.Text = lsbURL.Current.Text
[121]   Object.Unlock(txtElement)
[122]   txtElement.SetFocus
[123]   txtElement.SelectAll
[124] End ' lsbList_Click()
[125] 
[126] Public Sub lsbURL_DblClick()
[127]   Dim aMatrix As String[]
[128] 
[129]   aMatrix = Split(lsbURL.Current.Text, "|")
[130] 
[131] If Trim(Lower(aMatrix[1])) Begins "http" Or Trim(Lower(aMatrix[1])) Begins "https" Then
[132]    Try Desktop.Open(Trim(aMatrix[1]))
[133] Else
[134]    Message.Error("In der URL fehlt das Protokoll 'http' oder 'https'!")
[135]    Return
[136] Endif ' Protokoll ok ?
[137]   
[138] End ' lsbURL_DblClick()
[139] 
[140] Public Sub btnEnde_Click()  
[141]   FMain.Close(True)
[142] End ' btnEnde_Click()
[143] 
[144] '*************************************************************************************
[145] 
[146] Public Sub txtElement_Change()
[147]   If lsbURL.Current Then
[148]      If lsbURL.List.Exist(txtElement.Text) Then
[149]         Message.Error("Dieser Eintrag existiert bereits.")
[150]      Else
[151]         lsbURL.Current.Text = txtElement.Text
[152]      Endif
[153]   Endif ' lsbCBList.Current ?
[154] End ' txtElement_Change()
[155] 
[156] Private Sub Refresh()
[157]   Dim bEnabled As Boolean
[158]   
[159]   bEnabled = lsbURL.Count  
[160]   txtElement.Enabled = bEnabled
[161]   btnDeleteElement.Enabled = bEnabled
[162]   btnUp.Enabled = bEnabled
[163]   btnDown.Enabled = bEnabled
[164]   btnDeleteList.Enabled = bEnabled  
[165]   If Not bEnabled Then txtElement.Clear
[166]   
[167] End ' Refresh()
[168] 
[169] ' Diese Routine lädt die ListBox-Liste aus einer binären, gambas-spezifischen Datei.
[170] ' Parameter:
[171] ' sPath - Datei-Pfad zur ausgewählten Import-Datei.
[172] ' lsb_URL - Referenz auf die ausgewählte ListBox auf der Form.
[173] Public Sub ImportLBList(sPath As String, lsb_URL As ListBox) 
[174]   $hFile = Open sPath For Read
[175]     lsb_URL.List = Read #$hFile As Array
[176]   Close #$hFile  
[177] Catch
[178] Message.Error("Der Daten-Import war fehlerhaft!" & gb.NewLine & "Fehler: " & Error.Text)
[179] End ' ImportLBList(...)
[180] 
[181] ' Diese Routine speichert eine ListBox-Liste in eine binäre, gambas-spezifische Datei.
[182] ' Parameter:
[183] ' sPath - Datei-Pfad zur ausgewählten Export-Datei.
[184] ' lsb_URL - Referenz auf die ausgewählte ListBox auf der Form.
[185] Public Sub ExportLBList(sPath As String, lsb_URL As ListBox)
[186]   If lsb_URL.Count = 0 Then
[187]      Return
[188]   Else
[189]      $hFile = Open sPath For Write Create
[190]      Write #$hFile, lsb_URL.List As Array
[191]      Close #$hFile
[192] Catch 
[193] Message.Error("Der Daten-Export war fehlerhaft!" & gb.NewLine & "Fehler: " & Error.Text)  
[194]   Endif ' lsb_URL.Count = 0 ?
[195] End ' ExportLBList(...)
[196] 
[197] Public Sub lsbURL_Menu()
[198]   Dim mnuContextLB As Menu
[199]   Dim mnuMenuItem1, mnuMenuItem2 As Menu
[200] 
[201] ' Es wird ein neues Menü-Objekt für die Liste erzeugt:
[202]   mnuContextLB = New Menu(FMain, False)
[203] ' 1. Unter-Menü im Menü mnuContextLB
[204]   mnuMenuItem1 = New Menu(mnuContextLB) As "mnuEditLB"
[205]   mnuMenuItem1.Text = "ListBox-Hilfe aufrufen"
[206]   mnuMenuItem1.Picture = Stock["help"]
[207] ' 2. Unter-Menü im Menü mnuContextLB
[208]   mnuMenuItem2 = New Menu(mnuContextLB) As "mnuSelectAll"
[209]   mnuMenuItem2.Text = "Alle Elemente markieren"
[210]   mnuMenuItem2.Picture = Stock["select-all"]
[211]   mnuContextLB.Popup ' Das mnuContextLB wird der Liste als PopUp-Menü zugewiesen
[212] 
[213] End ' lsbURL_Menu()
[214] 
[215] ' 2 Aktionen, wenn das Kontextmenü der ListBox ausgewählt wurde:
[216] Public Sub mnuEditLB_Click()
[217]   Message.Info("Ich bin die kleine ListBox-Hilfe ...")
[218] End ' mnuEditLB_Click()
[219] 
[220] Public Sub mnuSelectAll_Click()  
[221]   If cboxMultiSelect.Value = cboxMultiSelect.True Then
[222]      lsbURL.SelectAll
[223]      lsbURL.Refresh
[224]      Wait
[225]   Else
[226]      Return
[227]   Endif ' cboxMultiSelect.Value = cboxMultiSelect.True ?
[228] End ' mnuSelectAll()
[229] 
[230] Public Sub Form_Close()
[231]   $sFilePath = $sProjectPath &/ "url.lis"
[232]   ExportLBList($sFilePath, lsbURL)
[233] End ' Form_Close()

Chapter 17.4 ListView introduces you to the WebRadio project, in which you no longer have to separate a text element into two parts by a separator - in the above-mentioned project it was the pipe character - but use the link address as a key and only see the name of the web radio station in the ListView. The Web radio station is called after selecting the displayed name of the Web radio station in the ListView using the corresponding key. Only 2 informatic flies with one stone - but still…..

Download