如何获取列表框中每个选定项目的字符串(MultiSimple)
你好,
我有一个列表框,其中SelectionMode = MultiSimple,我想检查它们包含的字符串,以便对每个字符串进行相应的操作(请参见代码段).
到目前为止,我已经尝试过:
Hi there,
I''ve got a Listbox where SelectionMode = MultiSimple and I''d like to check the strings they contain to follow up with according actions (see Snippet) for each.
What I''ve tried so far:
For Each selecteditem As [Object] In ListBox1.SelectedItems
If ListBox1.SelectedItem.ToString = "Text 1" Then
Action 1
ElseIf ListBox1.SelectedItem.ToString = "Text 2" Then
Action 2
ElseIf ListBox1.SelectedItem.ToString = "Text 3" Then
Action 3
End If
Next
问题:对于所选的每个项目执行For ... Next语句 (如果我选择3个项目,它将执行3次,如果我选择2个,则执行2次)时间等),但我认为iBlade.ListBox2.SelectedItem.ToString
仅返回我选择的第一项.
如果选择文本1",文本2"和文本3",则文本1"的动作(动作1")将执行3次.如果我选择文本2"和文本3",则文本2"的动作(动作2")将重复两次,依此类推...
我想对动作2"和动作3"分别执行一次.但是我无法真正弄清楚.
The problem: While the For...Next Statement is executed for each item that is selected (If I select 3 items it''s executed 3 times, if I select 2 it''s executed 2 times, etc) but I think iBlade.ListBox2.SelectedItem.ToString
only returns the first item I select.
If I select "Text 1", "Text 2" and "Text 3" the Action ("Action 1") for "Text 1" is executed three times. If I select "Text 2" and "Text 3" the Action ("Action 2") for "Text 2" is repeated two times and so on...
What I''d like to happen is for "Action 2" and "Action 3" each to be executed once. But I can''t really figure it out.
Sandeep Mewara试图告诉您使用selecteditem
对象,而不是ListBox1.SelectedItems(LBitmCounter)
.在我的示例中,我将其替换为oItem
Sandeep Mewara was trying to tell you to useselecteditem
object, notListBox1.SelectedItems(LBitmCounter)
. In my example i replaced it withoItem
For Each oItem As [Object] In ListBox1.SelectedItems
If oItem.ToString = "Text 1" Then
Action 1
ElseIf oItem.ToString = "Text 2" Then
Action 2
ElseIf oItem.ToString = "Text 3" Then
Action 3
End If
Next
了解有关选择案例 [
Learn about SELECT CASE[^] statement.
For LBitmCounter = 0 To ListBox1.SelectedItems.Count - 1
If ListBox1.SelectedItems(LBitmCounter).ToString = "Text 1" Then
Action 1
ElseIf ListBox1.SelectedItems(LBitmCounter).ToString = "Text 2" Then
Action 2
ElseIf ListBox1.SelectedItems(LBitmCounter).ToString = "Text 3" Then
Action 3
End If
Next