2017-12-13 4 views
0

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

코드를 다시 포맷하십시오 : 그와

염두에로 변경합니다. – jingx

답변

0

SetDefaults()에서 레이블에 빈 줄을 추가합니다. 또한 ArrayCounter를 0으로 재설정하지 않습니다.

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