Results 1 to 12 of 12

Thread: [RESOLVED] vb.net check if word exist

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2022
    Posts
    40

    [RESOLVED] vb.net check if word exist

    Hello everyone, I tried to do an internet search but I can't find the solution that fits my needs.
    Having a list of words that I decide, can I check if they are present inside a cell of a datagridview with a very long text?
    I tried different solutions like:
    Code:
    Public words As HashSet(Of String) = New HashSet(Of String)() From {
         "byebye",
         "say"
         }
    and it works but only if I have one of those words alone in the cell.
    Last edited by Marco:G; Mar 24th, 2023 at 11:02 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: vb.net check if word exists

    Quote Originally Posted by Marco:G View Post
    Hello everyone, I tried to do an internet search but I can't find the solution that fits my needs.
    What did you search for? Probably nothing useful. The DataGridView is irrelevant. You're searching for a substring with a String. Where that String comes from is irrelevant.

    If you just want to know whether an arbitrary substring exists anywhere, you can call String.Contains or String.IndexOf, depending on your .NET version and whether you want a case-insensitive search or not. If you're looking for distinct words, i.e. surrounded by spaces or punctuation, then you would use regular expressions via the Regex class.

    Now you know what to look into, you can look into it. You can work out how to do it for one substring and one String. If you then need to do it for multiple substrings and/or Strings, you can add the appropriate loops or, if you're comfortable, LINQ query.

  3. #3
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: vb.net check if word exists

    Interresting, this question is similar to this one VS 2019 I want to scan row and give msgbox if equal. Excuse me?-VBForums so the answer will be also similar...
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2022
    Posts
    40

    Re: vb.net check if word exists

    Quote Originally Posted by jmcilhinney View Post
    What did you search for? Probably nothing useful. The DataGridView is irrelevant. You're searching for a substring with a String. Where that String comes from is irrelevant.

    If you just want to know whether an arbitrary substring exists anywhere, you can call String.Contains or String.IndexOf, depending on your .NET version and whether you want a case-insensitive search or not. If you're looking for distinct words, i.e. surrounded by spaces or punctuation, then you would use regular expressions via the Regex class.

    Now you know what to look into, you can look into it. You can work out how to do it for one substring and one String. If you then need to do it for multiple substrings and/or Strings, you can add the appropriate loops or, if you're comfortable, LINQ query.
    I tried with string.contains but it finds the word or words that I have as keywords but only if they are alone in the textbox if they are in a sentence it does not find them

    this is where i call the above code:
    Code:
    If words.Contains(TextBox2.Text.ToLower) Then GoTo step1 Else GoTo step2
    I probably use it wrong

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: vb.net check if word exists

    Quote Originally Posted by Marco:G View Post
    I tried with string.contains but it finds the word or words that I have as keywords but only if they are alone in the textbox if they are in a sentence it does not find them

    this is where i call the above code:
    Code:
    If words.Contains(TextBox2.Text.ToLower) Then GoTo step1 Else GoTo step2
    I probably use it wrong
    Well that's the wrong way to do it, so no wonder it didn't work. This is what happens when you throw stuff at the wall without a clear idea of what you're actually trying to achieve. You are not trying to determine whether the list contains the value in the TextBox. You're trying to determine whether the value in the TextBox contains a specific value from the list, then again for the next value in the list, then so on for all the values in the list. Your logic is backwards and incomplete. What you should ALWAYS do when addressing a programming problem is forget it's a programming problem and ask yourself how you would do it if it were a manual problem. You start by reading the first value from your list, then you'd check whether it existed anywhere in the target text, then you'd do the same for then next item in the list and so on. The code you write needs to reflect that logic. Your code doesn't reflect any logic that anyone should have come up with for addressing this problem, but I have my doubts that you actually came up with any logic, because most people don't bother. They just start writing code and assume it will come together. It usually doesn't.

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2022
    Posts
    40

    Re: vb.net check if word exists

    Quote Originally Posted by Delaney View Post
    Interresting, this question is similar to this one VS 2019 I want to scan row and give msgbox if equal. Excuse me?-VBForums so the answer will be also similar...
    Thanks so much, that's what I was looking for. Super.

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2022
    Posts
    40

    Re: vb.net check if word exists

    Quote Originally Posted by jmcilhinney View Post
    Well that's the wrong way to do it, so no wonder it didn't work. This is what happens when you throw stuff at the wall without a clear idea of what you're actually trying to achieve. You are not trying to determine whether the list contains the value in the TextBox. You're trying to determine whether the value in the TextBox contains a specific value from the list, then again for the next value in the list, then so on for all the values in the list. Your logic is backwards and incomplete. What you should ALWAYS do when addressing a programming problem is forget it's a programming problem and ask yourself how you would do it if it were a manual problem. You start by reading the first value from your list, then you'd check whether it existed anywhere in the target text, then you'd do the same for then next item in the list and so on. The code you write needs to reflect that logic. Your code doesn't reflect any logic that anyone should have come up with for addressing this problem, but I have my doubts that you actually came up with any logic, because most people don't bother. They just start writing code and assume it will come together. It usually doesn't.
    Yes, I think you're right, for work reasons I had to approach this type of programming, I'm self-taught and with your help I managed to come up with something good.
    Sometimes I get lost, but reading between forums and other things I get out of it, sometimes I don't.

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2022
    Posts
    40

    Re: vb.net check if word exists

    Quote Originally Posted by Delaney View Post
    Interresting, this question is similar to this one VS 2019 I want to scan row and give msgbox if equal. Excuse me?-VBForums so the answer will be also similar...
    I'm sorry, but there is also a way to highlight keywords permanently

  9. #9

    Thread Starter
    Member
    Join Date
    Oct 2022
    Posts
    40

    Re: [RESOLVED] vb.net check if word exists

    Code:
     
    Dim highColor As Color = Color.Red
    Dim text_2 As String = Form1.TextBox2.Text 
    Dim Text_1 As New List(Of String) From {{"dog"}, {"cat"}, {"cow"}, {"bit"}, {"table"}}
    
    
            For Each word As String In Text_1
    
                If text_2.ToLower.Contains(word) Then
    
                    For Each r As DataGridViewRow In Report.Rows
                        If Not r.Index = Report.NewRowIndex Then
                            For Each c As DataGridViewCell In r.Cells
                                If Trim(c.Value.ToString.ToLower).Contains(Trim(Form1.TextBox2.Text.ToLower)) Then
                                    'If text_2.Contains(word) Then
                                    'c.Style.Font = highFont
                                    c.Style.BackColor = highColor
                                    'Else
                                    'c.Style.Font = defFont
                                    'c.Style.BackColor = defColor
                                End If
                            Next
                        End If
                    Next
                End If
    
            Next
    I am at this point and the cell with Text_1 is coloring me. I would just like to color the word of Text_1 in the cell

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: vb.net check if word exists

    To have multicoloured text in a dgvtextboxcell, you need to create a custom dgvtextboxcell, and override its paint event.
    Multicoloured text is not supported in a standard dgvtextboxcell…

  11. #11
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: vb.net check if word exists

    one way may be to bold the word in the sentence (not tested)
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  12. #12

    Thread Starter
    Member
    Join Date
    Oct 2022
    Posts
    40

    Re: [RESOLVED] vb.net check if word exist

    thanks everyone for the help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width