使用For循环从ComboBox中删除项目 [英] Removing Items from ComboBox using For loop

查看:253
本文介绍了使用For循环从ComboBox中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
请参见下面的代码:

hi all,
please see the code below:

Dim ComValue As String
        If cmbMonth.SelectedItem = "" Then
            MessageBox.Show("Please select Month To check", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub
        End If

        For i As Integer = 0 To Val(cmbRegNo.Items.Count - 1)
            ComValue = cmbRegNo.Items(i)
            CheckDepositedFees(cmbMonth.SelectedItem, ComValue)
            If k = "shiv" Then
                cmbRegNo.Items.Remove(cmbRegNo.Items(i))
            End If
        Next




在给定的代码中,cmbRegNo已被数据库中的函数填充,因此我们没有其range(count).我正在调用另一个名为"checkDepositedFees"的函数,如果找到数据,则将结果为(k ="shiv"),然后我要从cmbRegNo中删除该RegNo.但它给我错误,因为从cmbRegNo中删除了项目.我该如何解决该运行时错误(InvalidArgument =值"5"对"index"无效.参数名称:index)请尽快帮助我.




In the given code cmbRegNo has been filled with a function from database so we don''t have its range(count). I''m Calling another function called "checkDepositedFees" If data found then it will Result (k= "shiv") and then I want to remove that RegNo from cmbRegNo. but its gives me error because Items removed from cmbRegNo. how Can I solve that run time error(InvalidArgument=Value of ''5'' is not valid for ''index''. Parameter name: index) please help me soon.

推荐答案

之所以会这样,是因为在开始时已为For循环设置了cnbRegNo的总数.一旦删除一个,cmbRegNo中的项目数就会减少,但是for循环会执行额外的步骤.
一种方法是,您可以存储要删除的索引(在for循环工作时不删除它们),然后稍后又将它们删除(在for循环结束之后).
This is happening as For loop is set for total count of cnbRegNo in the the start. As soon you remove one, the count of items in cmbRegNo reduces but for loop goes to extra step.
One of the ways, you can store the indexes to remove (and not remove them when for loop is working) and then later remove them in one shot (after for loop ends).


I认为代码还存在上述提到的其他问题,

顺便说一句,不要删除循环中的项目,也不要存储索引.

如果存储索引以将其删除,则在结束循环后,删除索引仍然会遇到问题.

例如:
您存储了{0,1,4,10,15}

删除Item(0)之后,Item(1)的实际索引将为0,其他索引也将更改.

我更喜欢将 Item (不是它的索引)存储在List< typeofitem>中.并在完成循环后,尝试将其删除,
I think the code has another problems exept the mentioned,

By the way, do not remove the items in the loop and do not store the indexes too.

If you store the indexes to remove them, after ending the loop, You will still have problem in removing them.

For example:
You stored {0,1,4,10,15}

After removing Item(0) then the real index of Item(1) will be 0 and the other indexes will be also changed.

I prefer to store the Item (not it''s index) in a List<typeofitem> and after finishing the loop, try to remove them,


尝试以下操作:
Try this:
Private Sub RemoveDataB()
    Dim sTmp As String = String.Empty, i As Integer = 0, j As Integer
    Dim oItem As Object = Nothing

    sTmp = "Items to remove:" & vbCr
    j = Me.ComboBox1.Items.Count - 1
    Do While i < j
        oItem = Me.ComboBox1.Items(i)
        If oItem.ToString Like "Ship*" Then
            sTmp = sTmp & oItem.ToString & vbCr
            Me.ComboBox1.Items.Remove(oItem)
            i = i - 1
            j = j - 1
        End If
        i = i + 1
    Loop
    MsgBox(sTmp,MsgBoxStyle.Information,"Info...")
End Sub


这篇关于使用For循环从ComboBox中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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