在列表框中查找文本的一部分 [英] Find a part of text in a listbox

查看:34
本文介绍了在列表框中查找文本的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一些代码来查找包含在列表框中的文本,但这不是我需要的.当用户在文本框(即搜索字段)中键入时,他们必须键入准确的文本,而不是文本的一部分.有没有办法在列表框中找到值/文本的一部分?

例如,我有一个包含这些项目的列表框:

<块引用>

  1. 数据1
  2. 数据2

当我在搜索字段(文本框/富文本框)中输入 (2) 时,我希望选择包含2"值的第二项.

我该如何编码?

解决方案

你可以使用 IndexOf

Dim hits = From item In listBox1.Items.Cast(Of String)()其中 item.IndexOf(txtSearch.Text, StringComparison.OrdinalIgnoreCase) >= 0If hits.Any ThenlistBox1.SelectedItem = hits.First()万一

如果您不想忽略大小写,只需使用 String.Contains 而不是 String.IndexOf.

请注意,上面是一个 linq 查询,因此它不适用于 .NET 2.

I've been given some code to find text that is contained in a listbox but, its not what I need. When the user types in the textbox (which is the search field), they have to type the exact text, not the part of the text. Is there any way to find a part of a value/text in a listbox?

For example, I have a listbox that contains these items:

  1. data1
  2. data2

When I type (2) in the search field(textbox/richtextbox), I would like the second item, which contain the '2' value, to be selected.

How can I code this?

解决方案

You could use IndexOf

Dim hits = From item In listBox1.Items.Cast(Of String)()
           Where item.IndexOf(txtSearch.Text, StringComparison.OrdinalIgnoreCase) >= 0
If hits.Any Then
    listBox1.SelectedItem = hits.First()
End If

If you don't want to ignore the case, just use String.Contains instead of String.IndexOf.

Note that above is a linq query, so it won't work with .NET 2 this way.

这篇关于在列表框中查找文本的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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