📄 form1.vb
字号:
Option Strict On
Public Class ArraySearchForm
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents bttnPopulate As System.Windows.Forms.Button
Friend WithEvents bttnSearch As System.Windows.Forms.Button
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ListBox1 = New System.Windows.Forms.ListBox()
Me.bttnPopulate = New System.Windows.Forms.Button()
Me.bttnSearch = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'ListBox1
'
Me.ListBox1.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
Me.ListBox1.ItemHeight = 14
Me.ListBox1.Location = New System.Drawing.Point(8, 8)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(200, 312)
Me.ListBox1.TabIndex = 0
'
'bttnPopulate
'
Me.bttnPopulate.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
Me.bttnPopulate.Location = New System.Drawing.Point(216, 8)
Me.bttnPopulate.Name = "bttnPopulate"
Me.bttnPopulate.Size = New System.Drawing.Size(120, 40)
Me.bttnPopulate.TabIndex = 1
Me.bttnPopulate.Text = "Populate Array"
'
'bttnSearch
'
Me.bttnSearch.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
Me.bttnSearch.Location = New System.Drawing.Point(216, 64)
Me.bttnSearch.Name = "bttnSearch"
Me.bttnSearch.Size = New System.Drawing.Size(120, 40)
Me.bttnSearch.TabIndex = 2
Me.bttnSearch.Text = "Search Array"
'
'ArraySearchForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(346, 325)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.bttnSearch, Me.bttnPopulate, Me.ListBox1})
Me.Name = "ArraySearchForm"
Me.Text = "ArraySearch"
Me.ResumeLayout(False)
End Sub
#End Region
Dim words() As String
Private Sub bttnPopulate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnPopulate.Click
Dim wordLen As Integer
' number of words to place on the list
Dim Nwords As Integer = 999
' The words array holds the random words
ReDim words(Nwords)
Dim ascChar As Integer
Dim rnd As System.Random
rnd = New System.Random()
Dim rndChar As Char
Dim thisWord As String
Dim word, chr As Integer
Dim T1, T2 As System.DateTime
T1 = Now
' Generate random words and populate list
For word = 0 To Nwords
wordLen = CInt(rnd.Next(3, 15))
thisWord = ""
For chr = 0 To wordLen
rndChar = ChrW(65 + rnd.Next(0, 25))
thisWord = thisWord & rndChar
Next
words(word) = thisWord
'ListBox1.Items.Add(thisWord)
Next
' show how long it took to generate words
T2 = Now
MsgBox("It took " & ((T2.Ticks - T1.Ticks) / 10000).ToString & " msecs to create array")
' now sort the list
T1 = Now
ListBox1.Items.Clear()
System.Array.Sort(words)
' show how long it took to sort the array with the words
T2 = Now
MsgBox("It took " & ((T2.Ticks - T1.Ticks) / 10000).ToString & " msecs to sort array")
T1 = Now
' now add the words to the ListBox control
For word = 0 To Nwords
ListBox1.Items.Add(words(word))
Next
' and show how long it took to populate the control
T2 = Now
MsgBox("It took " & ((T2.Ticks - T1.Ticks) / 10000).ToString & " msecs to refresh the List Box")
End Sub
Private Sub bttnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnSearch.Click
Dim srchWord As String ' the word to search for
Dim wordIndex As Integer ' the index of the word in the list (if found)
srchWord = InputBox("Enter word to search for")
' perform a binary search to locate the word, because the list is sorted
wordIndex = System.Array.BinarySearch(words, srchWord)
Console.WriteLine(wordIndex)
If wordIndex >= 0 Then ' Zero or positive value indicates an exact match
MsgBox("Words(" & wordIndex.ToString & ") = " & words(wordIndex), , "EXACT MATCH")
ListBox1.TopIndex = wordIndex
ListBox1.SelectedIndex = wordIndex
Else
' a negative value indicates a near match (the first item after the location
' of the item we search for, if it were in the list
MsgBox("Words(" & (-wordIndex - 1).ToString & ") = " & words(-wordIndex - 1), , "NEAR MATCH")
ListBox1.TopIndex = -wordIndex - 1
ListBox1.SelectedIndex = -wordIndex - 1
End If
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -