从文本框中添加数组值并显示在标签中

时间:2018-11-13 09:33:26

标签: vb.net

enter image description here <<<我的界面

下周我正在为考试做一些事情。

我必须使用Visual Basic。我应该用整数和字符串创建一个数组。整数=距离字符串=名称。将有2个文本框,2个标签和2个按钮。

  

txtname.text,txtdistance.text,lblname,lbldistance,btninputdata和btnshowcontent

在填充30个数组并使btnshowcontent可见并在lblname和lbldistance中显示所有30个值(通过文本框插入的值)后,应禁用btninputdata。

两者都需要通过文本框存储插入到数组中,然后使用btnshowcontent,存储的数组应显示在名称和距离的单独标签上。

我的代码:

Public Class Form1
Dim ara(29) As String

Private Sub Form1_Load(sender As Object, e As EventArgs)

End Sub

Private Sub btninputdata_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btninputdata.Click

    If txtname.Text <> "" Then
        For h As Integer = 0 To 29
            If ara(h) = "" Then
                ara(h) = txtname.Text
                txtname.Clear()
                Exit Sub
            End If
            Label1.Text = ara.ToString()


        Next
        MsgBox("arry full")
        btninputdata.Visible = False
        btnshowcontent.Visible = True

    End If
End Sub

Private Sub btnshowcontent_Click(sender As Object, e As EventArgs) Handles btnshowcontent.Click
    'ListBox1.Items.Clear()
    'ListBox1.Items.AddRange(ara)
    ''Label1.Text &= ara(I) & ""

End Sub

Private Sub Form1_Load_1(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub
End Class

1 个答案:

答案 0 :(得分:2)

您将要从类似这样的内容开始。不过,不知道您是如何真正尝试显示所有内容。您可能还想对距离字段进行验证。

Public Class Form1
Dim Ara As New List(Of MyGroup)

Private Sub btninputdata_Click(sender As Object, e As EventArgs) Handles btninputdata.Click
    If txtName.Text.Trim() <> String.Empty Then
        Ara.Add(New MyGroup With {.Name = txtName.Text, .Distance = txtDistance.Text})

        If Ara.Count >= 30 Then
            'Show/Hide buttons

        End If

    End If
End Sub
End Class

Public Class MyGroup
    Public Name As String
    Public Distance As Decimal
End Class

如果您确实必须使用数组,则可以执行以下操作:

Public Class Form1
Private Ara(29) As MyGroup

Private Sub btninputdata_Click(sender As Object, e As EventArgs) Handles btninputdata.Click
    If txtName.Text.Trim() <> String.Empty Then

        Dim EmptyLocation = Array.FindIndex(Ara, Function(x) x Is Nothing)

        If EmptyLocation > -1 Then
            Ara(EmptyLocation) = New MyGroup With {.Name = txtName.Text, .Distance = txtDistance.Text}
            Return
        End If

        'Show/Hide buttons
        'Display the results however.

    End If
End Sub
End Class

Public Class MyGroup
    Public Name As String
    Public Distance As Decimal
End Class