2014-01-20 261 views
0

好吧,所以我有問題添加到我的二維數組元素。我正在使用3個文本框來允許用戶將項目輸入到我的數組中。我的問題是我似乎無法讓陣列過去(0,2)。我希望用戶能夠在每次點擊添加按鈕時添加一行輸入。這是迄今爲止我在我的代碼中所擁有的。誰能幫忙?這不是我自己學習的課程。二維陣列VB

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

嘗試......你計數變量必須放在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 
+0

非常感謝。我坐在這裏試圖弄清楚這個永遠大聲笑 – VinceCat

+0

高興地幫助:) –