📄 linearsearchtest.vb
字号:
' Fig. 7.13: LinearSearchTest.vb
' Linear search of an array
Public Class FrmLinearSearchTest
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 cmdSearch As System.Windows.Forms.Button
Friend WithEvents cmdCreate As System.Windows.Forms.Button
Friend WithEvents lblEnter As System.Windows.Forms.Label
Friend WithEvents lblResult As System.Windows.Forms.Label
Friend WithEvents txtInput As System.Windows.Forms.TextBox
Friend WithEvents txtData As System.Windows.Forms.TextBox
'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.txtData = New System.Windows.Forms.TextBox()
Me.lblEnter = New System.Windows.Forms.Label()
Me.cmdCreate = New System.Windows.Forms.Button()
Me.txtInput = New System.Windows.Forms.TextBox()
Me.cmdSearch = New System.Windows.Forms.Button()
Me.lblResult = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'txtData
'
Me.txtData.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.txtData.Location = New System.Drawing.Point(8, 48)
Me.txtData.Multiline = True
Me.txtData.Name = "txtData"
Me.txtData.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.txtData.Size = New System.Drawing.Size(256, 112)
Me.txtData.TabIndex = 4
Me.txtData.Text = ""
'
'lblEnter
'
Me.lblEnter.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblEnter.Location = New System.Drawing.Point(8, 8)
Me.lblEnter.Name = "lblEnter"
Me.lblEnter.Size = New System.Drawing.Size(200, 24)
Me.lblEnter.TabIndex = 2
Me.lblEnter.Text = "Enter Integer Search Key.:"
Me.lblEnter.TextAlign = System.Drawing.ContentAlignment.BottomCenter
'
'cmdCreate
'
Me.cmdCreate.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.cmdCreate.Location = New System.Drawing.Point(8, 200)
Me.cmdCreate.Name = "cmdCreate"
Me.cmdCreate.Size = New System.Drawing.Size(88, 40)
Me.cmdCreate.TabIndex = 2
Me.cmdCreate.Text = "Create Data"
'
'txtInput
'
Me.txtInput.Cursor = System.Windows.Forms.Cursors.IBeam
Me.txtInput.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.txtInput.Location = New System.Drawing.Point(216, 8)
Me.txtInput.Name = "txtInput"
Me.txtInput.Size = New System.Drawing.Size(48, 26)
Me.txtInput.TabIndex = 3
Me.txtInput.Text = ""
Me.txtInput.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
'
'cmdSearch
'
Me.cmdSearch.Enabled = False
Me.cmdSearch.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.cmdSearch.Location = New System.Drawing.Point(176, 200)
Me.cmdSearch.Name = "cmdSearch"
Me.cmdSearch.Size = New System.Drawing.Size(88, 40)
Me.cmdSearch.TabIndex = 0
Me.cmdSearch.Text = "Search"
'
'lblResult
'
Me.lblResult.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblResult.Location = New System.Drawing.Point(8, 160)
Me.lblResult.Name = "lblResult"
Me.lblResult.Size = New System.Drawing.Size(256, 32)
Me.lblResult.TabIndex = 6
'
'FrmLinearSearchTest
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(272, 253)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cmdCreate, Me.lblResult, Me.txtData, Me.txtInput, Me.lblEnter, Me.cmdSearch})
Me.Name = "FrmLinearSearchTest"
Me.Text = "Linear Search"
Me.ResumeLayout(False)
End Sub
#End Region
Dim array1 As Integer() = New Integer(19) {}
' creates random data
Private Sub cmdCreate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdCreate.Click
Dim output As String
Dim randomNumber As Random = New Random()
Dim i As Integer
output = "Index" & vbTab & "Value" & vbCrLf
' creates string containing 11 random numbers
For i = 0 To array1.GetUpperBound(0)
array1(i) = randomNumber.Next(1000)
output &= i & vbTab & array1(i) & vbCrLf
Next
txtData.Text = output ' displays numbers
txtInput.Text = "" ' clear search key text box
cmdSearch.Enabled = True ' enable search button
End Sub ' cmdCreate_Click
' searches key of element
Private Sub cmdSearch_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdSearch.Click
' if search key text box is empty, display
' message and exit procedure
If txtInput.Text = "" Then
MessageBox.Show("You must enter a search key.")
Exit Sub
End If
Dim searchKey As Integer = Convert.ToInt32(txtInput.Text)
Dim element As Integer = LinearSearch(searchKey, array1)
If element <> -1 Then
lblResult.Text = "Found Value in index " & element
Else
lblResult.Text = "Value Not Found"
End If
End Sub ' cmdSearch_Click
End Class ' FrmLinearSearch
' *************************************************************
' * (C) Copyright 2002 by Deitel & Associates, Inc. *
' * and Prentice Hall. *
' * All Rights Reserved. *
' * *
' * DISCLAIMER: The authors and publisher of this book have *
' * used their best efforts in preparing the book. These *
' * efforts include the development, research, and testing of *
' * the theories and programs to determine their *
' * effectiveness. The authors and publisher make no warranty *
' * of any kind, expressed or implied, with regard to these *
' * programs or to the documentation contained in these books.*
' * The authors and publisher shall not be liable in any event*
' * for incidental or consequential damages in connection *
' * with, or arising out of, the furnishing, performance, or *
' * use of these programs. *
' *************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -