Copyright © 2013, mgr inż. Janusz Bonarowski 1
Ć wiczenie VB6.00 Tablice, listy, instrukcja cyklu For...Next
Rys. 1. Propozycja formularza
Kod aplikacji
Private Sub btnDodaj_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnDodaj.Click If txtLiczba.Text = "" Then
MsgBox("Proszę wpisać liczbę", , "Brak danych") Exit Sub
End If
If Not IsNumeric(txtLiczba.Text) Then
MsgBox("Można wpisywać jedynie LICZBY", _ MsgBoxStyle.Critical, "Błąd danych") Exit Sub
End If
ListBox1.Items.Add(txtLiczba.Text) ListBox2.Items.Clear()
txtLiczba.Text = ""
txtLiczba.Focus() End Sub
Private Sub btnSuma_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSuma.Click Dim N, i As Integer
Dim suma As Single
N = ListBox1.Items.Count If N = 0 Then
MsgBox("Brak elementów do sumowania") Exit Sub
End If 'sumowanie suma = 0
For i = 1 To N
suma = suma + CSng(ListBox1.Items.Item(i - 1)) Next
lblSuma.Text = suma.ToString End Sub
Copyright © 2013, mgr inż. Janusz Bonarowski 2 Private Sub btnWyczyść_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnWyczyść.Click Dim tmp, N As Integer
N = ListBox1.Items.Count If N = 0 Then
MsgBox("Brak elementów do usunięcia") Exit Sub
End If
tmp = MsgBox("Czy rzeczywiście usunąć elementy", _
MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "Uwaga") If tmp = vbYes Then
ListBox1.Items.Clear() ListBox2.Items.Clear() lblSuma.Text = ""
End If End Sub
Private Sub btnUsuń_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnUsuń.Click Dim nr As Integer
If ListBox1.SelectedIndex <> -1 Then '___Usuń element z listy
nr = ListBox1.SelectedIndex ListBox1.Items.RemoveAt(nr) '--- ListBox2.Items.Clear()
lblSuma.Text = ""
Else
MsgBox("Proszę wybrać element do usunięcia") End If
End Sub
Copyright © 2013, mgr inż. Janusz Bonarowski 3 Private Sub btnRosnąco_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnRosnąco.Click Dim N, i, j As Integer
Dim tmp As Single
N = ListBox1.Items.Count If N = 0 Then
MsgBox("Brak elementów do sortowania", _
MsgBoxStyle.Question, "Sortowanie nie odbędzie się") Exit Sub
End If
Dim tabLista(N - 1) As Single 'Dekl. tablicy (N elementowej)
'Przekładanie liczb z listy do tablicy For i = 0 To N - 1
tabLista(i) = CSng(ListBox1.Items.Item(i)) Next i
'Sortowanie rosnąco (najmniejszy najwyżej) For i = 0 To N - 1
For j = 0 To N - 2 'For j=0 To N-2 - (i-1) If tabLista(j) > tabLista(j + 1) Then
'zamienić miejscami:
tmp = tabLista(j)
tabLista(j) = tabLista(j + 1) tabLista(j + 1) = tmp
End If Next j Next i
'Przekładanie z tablicy do listy ListBox2.Items.Clear()
For i = 0 To N - 1
ListBox2.Items.Add(CStr(tabLista(i))) Next i
End Sub
Copyright © 2013, mgr inż. Janusz Bonarowski 4 Private Sub btnMalejąco_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnMalejąco.Click Dim N, i, j As Integer
Dim tmp As Single
N = ListBox1.Items.Count If N = 0 Then
MsgBox("Brak elementów do sortowania", _
MsgBoxStyle.Question, "Sortowanie nie odbędzie się") Exit Sub
End If
Dim tabLista(N - 1) As Single 'Tablica N elementowa.
'Przekładanie liczb z listy do tablicy For i = 0 To N - 1
tabLista(i) = CSng(ListBox1.Items.Item(i)) Next i
'Sortowanie malejąco (największy najwyżej) For i = 0 To N - 1
For j = 0 To N - 2 'For j=0 To N-2 - (i-1) If tabLista(j) < tabLista(j + 1) Then 'zamienić miejscami:
tmp = tabLista(j)
tabLista(j) = tabLista(j + 1) tabLista(j + 1) = tmp
End If Next j Next i
'Przekładanie z tablicy do listy ListBox2.Items.Clear()
For i = 0 To N - 1
ListBox2.Items.Add(CStr(tabLista(i))) Next i
End Sub