In addition to using properties of the ColumnView control, the project demonstrates the sorting of the data and especially the use of the various Move() methods.
Figure 17.5.1.1: Program window - sorting by last name switched on
You can create new (random) data (12 elements) at any time. It is advantageous if you see an element as a row in a ColumnView.
Public Sub btnNewData_Click() covData.Clear() ' Delete content of the ColumnView. Does not apply to the optional header line! SetData(12) ' 12 Inserting new elements with random data ' Marking of the first element - if at least one exists If Not covData.MoveFirst() Then covData.Key = covData.Item.Key txbCurrentKey.Text = covData.Item.Key End Private Sub SetData(iCount As Integer) Dim i As Integer Dim aFirstNames As String[] = ["Hans", "Maria", ..., "Robert", "Stefan", "Emma", "Yvonne", "Claus"] Dim aSurNames As String[] = ["Meyer", "Lehmann", ..., "Müller", "Grahn", "Kaiser", "Vogt", "Zechlin"] Dim aPictures As String[] = ["led_green16.png", "led_red16.png", "led_blue16.png"] For i = 1 To iCount covData.Add(Str(i), Str(i), Picture[aPictures[Rand(0, aPictures.Max)]]) ' 1. Spalte + Bild covData[Str(i)][1] = aFirstNames[Rand(0, aFirstNames.Max)] ' 2. Spalte covData[Str(i)][2] = aSurNames[Rand(0, aSurNames.Max)] ' 3. Spalte covData[Str(i)][3] = Format$(Date(CFloat(Now() - Rnd(7000, 8200))), "dd.mm.yyyy") ' 4. Spalte Next End
The data can also be sorted out (→ CheckBox), whereby the data are sorted first after the 3rd column 'Last name'. Afterwards, all other columns can be sorted as well.
Public Sub ckboxSorting_MouseDown() txbCurrentKey.Text = covData.Item.Key If ckboxSorting.Value = True Then covData.Sorted = False Else covData.Sorted = True ' It's to be sorted! covData.Columns.Ascending = True ' A → Z covData.Columns.Sort = 2 ' Sort by last name Endif End
You can immediately move the internal pointer to the first or last element if there is at least one element - which will be checked. The first or last element is then selected so that you can see the current end position:
Public Sub btnFirst_Click() If covData.Count > 0 Then ' There is at least one element... covData.MoveFirst() txbCurrentKey.Text = covData.Item.Key ' Select element (ColumnView without Focus: Mark gray) covData.Key = covData.Item.Key Endif End Public Sub btnLast_Click() If covData.Count > 0 Then ' There is at least one element... covData.MoveLast() txbCurrentKey.Text = covData.Item.Key ' Select element (ColumnView without Focus: Mark gray) covData.Key = covData.Item.Key Endif End
From any position you can move the internal pointer step by step to the first or last element:
Public Sub btnUp_Click() If covData.Count > 0 Then ' There is at least one element... If Not covData.MoveAbove() Then txbCurrentKey.Text = covData.Item.Key & " : " & covData.Item[1] covData.Key = covData.Item.Key Else covData.MoveFirst() Endif Endif End Public Sub btnDown_Click() If covData.Count > 0 Then ' There is at least one element... If Not covData.MoveBelow() Then txbCurrentKey.Text = covData.Item.Key & " : " & covData.Item[1] covData.Key = covData.Item.Key Else covData.MoveLast() Endif Endif End
From any position you can move the internal pointer from the first to the last element (iteration). This procedure is well suited for reading data from a ColumnView or importing it into a ColumnView. Since the marking is switched off to demonstrate the independence of the internal pointer from the external (marking) pointer, you can recognize the current position during iteration in the text box (key and text of the first column):
Public Sub btnIterationDown_Click() Dim iLastKey As Integer If covData.Count > 0 Then ' There is at least one element... iLastKey = covData.Key ' Start-Key speichern für *** Endif ' Set the internal pointer to the topmost element, if possible If Not covData.MoveFirst() Then ' There is at least one element... Repeat ' ... then iteration of the top element txbCurrentKey.Text = covData.Item.Key & " : " & covData.Item[1] ' covData.Key = covData.Item.Key ' Mark element disabled Wait 0.4 ' Here are further instructions for processing data from the ColumnView: For example ' Print covData.Item[0]; gb.Tab; covData.Item[1]; gb.Tab; covData.Item[2]; gb.Tab; covData.Item[3] Until covData.MoveBelow() ' ... until the last element in the ColumnView is reached covData.MoveTo(iLastKey) ' *** Endif End
To fully specify the source code, you can see how the layout of the ColumnView is defined and start values are set:
Public Sub Form_Open() FMain.Resizable = False With covData ' Anzahl der Spalten festlegen .Columns.Count = 4 .Header = True ' Kopfzeile anzeigen ' Header-Spalten-Namen festlegen .Columns[0].Text = ("RGB") .Columns[1].Text = ("Vorname") .Columns[2].Text = ("Nachname") .Columns[3].Text = "Geburtsdatum" ' Spalten-Weite festlegen .Columns[0].Width = 25 .Columns[1].Width = 130 .Columns[2].Width = 130 .Columns[3].Width = 150 ' Spalten-Ausrichtung festlegen .Columns[0].Alignment = Align.Center .Columns[1].Alignment = Align.Normal .Columns[2].Alignment = Align.Normal .Columns[3].Alignment = Align.Center .Mode = Select.Single .Sorted = False ' Es soll *nicht* sortiert werden! .AutoResize = True .Resizable = True End With ckboxSorting.Value = ckboxSorting.False SetData(12) ' Markierung des 7. Elementes ' If covData.Count > 0 Then ' Es existiert mindestens ein (oberes) Element ... If Not covData.MoveFirst() Then ' Es existiert mindestens ein (oberes) Element ... If covData.MoveTo(7) = True Then ' Wenn ein Fehler auftrat ... covData.MoveBack() ' ... dann zurück zum Ausgangselement covData.Key = covData.Item.Key ' Element markieren Else ' Element markieren (ColumnView hat Focus → Markierung hellgrün (Mint 17.3)) covData.Key = covData.Item.Key Endif Endif End Public Sub ckboxSorting_MouseDown() txbCurrentKey.Text = covData.Item.Key If ckboxSorting.Value = True Then covData.Sorted = False Else covData.Sorted = True ' Es soll sortiert werden! covData.Columns.Ascending = True ' A → Z covData.Columns.Sort = 2 ' Sortieren nach Nachname Endif End Public Sub Form_Close() FMain.Close() End
Das Projekt demonstriert neben der Verwendung von Eigenschaften des Steuerelements ColumnView die Sortierung der Daten und besonders den Einsatz der diversen Move()-Methoden.
Abbildung 17.5.1.1: Programmfenster – Sortierung nach Nachnamen eingeschaltet
Sie können jederzeit neue (Zufalls)Daten erzeugen (12 Elemente). Es ist von Vorteil, wenn Sie ein Element als Zeile in einer ColumnView auffassen.
Public Sub btnNewData_Click() covData.Clear() ' Inhalt der ColumnView löschen. Gilt nicht für die optionale Kopf-Zeile! SetData(12) ' 12 Elemente mit Zufallsdaten neu einfügen ' Markierung des ersten Elementes - wenn mindestens eins existiert If Not covData.MoveFirst() Then covData.Key = covData.Item.Key txbCurrentKey.Text = covData.Item.Key End Private Sub SetData(iCount As Integer) Dim i As Integer Dim aFirstNames As String[] = ["Hans", "Maria", ..., "Robert", "Stefan", "Emma", "Yvonne", "Claus"] Dim aSurNames As String[] = ["Meyer", "Lehmann", ..., "Müller", "Grahn", "Kaiser", "Vogt", "Zechlin"] Dim aPictures As String[] = ["led_green16.png", "led_red16.png", "led_blue16.png"] For i = 1 To iCount covData.Add(Str(i), Str(i), Picture[aPictures[Rand(0, aPictures.Max)]]) ' 1. Spalte + Bild covData[Str(i)][1] = aFirstNames[Rand(0, aFirstNames.Max)] ' 2. Spalte covData[Str(i)][2] = aSurNames[Rand(0, aSurNames.Max)] ' 3. Spalte covData[Str(i)][3] = Format$(Date(CFloat(Now() - Rnd(7000, 8200))), "dd.mm.yyyy") ' 4. Spalte Next End
Die Daten können auch sortiert ausgegeben werden (→ CheckBox), wobei die Daten zuerst nach der 3. Spalte 'Nachname' sortiert werden. Anschließend können alle weiteren Spalten ebenso sortiert werden.
Public Sub ckboxSorting_MouseDown() txbCurrentKey.Text = covData.Item.Key If ckboxSorting.Value = True Then covData.Sorted = False Else covData.Sorted = True ' Es soll sortiert werden! covData.Columns.Ascending = True ' A → Z covData.Columns.Sort = 2 ' Sortieren nach Nachname Endif End
Den internen Zeiger können Sie sofort zum ersten oder zum letzten Element bewegen, sofern es mindestens ein Element gibt – was geprüft wird. Damit Sie die aktuelle End-Position sehen können, werden das erste oder das letzte Element anschließend markiert:
Public Sub btnFirst_Click() If covData.Count > 0 Then ' Es existiert mindestens ein Element ... covData.MoveFirst() txbCurrentKey.Text = covData.Item.Key ' Element markieren (ColumnView ohne Focus: Markierung grau) covData.Key = covData.Item.Key Endif End Public Sub btnLast_Click() If covData.Count > 0 Then ' Es existiert mindestens ein Element ... covData.MoveLast() txbCurrentKey.Text = covData.Item.Key ' Element markieren (ColumnView ohne Focus: Markierung grau) covData.Key = covData.Item.Key Endif End
Von jeder beliebigen Position aus können Sie den internen Zeiger schrittweise zum ersten oder zum letzten Element bewegen:
Public Sub btnUp_Click() If covData.Count > 0 Then ' Existiert mindestens ein Element ... If Not covData.MoveAbove() Then txbCurrentKey.Text = covData.Item.Key & " : " & covData.Item[1] covData.Key = covData.Item.Key Else covData.MoveFirst() Endif Endif End Public Sub btnDown_Click() If covData.Count > 0 Then ' Existiert mindestens ein Element ... If Not covData.MoveBelow() Then txbCurrentKey.Text = covData.Item.Key & " : " & covData.Item[1] covData.Key = covData.Item.Key Else covData.MoveLast() Endif Endif End
Von jeder beliebigen Position aus können Sie den internen Zeiger vom ersten bis zum letzten Element bewegen (Iteration). Diese Prozedur ist gut geeignet, um Daten aus einer ColumnView auszulesen oder in eine ColumnView einzulesen. Da die Markierung ausgeschaltet ist, um die Unabhängigkeit des internen Zeigers vom äußeren (Markierungs-)Zeiger zu demonstrieren, erkennen Sie die aktuelle Position bei der Iteration in der Textbox (Key und Text der 1. Spalte):
Public Sub btnIterationDown_Click() Dim iLastKey As Integer If covData.Count > 0 Then ' Es existiert mindestens ein Element ... iLastKey = covData.Key ' Start-Key speichern für *** Endif ' Den internen Zeiger auf das oberste Element setzen, wenn das möglich ist If Not covData.MoveFirst() Then ' Wenn mindestens ein Element existiert ... Repeat ' ... dann Iteration vom obersten Element txbCurrentKey.Text = covData.Item.Key & " : " & covData.Item[1] ' covData.Key = covData.Item.Key ' Element markieren abgeschaltet Wait 0.4 ' Hier weitere Anweisungen zur Verarbeitung von Daten aus der ColumnView: Zum Beispiel ' Print covData.Item[0]; gb.Tab; covData.Item[1]; gb.Tab; covData.Item[2]; gb.Tab; covData.Item[3] Until covData.MoveBelow() ' ... bis das letzte Element in der ColumnView erreicht ist covData.MoveTo(iLastKey) ' *** Endif End
Um den Quelltext vollständig anzugeben, wird u.a. gezeigt, wie das Layout der ColumnView festgelegt wird und Startwerte gesetzt werden:
Public Sub Form_Open() FMain.Resizable = False With covData ' Anzahl der Spalten festlegen .Columns.Count = 4 .Header = True ' Kopfzeile anzeigen ' Header-Spalten-Namen festlegen .Columns[0].Text = ("RGB") .Columns[1].Text = ("Vorname") .Columns[2].Text = ("Nachname") .Columns[3].Text = "Geburtsdatum" ' Spalten-Weite festlegen .Columns[0].Width = 25 .Columns[1].Width = 130 .Columns[2].Width = 130 .Columns[3].Width = 150 ' Spalten-Ausrichtung festlegen .Columns[0].Alignment = Align.Center .Columns[1].Alignment = Align.Normal .Columns[2].Alignment = Align.Normal .Columns[3].Alignment = Align.Center .Mode = Select.Single .Sorted = False ' Es soll *nicht* sortiert werden! .AutoResize = True .Resizable = True End With ckboxSorting.Value = ckboxSorting.False SetData(12) ' Markierung des 7. Elementes ' If covData.Count > 0 Then ' Es existiert mindestens ein (oberes) Element ... If Not covData.MoveFirst() Then ' Es existiert mindestens ein (oberes) Element ... If covData.MoveTo(7) = True Then ' Wenn ein Fehler auftrat ... covData.MoveBack() ' ... dann zurück zum Ausgangselement covData.Key = covData.Item.Key ' Element markieren Else ' Element markieren (ColumnView hat Focus → Markierung hellgrün (Mint 17.3)) covData.Key = covData.Item.Key Endif Endif End Public Sub ckboxSorting_MouseDown() txbCurrentKey.Text = covData.Item.Key If ckboxSorting.Value = True Then covData.Sorted = False Else covData.Sorted = True ' Es soll sortiert werden! covData.Columns.Ascending = True ' A → Z covData.Columns.Sort = 2 ' Sortieren nach Nachname Endif End Public Sub Form_Close() FMain.Close() End