此错误的解决方案“InvalidArgument ='0'的值对'index'无效。”

时间:2013-04-01 07:41:02

标签: vb.net

'错误原因是当我触发事件时,我试图禁用它以防止错误。 但是,再次启用它。当事件被触发时,错误将再次出现。

我用Google搜索了一些解决方案,例如检查ListView中是否存在项目

If ListView2.Items.Count > 0 Then

我也发现了一些处理事件的代码。但是,它似乎无能为力。

AddHandler ListView2.SelectedIndexChanged, AddressOf ListView2_SelectedIndexChanged

我认为删除这些项目会删除错误。但是,它没有用。

以下是代码:

For i = ListView2.Items.Count - 1 To 0 Step -1
  ListView2.Items.Remove(ListView2.Items(i))
Next i

以下是该活动的完整代码:

Private Sub ListView2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView2.SelectedIndexChanged
    If ListView2.Items.Count > 0 Then
        TransactionID.Text = ListView2.SelectedItems(0).Text
        Label6.Left -= 190
        Label7.Left -= 190
        GroupBox1.Left -= 190
        ListView2.Left -= 190

        Button2.Visible = True
        ListView1.Visible = True
        GroupBox2.Visible = True
        Label4.Visible = True
        Label5.Visible = True
        ListView2.Enabled = False
        AddHandler ListView2.SelectedIndexChanged, AddressOf ListView2_SelectedIndexChanged
    End If
End Sub

我发现错误的原因是我正在尝试选择ListView2但是我不再有权访问它,Listview中没有项目或者我没有选择它。 奇怪的是,我确保Listview有项目并启用Listview。

所以是的......有什么可能解决这个问题?

编辑:添加了绑定LISTVIEW的代码

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If ComboBox1.Text = "" Then
        MsgBox("Please specify status of transaction!")
        ComboBox1.Focus()
    End If
    Dim conn As MySqlConnection
    conn = New MySqlConnection()
    Label6.Text = 0

    time = dateTo.Text
    format = "yyyy-MM-dd"
    outTo = (time.ToString(format))
    outTo = outTo & " " & "23:59:59"

    time = dateFrom.Text
    format = "yyyy-MM-dd"
    outFrom = (time.ToString(format))
    outFrom = outFrom & " " & "00:00:00"

    conn.ConnectionString = "server=localhost;user id=root;password=zhakige;database=singin"
    Dim strSQL = "SELECT transaction_id, transaction_status, transaction_staffusername ,transaction_date, transaction_totalprice FROM `transaction` WHERE transaction_date BETWEEN '" & outFrom & "' AND '" & outTo & "' AND transaction_status = '" & ComboBox1.Text & "';"
    conn.Open()
    Dim cmd = New MySqlCommand(strSQL, conn)
    Dim dr = cmd.ExecuteReader()
    ListView2.Items.Clear()

    Do While dr.Read()
        a = (dr.Item("transaction_id").ToString())
        b = (dr.Item("transaction_status").ToString())
        c = (dr.Item("transaction_staffusername").ToString())
        time = (dr.Item("transaction_date"))
        format = "yyyy-MM-dd"
        output = (time.ToString(format))
        d = output
        e2 = (dr.Item("transaction_totalprice").ToString())


        Dim lv As ListViewItem = ListView2.Items.Add(a)
        lv.SubItems.Add(b)
        lv.SubItems.Add(c)
        lv.SubItems.Add(d)
        lv.SubItems.Add(e2)
        Label6.Text += Val(e2)


    Loop
    If ListView2.Items.Count <= 0 Then
        MsgBox("No record found for specified options")
    End If

    dr.Close()
    cmd.Dispose()
    conn.Close()
End Sub

2 个答案:

答案 0 :(得分:0)

MSDN says

  

包含项目的ListView.SelectedListViewItemCollection   在控件中选择。如果当前没有选择任何项目,   返回空的ListView.SelectedListViewItemCollection。

因此,如果您没有任何SelectedItems,则无法使用索引0来读取某些值。 将您的代码更改为

If ListView2.SelectedItems.Count > 0 Then
   ....

答案 1 :(得分:0)

更改此代码

For i = ListView2.Items.Count - 1 To 0 Step -1
  ListView2.Items.Remove(ListView2.Items(i))
Next i

If ListView2.Items.Count > 0 Then
  For i = ListView2.Items.Count - 1 To 0 Step - 1
    ListView2.Items.Remove(ListView2.Items(i))
  Next i
End If

更新

在此代码中

Private Sub ListView2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView2.SelectedIndexChanged
  If ListView2.Items.Count > 0 Then
    TransactionID.Text = ListView2.SelectedItems(0).Text
    Label6.Left -= 190
    Label7.Left -= 190
    GroupBox1.Left -= 190
    ListView2.Left -= 190

    Button2.Visible = True
    ListView1.Visible = True
    GroupBox2.Visible = True
    Label4.Visible = True
    Label5.Visible = True
    ListView2.Enabled = False
    AddHandler ListView2.SelectedIndexChanged, AddressOf ListView2_SelectedIndexChanged
  End If
End Sub

表示

的行
    ListView2.Enabled = False

禁用ListView2,因此应解释清除ListView2的WHY和WHERE

...还 这一行

AddHandler ListView2.SelectedIndexChanged, AddressOf ListView2_SelectedIndexChanged

尝试将事件处理程序分配给它声明的相同例程!

Private Sub ListView2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView2.SelectedIndexChanged

所以很可能它根本没什么新意。