-
Mar 11th, 2023, 05:36 AM
#1
Thread Starter
Addicted Member
How to get Msgbox Display the List of Non-Matched Value of Arrays of String ?
Hello
Does anyone know how to get Msgbox Display the List of Non Matched Value of Array in String ?
I've gone through the following
https://stackoverflow.com/questions/...ems-using-linq
Everything seems going over and above my brains.
For Eg
if Textbox1 contains following Data ie
Then Msgbox to Display the Following list as
Your Textbox1 Does Not Contain
The
Qucikly
Jumped
the
Dog
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim arrayString() As String = {The,Brown,Fox,Qucikly,Jumped,over,the,Lazy,Dog}
For j As Integer = LBound(arrayString) To UBound(arrayString)
If Not Textbox1.Text.Contains(arrayString(j)) Then
MsgBox("Yor Textbox1 Does Not Contain" & LIST of Non Mathced Value)
End If
Next J
End Sub
SamD
113
Last edited by SamDsouza; Mar 11th, 2023 at 05:41 AM.
-
Mar 11th, 2023, 07:00 AM
#2
Re: How to get Msgbox Display the List of Non-Matched Value of Arrays of String ?
The message box is irrelevant. It simply displays a String. Where that String comes from makes no difference- a String is a String.
If you want to display a single String containing a list of values then you obviously don't want to display it inside a loop. The point of a loop is to do something multiple times. Think about the logic first, then write code to implement that logic. You have a list of words you want to match. For each word in that list, you need to check if it matches and, if it doesn't, put it into another list. Once you're done checking, combine that list into a single String, then display that String.
Now that you have the steps involved, you can address each step individually. For instance, do you know how to combine a list of Strings into a single String? If not, you can research that first. Where the list came from is irrelevant, so you don't have to be able to do the rest to get the list first before working out how to combine the list. Etc.
-
Mar 11th, 2023, 06:34 PM
#3
Re: How to get Msgbox Display the List of Non-Matched Value of Arrays of String ?
Code:
MsgBox("Yor Textbox1 Does Not Contain" & Environment.NewLine & String.Join(Environment.NewLine, YourList.ToArray))
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 12th, 2023, 06:07 AM
#4
Thread Starter
Addicted Member
Re: How to get Msgbox Display the List of Non-Matched Value of Arrays of String ?
.Paul
Thanks for the Syntax Provided
jmcilhinney
For each word in that list, you need to check if it matches and, if it doesn't, put it into another list. Once you're done checking, combine that list into a single String, then display that String.
Thanks for the logic provided
For instance, do you know how to combine a list of Strings into a single String? If not, you can research that first. Where the list came from is irrelevant, so you don't have to be able to do the rest to get the list first before working out how to combine the list. Etc.
Maximum researched
On basis of above and syntax provided
In fact lead into Cloud of Confussion but nevertheless
Yes I tried the Following and succeeded
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim arrayString() As String = {The,Brown,Fox,Qucikly,Jumped,over,the,Lazy,Dog}
Dim nonMatchedList as New List(of String)'(arrayString)
For Each item in arrayString
If Not (Textbox1.Text.Contains(item) Then
nonMatchedList.Add(item)
End If
Next
MsgBox("Yor Textbox1 Does Not Contain" & Environment.NewLine & String.Join(Environment.NewLine, nonMatchedList.ToArray))
End Sub
By the way what would the Following Syntax mean
Dim nonMatchedList as New List(of String)(arrayString)
Thanks Once Again.
SamD
114
-
Mar 12th, 2023, 06:43 AM
#5
Re: How to get Msgbox Display the List of Non-Matched Value of Arrays of String ?
https://learn.microsoft.com/en-us/do...t?view=net-7.0 is a possibility instead of doing it yourself.
However there are a few issues with the code you posted, firstly your array initialisation wont compile because the values need to be in quotes..
Code:
Dim arrayString() As String = {"The", "Brown", "Fox", "Qucikly", "Jumped", "over", "the", "Lazy", "Dog"}
The line of code .
Code:
Dim nonMatchedList as New List(of String)'(arrayString)
is simply declaring and instantiating a list of strings, the '(arrayString) is just a comment - a meaningless comment as well.
Code:
If Not TextBox1.Text.Contains(item) Then
Is either missing a ) at the end or has an extra ( at the start and won't actually compile.
I would also suggest using MessageBox.Show instead of MsgBox, MsgBox is throwback to VB6 while MessageBox.Show is the newer .Net approach.
-
Mar 12th, 2023, 08:01 AM
#6
Thread Starter
Addicted Member
Re: How to get Msgbox Display the List of Non-Matched Value of Arrays of String ?
PlausibliyDamp
Typo Error:
Corrections Made
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim arrayString() As String = {"The","Brown","Fox","Qucikly","Jumped","over","the","Lazy","Dog"}
Dim nonMatchedList as New List(of String)
For Each item in arrayString
If Not Textbox1.Text.Contains(item) Then
nonMatchedList.Add(item)
End If
Next
MsgBox("Yor Textbox1 Does Not Contain" & Environment.NewLine & String.Join(Environment.NewLine, nonMatchedList.ToArray))
End Sub
Although i tried above and suceeded but was wanting to see the possibility
with the Original loop as #1
For j As Integer = LBound(arrayString) To UBound(arrayString)
SamD
115
Last edited by SamDsouza; Mar 12th, 2023 at 08:13 AM.
-
Mar 12th, 2023, 08:19 AM
#7
Re: How to get Msgbox Display the List of Non-Matched Value of Arrays of String ?
Originally Posted by SamDsouza
PlausibliyDamp
Typo Error:
Corrections Made
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim arrayString() As String = {"The","Brown","Fox","Qucikly","Jumped","over","the","Lazy","Dog"}
Dim nonMatchedList as New List(of String)
For Each item in arrayString
If Not Textbox1.Text.Contains(item) Then
nonMatchedList.Add(item)
End If
Next
MsgBox("Yor Textbox1 Does Not Contain" & Environment.NewLine & String.Join(Environment.NewLine, nonMatchedList.ToArray))
End Sub
Although i tried above and suceeded but was wanting to see the possibility
with the Original loop as #1
For j As Integer = LBound(arrayString) To UBound(arrayString)
SamD
115
You could change the loop bit to
Code:
For j As Integer = LBound(arrayString) To UBound(arrayString)
If Not TextBox1.Text.Contains(arrayString(j)) Then
nonMatchedList.Add(arrayString(j))
End If
Next
although I can't think of a compelling reason to do so, it makes the code harder to read and uses even more legacy VB6 methods (LBound and UBound), if you wanted to at least do that and keep it a bit more .Net then I suppose something like
Code:
For j As Integer = arrayString.GetLowerBound(0) To arrayString.GetUpperBound(0)
If you really insist on using a For Next loop then
Code:
For j As Integer = 0 To arrayString.Length - 1
would be a cleaner way - you know the starting element is 0 and the last one is the Length - 1 because you declared the array without specifying any upper or lower bounds.
Again I am not seeing any real benefits. Stick with the For Each in my opinion.
Last edited by PlausiblyDamp; Mar 12th, 2023 at 08:46 AM.
-
Mar 13th, 2023, 12:54 AM
#8
Thread Starter
Addicted Member
Re: How to get Msgbox Display the List of Non-Matched Value of Arrays of String ?
PlausibilyDamp
Thanks for the Options provided and encouraging me to move towards VB.Net. Appreciate it.
BTW I am only Familar with VBA and i dont know the Heads or Tails of VB6 and Earlier versions & Till today have not seen the Programing Enviornemt of VB6
For Eg I would Like to Replace arrayString(j) with Something Else. How this Could be coded in For Each Statement. These are for all the (Matched) items from Lbound(arraystring) to Ubound(arraystring)
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim arrayString() As String = {"The","Brown","Fox","Qucikly","Jumped","over","the","Lazy","Dog"}
Dim nonMatchedList as New List(of String)
For Each item in arrayString
If Not Textbox1.Text.Contains(item) Then
nonMatchedList.Add(item)
End If
Next
MsgBox("Yor Textbox1 Does Not Contain" & Environment.NewLine & String.Join(Environment.NewLine, nonMatchedList.ToArray))
End Sub
Logic to repalce was already coded but somehting happened and all the data was not matching and ie why i had to post this thread
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim arrayString() As String = {"The","Brown","Fox","Qucikly","Jumped","over","the","Lazy","Dog"}
Dim Strtxt As String
For j As Integer = LBound(arrayString) To UBound(arrayString)
Strtxt = Replace(Strtxt, arrayStrings(j), arrayStrings(j).PadRight(18))
Next J
End Sub
SamD
116
-
Mar 13th, 2023, 01:08 AM
#9
Re: How to get Msgbox Display the List of Non-Matched Value of Arrays of String ?
Code:
Dim arrayString() As String = {"The","Brown","Fox","Qucikly","Jumped","over","the","Lazy","Dog"}
Dim nonMatchedList as New List(of String)(arrayString.Where(Function(s) Not Textbox1.Text.Contains(s)) .ToArray)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 13th, 2023, 10:17 AM
#10
Thread Starter
Addicted Member
Re: How to get Msgbox Display the List of Non-Matched Value of Arrays of String ?
.Paul,
Code:
Dim arrayString() As String = {"The","Brown","Fox","Qucikly","Jumped","over","the","Lazy","Dog"}
Dim nonMatchedList as New List(of String)(arrayString.Where(Function(s) Not Textbox1.Text.Contains(s)).ToArray)
'Added the syntax below as #3
MsgBox("Yor Textbox1 Does Not Contain" & Environment.NewLine & String.Join(Environment.NewLine, nonMatchedList.ToArray))
this has indeed worked perfectly for Non Matched items without any for Next Loop and if then Condition....
The difficulty now i am facing is for if All Matched Items List then i dont want to display the above message. How can i go ahead ?
SamD
117
-
Mar 13th, 2023, 10:44 AM
#11
Re: How to get Msgbox Display the List of Non-Matched Value of Arrays of String ?
You only need to display the messagebox if nonMatchedList has 1 or more items in it, right?
https://learn.microsoft.com/en-us/do...t?view=net-8.0
-
Mar 14th, 2023, 05:49 AM
#12
Thread Starter
Addicted Member
Re: How to get Msgbox Display the List of Non-Matched Value of Arrays of String ?
OptionBase1
Thanks for providing the Link where Dinosaur example made things clear. But unable to workout. ?
You only need to display the messagebox if nonMatchedList has 1 or more items in it, right?
Here the result is Msgbox Displays if the items are Less with the List of nonMatchedList values.
As well it displays if Matched Items are Equal But without the nonMatchedList values
Unsuccessful
Code:
For Each item in arrayString
If Textbox1.Text.Contains(item) Then
If arrayString.Count <> Ubound(arrayString) Then
Dim nonMatchedList as New List(of String)(arrayString.Where(Function(s) Not Textbox1.Text.Contains(s)).ToArray)
MsgBox("Your Textbox1 Does Not Contain" & Environment.NewLine & String.Join(Environment.NewLine, nonMatchedList.ToArray))
Else
For j As Integer = LBound(arrayString) To UBound(arrayString)
item = Replace(item, arrayStrings(j), arrayStrings(j).PadRight(18))
Next
End If
End If
Next
Unsuccessful
SamD
118
-
Mar 14th, 2023, 06:28 AM
#13
Re: How to get Msgbox Display the List of Non-Matched Value of Arrays of String ?
Originally Posted by SamDsouza
OptionBase1
Thanks for providing the Link where Dinosaur example made things clear. But unable to workout. ?
Here the result is Msgbox Displays if the items are Less with the List of nonMatchedList values.
As well it displays if Matched Items are Equal But without the nonMatchedList values
Unsuccessful
Code:
For Each item in arrayString
If Textbox1.Text.Contains(item) Then
If arrayString.Count <> Ubound(arrayString) Then
Dim nonMatchedList as New List(of String)(arrayString.Where(Function(s) Not Textbox1.Text.Contains(s)).ToArray)
MsgBox("Your Textbox1 Does Not Contain" & Environment.NewLine & String.Join(Environment.NewLine, nonMatchedList.ToArray))
Else
For j As Integer = LBound(arrayString) To UBound(arrayString)
item = Replace(item, arrayStrings(j), arrayStrings(j).PadRight(18))
Next
End If
End If
Next
Unsuccessful
SamD
118
When your code doesn't do what you expect you need to learn how to debug it. Try putting a breakpoint on the first line of that code and step through it. Does the code do what you expect each time you step? If not, try and figure out why not... How did you get to that final bit of code from the previous "working" code?
However some things to think about in your code.
What do you think the following code is doing, and why are you doing it?
Code:
If arrayString.Count <> Ubound(arrayString) Then
What does the following line do, and what is the consequence of doing it inside of a loop?
Code:
Dim nonMatchedList as New List(of String)(arrayString.Where(Function(s) Not Textbox1.Text.Contains(s)).ToArray)
What is the following code doing and when will it run in your example?
Code:
For j As Integer = LBound(arrayString) To UBound(arrayString)
item = Replace(item, arrayStrings(j), arrayStrings(j).PadRight(18))
Next
Try and debug your code / think about your code and see if you can answer those three questions. Once you have answered them, does it do what you are intending?
Last edited by PlausiblyDamp; Mar 14th, 2023 at 07:07 AM.
-
Mar 14th, 2023, 06:42 AM
#14
Re: How to get Msgbox Display the List of Non-Matched Value of Arrays of String ?
@Sam
take a look at Linq = Intersect and Except methods
https://dotnettutorials.net/lesson/l...ersect-method/
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Mar 14th, 2023, 09:14 AM
#15
Re: How to get Msgbox Display the List of Non-Matched Value of Arrays of String ?
So using Textbox1.Text.Contains is not a good way to go IMHO. In the following code I split the text into words using some common separators. Once that is done the results are fairly easy.
Code:
' TB1 text
' Fox, over, Lazy, Brown fox
Dim checkWords As New List(Of String) From {"The", "Brown", "Fox", "Qucikly", "Jumped", "over", "the", "Lazy", "Dog"}
Const seps As String = " ,.;:-!?"
Dim tb1Words As List(Of String)
tb1Words = TextBox1.Text.Split(seps.ToArray, StringSplitOptions.RemoveEmptyEntries).ToList
Dim noMatch As IEnumerable(Of String)
noMatch = checkWords.Intersect(tb1Words)
noMatch = checkWords.Except(noMatch)
Dim rslts As String = String.Join(", ", noMatch)
EDIT: I see ChrisE and I think alike...
Last edited by dbasnett; Mar 14th, 2023 at 11:46 AM.
-
Mar 14th, 2023, 10:00 AM
#16
Re: How to get Msgbox Display the List of Non-Matched Value of Arrays of String ?
Originally Posted by SamDsouza
OptionBase1
Thanks for providing the Link where Dinosaur example made things clear. But unable to workout. ?
Here the result is Msgbox Displays if the items are Less with the List of nonMatchedList values.
As well it displays if Matched Items are Equal But without the nonMatchedList values
Unsuccessful
Code:
For Each item in arrayString
If Textbox1.Text.Contains(item) Then
If arrayString.Count <> Ubound(arrayString) Then
Dim nonMatchedList as New List(of String)(arrayString.Where(Function(s) Not Textbox1.Text.Contains(s)).ToArray)
MsgBox("Your Textbox1 Does Not Contain" & Environment.NewLine & String.Join(Environment.NewLine, nonMatchedList.ToArray))
Else
For j As Integer = LBound(arrayString) To UBound(arrayString)
item = Replace(item, arrayStrings(j), arrayStrings(j).PadRight(18))
Next
End If
End If
Next
Unsuccessful
SamD
118
So, you had three lines of code that were working as per your response to Paul, except for one condition, and then you add in nonsensical nested loops for what reason?
There's a reason I didn't give you a simple copy/paste solution. This is a programming forum, where the flow of "mental logic <---> code" needs to be able to go both directions.
You had a simple situation where if a value is >= 1, then do something, if it isn't, then do nothing. I pointed you in the direction of how to access that value. That should be a situation where the logic is plainly written in code. If not, then you've missed some very fundamental pieces of understanding VB.NET code in your programming journey.
Good luck.
-
Mar 17th, 2023, 03:29 AM
#17
Thread Starter
Addicted Member
Re: How to get Msgbox Display the List of Non-Matched Value of Arrays of String ?
OptionBase1
There's a reason I didn't give you a simple copy/paste solution. This is a programming forum, where the flow of "mental logic <---> code" needs to be able to go both directions.
Really Appreciate
You had a simple situation where if a value is >= 1, then do something, if it isn't, then do nothing
Am really confused of which value are you referring to
if not, then you've missed some very fundamental pieces of understanding VB.NET code in your programming journey
Although gone thru quite a lot of references. Still i am unable to meet the fundamental logic. I give Up
My Last trial as Below Code : Though it displays the List of nonmatched values. But when all are matched still Blank msgbox displays. How to prevent this blank msgbox appearing.
Code:
If Not Textbox1.Text.Contains(nonMatchedList.ToString)
MsgBox("Your Textbox1 Does Not Contain" & Environment.NewLine & String.Join(Environment.NewLine, nonMatchedList.ToArray))
End IF
ChrisE
Thanks for beautiful Tutorial links you have provided. Will definately work with intersect method and come up. But not Right now because for simple coding #17 like above.
I would like to know where i have gone wrong.
SamD
119
Last edited by SamDsouza; Mar 17th, 2023 at 03:34 AM.
-
Mar 17th, 2023, 09:44 AM
#18
Re: How to get Msgbox Display the List of Non-Matched Value of Arrays of String ?
Originally Posted by SamDsouza
Am really confused of which value are you referring to
https://www.vbforums.com/showthread....=1#post5598405
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
|