Vb.net 列表框项目删除

Vb.net 列表框项目删除,vb.net,visual-studio-2010,listbox,Vb.net,Visual Studio 2010,Listbox,如何在包含“”之后删除列表框项目文本 列表框项目文本示例: I want it ? please, remove it. 我的代码: 如果我想删除特定的文本,那么我就使用它 For i = 0 To CheckedListBox1.Items.Count - 1 If CheckedListBox1.Items(i).ToString.Contains("something") Then CheckedListBox1

如何在包含“”之后删除列表框项目文本

列表框项目文本示例:

I want it ? please, remove it.
我的代码:

如果我想删除特定的文本,那么我就使用它

For i = 0 To CheckedListBox1.Items.Count - 1
            If CheckedListBox1.Items(i).ToString.Contains("something") Then
                CheckedListBox1.Items(i) = CheckedListBox1.Items(i).ToString.Replace("something", "")
            End If
但它是动态生成的列表框项。 例如:


开始向后循环,然后使用

当您要从集合中删除一个或多个项时,从集合的结尾向其第一个元素循环是很重要的。如果在正常转发模式下循环,则删除项目时可能会出现问题。例如,如果删除位置5处的项目,位置6处的项目将在位置5处移动,但随后将循环索引器增加到6,从而有效地跳过先前位于位置6处的项目的逻辑

更新:如果要删除包含问号的项目,请使用其他方法检查所检查项目中是否存在问号

For i = ListBox1.Items.Count - 1 To 0 Step -1
    If ListBox1.Items(i).ToString.Contains("?") Then
       ListBox1.Items.RemoveAt(i)
    End If
Next
或者,如果您想替换包含问号的当前项,请在问号后剪掉文本,并编写如下内容

For i = ListBox1.Items.Count - 1 To 0 Step -1
    If ListBox1.Items(i).ToString.Contains("?") Then
       Dim item = ListBox1.Items(i).ToString()
       Dim pos = item.IndexOf("?"c)
       item = item.Substring(0, pos)
       ListBox1.Items(i) = item
    End If
Next
最后,如果问号被空格包围,您可以添加修剪

 item = item.Substring(0, pos).TrimEnd()

ListBox1.Items.Remove或RemoveAt(我不确定)您的问题不清楚:是否要1)删除列表框中的项目?2) 用另一个项目替换列表框中的项目?或3)替换/格式化/更改/更改该列表框中项目的显示的
文本
。我想在“?”之后替换(或删除)我的列表框项目文本。我更新了我的问题,请重新阅读。“ListBox1.Items.RemoveAt(i)”是不执行任何操作。RemoveAt完全删除项目。您的条件(StartsWith)查找以问号开头的项。如果要删除包含问号的项目,则需要使用CONTAINSY您的代码删除包含“”的所有项目,我要替换(或删除)我的列表框项目“”后的文本示例:我保留它?我删除它
For i = ListBox1.Items.Count - 1 To 0 Step -1
    If ListBox1.Items(i).ToString.Contains("?") Then
       ListBox1.Items.RemoveAt(i)
    End If
Next
For i = ListBox1.Items.Count - 1 To 0 Step -1
    If ListBox1.Items(i).ToString.Contains("?") Then
       Dim item = ListBox1.Items(i).ToString()
       Dim pos = item.IndexOf("?"c)
       item = item.Substring(0, pos)
       ListBox1.Items(i) = item
    End If
Next
 item = item.Substring(0, pos).TrimEnd()