检查列表框集合中是否找到文本框中的单词? [英] Check if a word in textbox is found in listbox collection?

查看:102
本文介绍了检查列表框集合中是否找到文本框中的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大量单词的列表。我也有一个文本框。当用户按下按钮时,程序应该看到文本框文本中的单词(例如:这是我的文本。)是否包含在我的列表框中。我不能用这个:

I have a list which contains an amount of words. I also have a textbox. When the user presses a button, the program should see if a word inside the text of the textbox (Example: "This is my text.") is contained inside my listbox. I can't use this:

If listbox4.items.contains(textbox1.text) = true then
'My code
End If





代码所做的全部^^^检查文本框中的所有文本是否与列表框中的项目匹配。我只想检查我的文本框中文本句子中的某个单词是否在我的列表框中找到。



这就像一个永无止境的循环! <<根据我疲惫的星期四大脑



例如。 (愚蠢的疯狂代码):



All ^^^that code does is check if ALL of the text in the textbox matches an item in the listbox. I only want it to check if a certain word in a sentence of the text in my textbox is found in my listbox.

It's like a never ending loop! << according to my tired thursday brain

Eg. (silly code of madness):

If textbox1.text.contains(listbox4.items.collection.tostring or an item in my listbox) then
'My code
End If





任何帮助将不胜感激!如果有某种东西或某种方法我应该知道,请告诉我!



顺便说一句:我在网上寻求帮助!

推荐答案

首先,您必须将文本框中的字符串拆分为由空格分隔的单词数组。接下来循环通过数组来检查列表框中是否包含任何单词元素。



First, you have to split the string in the textbox into array of words delimited by space. Next loop thru the array to check if any of the word element is contained in the listbox.

Public Class Form9

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        ' Split string based on space
        Dim textsrtring As String = TextBox1.Text
        Dim words As String() = textsrtring.Split(New Char() {" "c})
        Dim found As Boolean = False

        ' Use For Each loop over words
        Dim word As String
        For Each word In words
            If ListBox1.Items.Contains(word) Then
                found = True
                Exit For
            End If
        Next

        MessageBox.Show(found)

    End Sub

    Private Sub Form9_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.Add("AAAA")
        ListBox1.Items.Add("BBBB")
        ListBox1.Items.Add("CCCC")
        ListBox1.Items.Add("DDDD")
    End Sub
End Class


实际上很简单:使用空格作为分隔符拆分文本框的文本,并搜索结果数组包含的每个单词。您可以在第一场比赛中停止或检索与任何单词匹配的所有列表项。
It is actually simple: split the text of your textbox using the space as separator and search for each of the word the result array contains. You could stop at firsh match or retrieve all the list items matching any of the word.


这篇关于检查列表框集合中是否找到文本框中的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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