Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays VB在listbox中显示多维数组中的元素_Arrays_Vb.net_Multidimensional Array_Listbox - Fatal编程技术网

Arrays VB在listbox中显示多维数组中的元素

Arrays VB在listbox中显示多维数组中的元素,arrays,vb.net,multidimensional-array,listbox,Arrays,Vb.net,Multidimensional Array,Listbox,由于某种奇怪的原因,当我使用循环来显示数组的元素时,我得到了一个错误。我似乎无法理解我在做什么或做得不对。这是到目前为止的代码。这不是为了上课,我在自学 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 Obje

由于某种奇怪的原因,当我使用循环来显示数组的元素时,我得到了一个错误。我似乎无法理解我在做什么或做得不对。这是到目前为止的代码。这不是为了上课,我在自学

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

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

    Dim intHighRow As Integer = strExams.GetUpperBound(0)
    Dim intHighCol As Integer = strExams.GetUpperBound(1)
    Dim intR As Integer
    Dim intC As Integer
    Do While intC <= intHighCol
        intR = 0
        Do While intR <= intHighRow
            lstMessage.Items.Add(strExams(intR, intC))
            intR += 1
        Loop
        intC += 1
    Loop

End Sub
选项严格打开
选项显式打开
选项推断
公开课表格1
私有strExams(49,2)作为字符串
Dim计数为整数=0
私有子btnAdd_Click(发送者作为对象,e作为事件参数)处理btnAdd。单击
Dim strStudent As String=TXTSUDENT.Text
Dim strTest As String=TXTEST.Text
Dim strScore作为字符串=txtScore.Text

如果计数请尝试此操作。这对我来说更有意义。出现空错误的原因是数组中没有填写所有内容,并且listbox不能列出空项。因此,一种解决方法是只枚举已经有值的项,因此,只需循环直到count的最后一个值

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
    Dim intR As Integer

    lstMessage.Items.Clear()
    Do While intR < count
        lstMessage.Items.Add(strExams(intR, 0) & " - " & strExams(intR, 1) & " - " & strExams(intR, 2))
        intR += 1
    Loop
End Sub
Private Sub btnDisplay\u Click(发送者作为对象,e作为事件参数)处理btnDisplay。单击
作为整数的Dim intR
lstMessage.Items.Clear()
当intR<计数时执行
lstMessage.Items.Add(strExams(intR,0)&“-”和strExams(intR,1)&“-”和strExams(intR,2))
intR+=1
环
端接头

单击btnDisplay按钮时,您想在这里实现什么?我试图将数组的所有内容放入列表框中。我似乎混淆了我希望如何列出它。它应该是按行而不是按列列出的。我想是这样的,但不确定如何将所有列添加到列表框中。再次谢谢,不客气。默认的listbox控件只能有1列,因此我在单独的值中添加了“-”字符。如果要以多列格式显示,则必须使用ListView,或者可以使用具有列出多列格式功能的第三方listbox控件。