Imports System.IO Public Class StringTable ' return random index from 0 to num-1 Public Function randidx(ByVal num As Integer) As Integer Return Int(num * Rnd()) End Function ' add a new string Private Sub butAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butAdd.Click If (txtString.Text.Length > 0) And Not lstBox.Items.Contains(txtString.Text) Then lstBox.SelectedIndex = lstBox.Items.Add(txtString.Text) txtString.Clear() txtString.Focus() End If End Sub ' find a string Private Sub butFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butFind.Click If (txtString.Text.Length > 0) And lstBox.Items.Contains(txtString.Text) Then Dim idx As Integer = lstBox.Items.IndexOf(txtString.Text) lstBox.SelectedIndex = idx End If End Sub ' delete a string Private Sub butDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butDelete.Click If lstBox.SelectedIndex >= 0 Then lstBox.Items.RemoveAt(lstBox.SelectedIndex) End If End Sub ' sort the strings Private Sub butSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSort.Click lstBox.Sorted = True End Sub ' randomise the strings Private Sub butRand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butRand.Click lstBox.Sorted = False For i As Integer = 1 To 10 * lstBox.Items.Count Dim idx1 As Integer = randidx(lstBox.Items.Count) Dim idx2 As Integer = randidx(lstBox.Items.Count) ' MsgBox("idx1=" & idx1 & " idx2=" & idx2) Dim tmp As String = lstBox.Items.Item(idx1) lstBox.Items.Item(idx1) = lstBox.Items.Item(idx2) lstBox.Items.Item(idx2) = tmp Next End Sub ' load strings from file Private Sub butLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butLoad.Click If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then lstBox.Items.Clear() Dim sr As StreamReader = New StreamReader(OpenFileDialog1.FileName) While Not sr.EndOfStream lstBox.Items.Add(sr.ReadLine()) End While sr.Close() End If End Sub ' save strings to file Private Sub butSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSave.Click If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then Dim sw As StreamWriter = New StreamWriter(SaveFileDialog1.FileName) For i As Integer = 0 To lstBox.Items.Count - 1 sw.WriteLine(lstBox.Items.Item(i)) Next sw.Close() End If End Sub End Class