我怎样才能从列表框中选择一些东西
问题描述:
有没有办法读取列表框项目中的字符串,提示用户输入字符串,如果用户正确输入它,选择下一个项目,直到没有剩下要选择的项目?我在VB.NET中做这个。帮助将不胜感激。我怎样才能从列表框中选择一些东西
答
你可以做这样的事情:
Dim items = ListBox1.Items.Cast(Of Object)().Select(Function(x) x >= x.ToString).ToList()
If items.Contains(TextBox1.Text) Then
Dim nextIndex = items.IndexOf(TextBox1.Text) + 1
If nextIndex < items.Count Then 'There is next item
Dim nextItem = items(nextIndex).ToString()
'Do something with your nextItem
End If
End If
基本上,你尝试这样做是为了让所有的文本在您的ListBox
:
Dim items = ListBox1.Items.Cast(Of Object)().Select(Function(x) x >= x.ToString).ToList()
然后你检查是否输入字符串是其中在您的列表框中的文本:
If items.Contains(TextBox1.Text) Then
.
.
.
End If
如果确实如此,您将得到nex的索引牛逼的项目:
Dim nextIndex = items.IndexOf(TextBox1.Text) + 1
;如果下一个项目的索引没有超过ListBox
的元素的数量,你在接下来的指数获得该项目:
If nextIndex < items.Count Then 'There is next item
Dim nextItem = items(nextIndex).ToString()
'Do something with your nextItem
End If
你的下一个字符串在nextItem
,做一些事情。
或者,不填充整个项目,但仍然得到同样的结果,你可以添加额外的Where
LINQ条款(如Codexer建议):
Dim item = ListBox1.Items.Cast(Of Object)().Select(Function(x) x >= x.ToString).Where(Function(y) y = TextBox1.Text).FirstOrDefault()
If Not item Is Nothing Then
Dim nextIndex = ListBox1.Items.IndexOf(item) + 1
If nextIndex < ListBox1.Items.Count Then 'There is next item
Dim nextItem = ListBox1.Items(nextIndex)
'Do something with your nextItem
End If
End If
有点...不清楚 – Ian
要明确希望它,列表框中有3个项目。项目1的字符串是“字符串1”,项目2的字符串是“字符串2”,项目3的字符串是“字符串3”。如果用户在文本框中输入了项目1的正确字符串(“字符串1”),程序将提示你输入项目2的字符串(“字符串2”)@Ian –
@Ian你在吗? –