vb.net 此错误“InvalidArgument=Value of '0' 对 'index' 无效”的解决方案。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15739570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:03:57  来源:igfitidea点击:

Solution for this error "InvalidArgument=Value of '0' is not valid for 'index'."

vb.net

提问by Kyo Yamagata

The 'cause of error is when I trigger the event, I tried to disable it to prevent the error. But, upon enabling it again. When, the event is triggered the error would show up again.

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

I Googled some solutions like checking if items exist in the ListView

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

If ListView2.Items.Count > 0 Then

I also, found some code on handling the event. But, It seems to do nothing.

我还找到了一些处理事件的代码。但是,它似乎什么都不做。

AddHandler ListView2.SelectedIndexChanged, AddressOf ListView2_SelectedIndexChanged

I thought removing the items will remove the error. But, It didn't work.

我认为删除项目将消除错误。但是,它没有用。

Here is the code:

这是代码:

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

Below are the full codes for the event:

以下是活动的完整代码:

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

I found out that the cause of the error is that I'm trying to select the ListView2 but, I no longer have access to it, there are no items in the Listview or I haven't selected it. It's just strange the I made sure the Listview has items and the Listview is enabled.

我发现错误的原因是我正在尝试选择 ListView2,但是我无法再访问它,Listview 中没有项目或者我没有选择它。我确定 Listview 有项目并且 Listview 已启用,这很奇怪。

So yeah... What could possibly the solution for this?

所以是的......有什么可能的解决方案?

EDIT: ADDED THE CODE FOR BINDING THE 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

回答by Steve

MSDN says

MSDN 说

A ListView.SelectedListViewItemCollection that contains the items that are selected in the control. If no items are currently selected, an empty ListView.SelectedListViewItemCollection is returned.

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

So if you don't have any SelectedItems you can't use index 0 to read some value. Change your code to

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

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

回答by Zeddy

Change this code

更改此代码

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

to

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

Update

更新

in this code

在这段代码中

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

the line that says

说的那一行

    ListView2.Enabled = False

disables the ListView2, so that should explain WHY and WHERE the ListView2 is being disabled

禁用 ListView2,因此应该解释禁用 ListView2 的原因和位置

Also... This line

还有...这一行

AddHandler ListView2.SelectedIndexChanged, AddressOf ListView2_SelectedIndexChanged

attempts to assign an event handler to the same routine it is declared in!

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

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

So chances are it probably does nothing new at all.

所以很有可能它根本就没有什么新东西。