-
Mar 21st, 2023, 03:04 AM
#1
Thread Starter
Member
[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.
-
Mar 21st, 2023, 03:46 AM
#2
Re: vb.net check if word exists
Originally Posted by Marco:G
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.
-
Mar 21st, 2023, 03:48 AM
#3
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)
-
Mar 21st, 2023, 03:56 AM
#4
Thread Starter
Member
Re: vb.net check if word exists
Originally Posted by jmcilhinney
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
-
Mar 21st, 2023, 04:05 AM
#5
Re: vb.net check if word exists
Originally Posted by Marco:G
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.
-
Mar 21st, 2023, 04:22 AM
#6
Thread Starter
Member
Re: vb.net check if word exists
Originally Posted by Delaney
Thanks so much, that's what I was looking for. Super.
-
Mar 21st, 2023, 04:23 AM
#7
Thread Starter
Member
Re: vb.net check if word exists
Originally Posted by jmcilhinney
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.
-
Mar 21st, 2023, 08:47 AM
#8
Thread Starter
Member
Re: vb.net check if word exists
Originally Posted by Delaney
I'm sorry, but there is also a way to highlight keywords permanently
-
Mar 21st, 2023, 10:30 AM
#9
Thread Starter
Member
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
-
Mar 21st, 2023, 01:59 PM
#10
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…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 21st, 2023, 04:31 PM
#11
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)
-
Mar 24th, 2023, 11:03 AM
#12
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|