首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >清除窗体上的数组和标签

清除窗体上的数组和标签
EN

Stack Overflow用户
提问于 2017-12-14 05:28:24
回答 1查看 23关注 0票数 0

我正在用Visual basic.net编写一个简单的程序来对名字进行排序。单击窗体上的"report“按钮对名称数组进行排序,并将其显示在窗体上的标签中。单击“重置”按钮将清除所有字段和数组并重新开始。我的问题是,当我重置时,下一个名称列表显示在标签的一半处。我该如何解决这个问题呢?

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2017-12-14 07:36:27

SetDefaults()中,您向标签添加了一堆空行。此外,您没有将ArrayCounter重置为零。

考虑到这一点,请尝试将其更改为:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47802387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档