Visual Basic enter information in an array

2019-09-21 05:09发布

问题:

I have two text boxes to enter the Name and the Marks of students.

I don't know how to create the array for it in Visual Basic

The array must be multidimensional and also needs to have an index that increments every time a new mark and name is entered.

Once the array is completed I need the results to show in a list box.

Thank you

EDIT!!!

This is the code i now have but i still have few errors

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim LName As New List(Of String)
    Dim LMark As New List(Of Integer)


    LName.Add(txtEnterName.Text)
    LMark.Add(txtEnterMarks.Text)

    For counterOne As Integer = 0 To 10

        For counterTwo As Integer = 0 To 10

       Array[counterOne][counterTwo] = listview.text

        Next

    Next
End Sub

回答1:

Basically like this ..

Dim LName as New List(Of String)
Dim LMark as New List(Of Integer)

So, if you want to add your textbox into ..

LName.Add(Textbox1.Text)
LMark.Add(TextBox2.Text)

For Next you have to learn about List Of .. I'm sure Mr. Google will help you ..

To resulting in ListBox .. better you use ViewListBox with 2 columns ..



回答2:

Arrays are kind of messy for this kind of application. It's not easy to increase or decrease it's size according to the input you get. List's are much easier to use. You could also use a list of keyvaluepairs which will tie the two parts of your data together:

Public Class Form2
    'Declare the list here so that it's available to the whole class    
    Dim AllMarks As New List(Of KeyValuePair(Of String, Integer))
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub

Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    Dim Mark As Integer
    If Integer.TryParse(txtEnterMarks.Text, Mark) AndAlso txtEnterName.Text <> "" Then
        AllMarks.Add(New KeyValuePair(Of String, Integer)(txtEnterName.Text, Mark))
    End If
    'Using the datasource property is a quick and easy way to fill your listbox
    ListBox1.DataSource = Nothing
    ListBox1.Items.Clear()
    ListBox1.DataSource = AllMarks
    txtEnterMarks.Text = ""
    txtEnterName.Text = ""
End Sub
End Class