代码可以工作,但只适用于第一项? [英] The code works but only for the first item?

查看:58
本文介绍了代码可以工作,但只适用于第一项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个列表视图:PokedexList,PkmFormList和PkmAbList。在加载表单时,将从数据库中填充PokedexList。当用户从PokedexList中选择一个宠物小精灵时,它会用所有形式的精选小精灵填充PkmFormList。当从PkmFormList中选择表格时,PkmAbList将填充所选择的口袋妖怪表单的能力。这是预期的,这就是它的作用。但是,它仅适用于所选的第一个项目。



示例:我从PokedexList中选择Absol,用Absol的两种形式Normal和Mega填充PkmFormList。但是,如果我然后决定,而不是Absol,我想要Deoxys,我希望PkmFormList可以填充Normal,Attack,Defense和Speed。相反,它会弹出一个错误,上面写着InvalidArgument ='0'的值对'index'无效。



I have 3 list views: PokedexList, PkmFormList, and PkmAbList. PokedexList is populated from a database when the form loads. When the user selects a pokemon from PokedexList it populates PkmFormList with all the forms of the selected pokemon. When a form is selected from PkmFormList, PkmAbList is populated with the abilities of the selected pokemon form. This is what's expected and this is what it does. However, it only does it for the first item selected.

Example: I select "Absol" from PokedexList, that populates PkmFormList with Absol's two forms, "Normal" and "Mega." However, if I then decide that, instead of "Absol", I wanted "Deoxys", I would expect PkmFormList to be populated with "Normal", "Attack", "Defense", and "Speed". Instead, it pops an error that says "InvalidArgument=Value of '0' is not valid for 'index'."

Private Sub PokedexList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles PokedexList.SelectedIndexChanged

    'shows the selected pokemon as part of your party
    If Pkm1Select.Checked = True Then
        For Each item As ListViewItem In PokedexList.SelectedItems
            Pkm1Txt.Text = item.Text
        Next

    ElseIf Pkm2Select.Checked = True Then
        For Each item As ListViewItem In PokedexList.SelectedItems
            Pkm2Txt.Text = item.Text
        Next

    ElseIf Pkm3Select.Checked = True Then
        For Each item As ListViewItem In PokedexList.SelectedItems
            Pkm3Txt.Text = item.Text
        Next

    ElseIf Pkm4Select.Checked = True Then
        For Each item As ListViewItem In PokedexList.SelectedItems
            Pkm4Txt.Text = item.Text
        Next

    ElseIf Pkm5Select.Checked = True Then
        For Each item As ListViewItem In PokedexList.SelectedItems
            Pkm5Txt.Text = item.Text
        Next

    ElseIf Pkm6Select.Checked = True Then
        For Each item As ListViewItem In PokedexList.SelectedItems
            Pkm6Txt.Text = item.Text
        Next
    End If

    'pulls a list of forms for the selected pokemon
    Dim SqlQry As String = "SELECT [Pokemon_forms].[pokemon_id], [Pokemon].[id], [Pokemon].[identifier], [Pokemon_forms].[form_identifier] " &
        "FROM [Pokemon], [Pokemon_forms] WHERE (([Pokemon].[id]=[Pokemon_forms].[pokemon_id])) AND [Pokemon].[identifier] = ?;"

    DbCommand = New OleDbCommand(SqlQry, DbConnection)
    Dim IdentifierParam As New OleDbParameter("?", OleDbType.VarChar)
    DbCommand.Parameters.Add(IdentifierParam)
    IdentifierParam.Value = PokedexList.SelectedItems(0).Text

    DbConnection.Open()

    Dim da As OleDbDataAdapter = New OleDbDataAdapter(DbCommand)

    Dim ds As DataSet = New DataSet

    da.Fill(ds, "Pokemon_forms")

    Dim dt As DataTable = ds.Tables("Pokemon_forms")

    Dim row As DataRow

    For Each row In dt.Rows
        Dim FormIdentifier As String = row("form_identifier")

        FormIdentifier = StrConv(FormIdentifier, vbProperCase)

        PkmFormList.Items.Add(FormIdentifier)
    Next

    DbConnection.Close()

End Sub





任何想法如何解决这个问题?



Any ideas how to fix this?

推荐答案

'this is where it throws the error
IdentifierParam.Value = PokedexList.SelectedItems(0).Text



然后可以合理地假设 PokedexList.SelectedItems 不包含任何项目。使用调试器找出原因。


Then it is reasonable to assume that PokedexList.SelectedItems does not contain any items. Use your debugger to find out why.


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



您是否试过


Did you try with

Option Base 0




?


这篇关于代码可以工作,但只适用于第一项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆