VB.net索引超出范围异常

时间:2014-11-04 17:01:20

标签: arrays vb.net visual-studio-2010 indexing

此程序加载.txt将其拆分为":"然后拿走每个并在某处检查它们 我的程序到达列表末尾时出错。这是我的代码

Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
    If ListBox1.Items.Count = 0 Then
        MsgBox("Checking DONE")
    ElseIf ListBox1.Items.Count.ToString > 0 Then

        Dim str As String
        Dim strArr() As String

        str = ListBox1.Items(0)
        strArr = str.Split(":")
        If str.Count > 0 Then
            WebBrowser1.Document.GetElementById("email").SetAttribute("value", (strArr(0)))
            WebBrowser1.Document.GetElementById("password").SetAttribute("value", (strArr(1)))
            WebBrowser1.Document.GetElementById("login-form-contBtn").InvokeMember("click")
            WaitForPageLoad()
            Threading.Thread.Sleep(5000)
            Me.Button2.PerformClick()
        Else
            MsgBox("Done")
        End If
        End If


End Sub

同样,Me.Button2.PerformClick()以编程方式单击按钮,然后按两次单击此按钮,它应该继续,直到列表框为空,但它只是崩溃/给出错误

这是错误http://gyazo.com/cb7c47bca69af101871035bf18231b6e

这是将列表导入列表框的按钮     OpenFileDialog1.InitialDirectory =" ./"         OpenFileDialog1.FileName ="打开文本文件......"         OpenFileDialog1.Filter ="文本文件(* .txt)| * .TXT"         OpenFileDialog1.ShowDialog()

    Dim R As New IO.StreamReader(OpenFileDialog1.FileName)
    Dim string1 As String() = R.ReadToEnd().Split(New String(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries))

    ListBox1.Items.AddRange(string1)
    R.Close()
    ListBox1.SelectedItem = ListBox1.Items(0)

    Dim str As String
    Dim strArr() As String

    str = ListBox1.SelectedItem
    strArr = str.Split(":") 'Delimits the imported combo list
    Label4.Text = (strArr(0)) 'Email
    Label3.Text = (strArr(1)) 'Password
    Label6.Text = (ListBox1.Items.Count) 'How big is combo?

1 个答案:

答案 0 :(得分:2)

您在strArr中拆分str,但检查str.Count

str = ListBox1.Items(0)
strArr = str.Split(":")
If strArr.Count > 0 Then
   ....

以下代码使用strArr的索引0和索引1,以便更好地检查

If strArr.Count > 1 Then