二维数组VB

时间:2014-01-20 00:00:42

标签: arrays vb.net multidimensional-array textbox

好的,所以我在向2d数组中添加元素时遇到了问题。我使用3个文本框来允许用户将项目输入到我的数组中。我的问题是我似乎无法让数组过去(0,2)。我希望用户能够在每次单击添加按钮时添加一行输入。到目前为止,这是我的代码中的内容。有人可以帮忙吗?这不适合我自己学习的课程。

Option Strict On
Option Explicit On
Option Infer Off

Public Class Form1

Private strExams(49, 2) As String

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

    Dim strStudent As String = txtStudent.Text
    Dim strTest As String = txtTest.Text
    Dim strScore As String = txtScore.Text
    Dim count As Integer = 0

    If count <= 49 Then
        strExams(count, 0) = strStudent
        strExams(count, 1) = strTest
        strExams(count, 2) = strScore
        count += 1
    End If

    txtStudent.Text = String.Empty
    txtTest.Text = String.Empty
    txtScore.Text = String.Empty

    txtStudent.Focus()

End Sub

1 个答案:

答案 0 :(得分:1)

试试这个......你的计数变量必须放在btnAdd_Click子项之外,否则它将一直重置为0,因此,你不会超过(0,2)。

Option Strict On
Option Explicit On
Option Infer Off

Public Class Form1

Private strExams(49, 2) As String
Dim count As Integer = 0

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

Dim strStudent As String = txtStudent.Text
Dim strTest As String = txtTest.Text
Dim strScore As String = txtScore.Text

If count <= 49 Then
    strExams(count, 0) = strStudent
    strExams(count, 1) = strTest
    strExams(count, 2) = strScore
    count += 1
End If

txtStudent.Text = String.Empty
txtTest.Text = String.Empty
txtScore.Text = String.Empty

txtStudent.Focus()

End Sub