我在Visual Basic.net中编写一个简单的程序来排序名称。点击"报告"我的表单上的按钮对一组名称进行排序,并将其显示在表单上的标签中。单击"重置"按钮清除所有字段和数组并重新开始。我的问题是,当我重置时,下一个名称列表显示在标签的中间位置。我该如何解决这个问题?
Public Class Form1
Const MaxArray As Integer = 29
Dim NameArray(MaxArray) As String
Dim NameCounter As Integer = 1
Dim ArrayCounter As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
SetDefaults()
Reset()
End Sub
Private Sub AddButton_Click(sender As Object, e As EventArgs) Handles AddButton.Click
'This button adds each name into the array by clicking the "add" button.
'erases the First and Last name fields.
'Adds number to entry counter.
FillArray()
DisplayEntry()
ClearFields()
NameCounter = NameCounter + 1
End Sub
Private Sub ReportButton_Click(sender As Object, e As EventArgs) Handles ReportButton.Click
'the report button displays the names on the label
Try
SortArray()
DisplayArray()
Catch ex As Exception
ErrorLabel.Text = ex.Message
End Try
ErrorLabel.Text = "Report Created."
End Sub
Private Sub ResetAllButton_Click(sender As Object, e As EventArgs) Handles ResetAllButton.Click
'This button clears the array, all fields and labels, and starts over
SetDefaults()
SortArray()
DisplayArray()
ErrorLabel.Text = "Reset Complete."
End Sub
Sub ClearFields()
'This sub clears only the first and last name fields.
FirstNameBox.Text = String.Empty
LastNameBox.Text = String.Empty
End Sub
Sub FillArray()
Try
NameArray(ArrayCounter) = LastNameBox.Text & "," & FirstNameBox.Text
ArrayCounter += 1
Catch ex As Exception
ErrorLabel.Text = ex.Message
End Try
End Sub
Sub SortArray()
Dim TempVar As String
Dim ChangeHappened As Boolean = False
Try
Do
ChangeHappened = False
For LoopCounter As Integer = 0 To ArrayCounter - 1
Select Case True
Case (LoopCounter + 1) > ArrayCounter - 1
Case NameArray(LoopCounter) > NameArray(LoopCounter + 1)
TempVar = NameArray(LoopCounter)
NameArray(LoopCounter) = NameArray(LoopCounter + 1)
NameArray(LoopCounter + 1) = (TempVar)
ChangeHappened = True
Case Else
End Select
Next
Loop While ChangeHappened = True
Catch ex As Exception
ErrorLabel.Text = ex.Message
End Try
ErrorLabel.Text = "Done."
End Sub
Sub DisplayArray()
For LoopCounter = 0 To ArrayCounter - 1
SortedNameLabel.Text = SortedNameLabel.Text & NameArray(LoopCounter) & Environment.NewLine
Next
End Sub
Sub DisplayEntry()
UnsortedNameLabel.Text = UnsortedNameLabel.Text & LastNameBox.Text & "," & " " & FirstNameBox.Text & Environment.NewLine
EntryCounterLabel.Text = NameCounter.ToString
End Sub
Sub SetDefaults()
NameCounter = 1
SortedNameLabel.Text = String.Empty
UnsortedNameLabel.Text = String.Empty
FirstNameBox.Text = String.Empty
LastNameBox.Text = String.Empty
Array.Clear(NameArray, 0, NameArray.Length)
For LoopCounter = 0 To ArrayCounter - 1
SortedNameLabel.Text = SortedNameLabel.Text & NameArray(LoopCounter) & Environment.NewLine
Next
End Sub
End Class
答案 0 :(得分:0)
在SetDefaults()
中,您向Label添加了一堆空行。此外,您没有将ArrayCounter重置为零。
考虑到这一点,请尝试将其更改为:
Sub SetDefaults()
ArrayCounter = 0
NameCounter = 1
SortedNameLabel.Text = String.Empty
UnsortedNameLabel.Text = String.Empty
FirstNameBox.Text = String.Empty
LastNameBox.Text = String.Empty
Array.Clear(NameArray, 0, NameArray.Length)
End Sub