-
Dec 8th, 2018, 03:19 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Ignoe case in message box
Below is the code to delete a member from 2 listboxes.
What I want is to have the name that is typed in the message box to match the name in the two listboxes no matter how it is typed in the message box.
Code:
Private Sub btnDeleteMember_Click(sender As Object, e As EventArgs) Handles btnDeleteMember.Click
Dim delete_member As String = InputBox("Delete Member", "Delete " &
"Member")
If delete_member.Length = 0 Then Exit Sub
' delete members name.
lstHidden.Items.Remove(delete_member)
lstMembers.Items.Remove(delete_member)
End Sub
Learning is a never ending subject.
-
Dec 8th, 2018, 03:45 PM
#2
Re: Ignoe case in message box
Rather than having the user type a value (trying to get the case and spelling correct), it would be simpler for them (and you) to pick the item to delete based on which listbox item is selected, presumably with a MessageBox for confirmation (eg: "Are you sure you want to delete member: [value of selected] ?").
-
Dec 8th, 2018, 05:00 PM
#3
Thread Starter
Hyperactive Member
Re: Ignoe case in message box
Thanks Si,
I agree it would be better to choose the name and delete the selected, unfortunately if you tap a name in the lstMember box it automatically fires it off to lstMat1 when four names are in lstMat1 it fires off to lstMat2 then lstMat3 then lstMat4 after that the names go to lstMain.
I could just have a message box that shows if the name isn't correct with something like 'You must type the name as it appears in the list'
But just wondered if it was possible to go ignore case
Learning is a never ending subject.
-
Dec 8th, 2018, 05:32 PM
#4
Re: Ignoe case in message box
If all you want to do is ignore the case then you could loop through the ListBox items.
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim str = InputBox("Delete Member", "Delete Member").Trim
If str.Length > 0 Then
For Each itm In Me.ListBox1.Items
If itm.ToString.ToUpper = str.ToUpper Then
Me.ListBox1.Items.Remove(itm)
Exit For
End If
Next
End If
End Sub
-
Dec 9th, 2018, 03:37 AM
#5
Thread Starter
Hyperactive Member
Re: [RESOLVED] Ignoe case in message box
Thanks Wes,
Not only has it answered the question but I actually understand how it works.
Unfortunately can't leave a rep
Learning is a never ending subject.
-
Dec 9th, 2018, 03:47 AM
#6
Re: [RESOLVED] Ignoe case in message box
If you want to do a case-insensitive comparison then you should actually do a case-insensitive comparison, not change the case and do a case-sensitive comparison. The String.Equals method allows you to perform an actual case-insensitive comparison.
-
Dec 10th, 2018, 04:01 PM
#7
Thread Starter
Hyperactive Member
Re: [RESOLVED] Ignoe case in message box
Tried String.equals but it doesn't work. I guess because I'm comparing 2 strings that are not equal.
So what do I need to add that I'm not doing?
Code:
Dim strMatch = InputBox("Delete Member", "Delete Member").Trim
If strMatch.Length > 0 Then ' check inputbox has text in it
For Each itm In lstMembers.Items ' loop through membeslist
If String.Equals(itm, strMatch) Then ' if they match
lstMembers.Items.Remove(itm) ' remove from members list
lstHidden.Items.Remove(itm) ' remove from hidden list
End If
Next
End If
Learning is a never ending subject.
-
Dec 10th, 2018, 04:24 PM
#8
Re: [RESOLVED] Ignoe case in message box
I personally have never used this method, in fact I never new it existed. lol
But if you look at the documentation you see this method has various optional parameters.
Code:
If String.Equals(itm.ToString, str, StringComparison.CurrentCultureIgnoreCase)
I don't know if this is the best choice for you, I'll let you work that out.
You might want to use "OrdinalIgnoreCase"
Last edited by wes4dbt; Dec 10th, 2018 at 04:31 PM.
-
Dec 10th, 2018, 04:58 PM
#9
Re: [RESOLVED] Ignoe case in message box
Originally Posted by Mallard8
So what do I need to add that I'm not doing?
What you're not doing is paying attention to Intellisense or reading the documentation, both of which would have told you that String.Equals has multiple overloads and what the parameters of those overloads are for. When you want to know something about a type or member, reading the relevant documentation should ALWAYS be your first course of action. Only if you don't find what you need or understand what you find do you then need to look elsewhere.
-
Dec 10th, 2018, 05:26 PM
#10
Thread Starter
Hyperactive Member
Re: [RESOLVED] Ignoe case in message box
Stayed with ToUpper as I couldn't get String.Equals, String.Compare, StringComparison.CurrentCultureIgnoreCase or OrdinalIgnoreCase to work.
Learning is a never ending subject.
-
Dec 10th, 2018, 06:00 PM
#11
Re: [RESOLVED] Ignoe case in message box
Originally Posted by Mallard8
Stayed with ToUpper as I couldn't get String.Equals, String.Compare, StringComparison.CurrentCultureIgnoreCase or OrdinalIgnoreCase to work.
Then you did it wrong. Maybe, instead of giving up and doing the wrong thing, you should show us what you actually did and we can help you work out what's wrong with it.
-
Dec 10th, 2018, 06:07 PM
#12
Re: [RESOLVED] Ignoe case in message box
That said, it's rather simple stuff, particularly as wes4dbt has already shown you how.
vb.net Code:
Module Module1 Sub Main() Dim str1 = "Hello World" Dim str2 = "hello world" Dim str3 = "Goodbye cruel world" Compare(str1, str1) Compare(str1, str2) Compare(str1, str3) CompareInsensitive(str1, str1) CompareInsensitive(str1, str2) CompareInsensitive(str1, str3) Console.ReadLine() End Sub Sub Compare(a As String, b As String) If a = b Then Console.WriteLine("Compare: '{0}' is equal to '{1}'", a, b) Else Console.WriteLine("Compare: '{0}' is NOT equal to '{1}'", a, b) End If End Sub Sub CompareInsensitive(a As String, b As String) If String.Equals(a, b, StringComparison.CurrentCultureIgnoreCase) Then Console.WriteLine("CompareInsensitive: '{0}' is equal to '{1}'", a, b) Else Console.WriteLine("CompareInsensitive: '{0}' is NOT equal to '{1}'", a, b) End If End Sub End Module
-
Dec 10th, 2018, 06:21 PM
#13
Thread Starter
Hyperactive Member
Re: [RESOLVED] Ignoe case in message box
I did read lots of information but most of it conflicts with the other depending which 'expert' on which forum you read.
Here's one I tried
Code:
Private Sub btnDeleteMember_Click(sender As Object, e As EventArgs) Handles btnDeleteMember.Click
Dim strMatch = InputBox("Delete Member", "Delete Member").Trim
If strMatch.Length > 0 Then ' check inputbox has text in it
For Each itm In lstMembers.Items ' loop through membeslist
If String.Equals(itm.ToString, strMatch, StringComparison.CurrentCultureIgnoreCase) Then ' if they match
lstMembers.Items.Remove(itm) ' remove from members list
lstHidden.Items.Remove(itm) ' remove from hidden list
End If
Next
End If
End Sub
Here's the error
System.InvalidOperationException: 'List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.'
Learning is a never ending subject.
-
Dec 10th, 2018, 06:21 PM
#14
Re: [RESOLVED] Ignoe case in message box
If ToUpper worked, and the right version of Equals (CurrentCultureIgnoreCase) doesn't work, that would make me a bit nervous, so I'd be wanting to figure out what was different between the two strings. I don't know if it could be an issue, but could this be ASCII vs Unicode?
My usual boring signature: Nothing
-
Dec 10th, 2018, 06:24 PM
#15
Re: [RESOLVED] Ignoe case in message box
Originally Posted by Mallard8
I did read lots of information but most of it conflicts with the other depending which 'expert' on which forum you read.
Here's one I tried
Code:
Private Sub btnDeleteMember_Click(sender As Object, e As EventArgs) Handles btnDeleteMember.Click
Dim strMatch = InputBox("Delete Member", "Delete Member").Trim
If strMatch.Length > 0 Then ' check inputbox has text in it
For Each itm In lstMembers.Items ' loop through membeslist
If String.Equals(itm.ToString, strMatch, StringComparison.CurrentCultureIgnoreCase) Then ' if they match
lstMembers.Items.Remove(itm) ' remove from members list
lstHidden.Items.Remove(itm) ' remove from hidden list
End If
Next
End If
End Sub
Here's the error
System.InvalidOperationException: 'List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.'
Oh, that's a totally different issue. Ignore my last post.
You can't be doing removes on lists using a For Each. Items are changing positions when you do that. Instead, use a For Next and iterate backwards:
For x = lstMembers.Items.Count-1 To 0 Step -1
If String.Equals(lstMembers.Items(x).ToString, strMatch, StringComparison.CurrentCultureIgnoreCase) Then ' if they match
lstMembers.Items.RemoveAt(x) ' remove from members list
lstHidden.Items.RemoveAt(x) ' remove from hidden list
End If
Next
My usual boring signature: Nothing
-
Dec 10th, 2018, 06:27 PM
#16
Thread Starter
Hyperactive Member
Re: [RESOLVED] Ignoe case in message box
Shaggy, I don't know what it could be, I ain't that clever yet but I do enjoy the challenge of it all.
JMC, will look through waht you have suggested.
Learning is a never ending subject.
-
Dec 10th, 2018, 06:32 PM
#17
Thread Starter
Hyperactive Member
Re: [RESOLVED] Ignoe case in message box
Shaggy, what you have written now makes sense of the error message and explains why I was getting in such a pickle!
Turning in for the night will try it tomorrow morning and let you know how I get on.
Thank You
Learning is a never ending subject.
-
Dec 10th, 2018, 06:57 PM
#18
Re: [RESOLVED] Ignoe case in message box
We probably should have picked up that issue from the code in post #7 but, if you don't tell us what error message you get, we may miss things that we are not actually looking for. It's always important that you specify three things: what you're trying to do, how you're trying to do it and what happens when you try. Different error messages mean different issues and thus we'll be looking for different things. Of course, because this issue is unrelated to the comparison method, using ToUpper wouldn't have helped and you'd have got the same error at the same place.
-
Dec 10th, 2018, 07:21 PM
#19
Re: [RESOLVED] Ignoe case in message box
sh,
You can't be doing removes on lists using a For Each. Items are changing positions when you do that. Instead, use a For Next and iterate backwards:
You can if you're only going to be removing one item, then Exit the For loop as I did in my example. I guess maybe it's a good habit to always iterate backwards when your going to remove something. But either way will work for one item.
-
Dec 11th, 2018, 05:03 AM
#20
Thread Starter
Hyperactive Member
Re: [RESOLVED] Ignoe case in message box
Wes,
Tried what you suggested removing one item then exiting For loop, below is the code and the error.
Code:
Dim strMatch = InputBox("Delete Member", "Delete Member").Trim
For Each itm In lstMembers.Items
If String.Equals(lstMembers.Items(itm), strMatch, StringComparison.CurrentCultureIgnoreCase) Then ' if they match
lstMembers.Items.Remove(itm) ' remove from members list
lstHidden.Items.Remove(itm) ' remove from hidden list
Exit For
End If
Next
System.InvalidCastException: 'Conversion from string "Jan" to type 'Integer' is not valid.'
Inner Exception
FormatException: Input string was not in a correct format.
When I step in to it the first name in the lstMembers shows Jan the name I'm looking to delete (strMatch) shows mAndY.
So if I'm asking is Jan = mAndY, where does the string to integer come in to it?
Learning is a never ending subject.
-
Dec 11th, 2018, 05:56 AM
#21
Re: [RESOLVED] Ignoe case in message box
Code:
For Each itm In lstMembers.Items
If String.Equals(lstMembers.Items(itm), strMatch, StringComparison.CurrentCultureIgnoreCase) Then ' if they match
Why are you trying to get the item at index 'itm' when 'itm' isn't an index of an item but an item? You already have the item so you don't need to get the item again by index, especially when you don't actually have an index.
Code:
For Each itm In lstMembers.Items
If String.Equals(itm, strMatch, StringComparison.CurrentCultureIgnoreCase) Then ' if they match
-
Dec 11th, 2018, 09:59 AM
#22
Thread Starter
Hyperactive Member
Re: [DEFINITELY NOW RESOLVED] Ignore case in message box.
JMC,
Why are you trying to get the item at index 'itm' when 'itm' isn't an index of an item but an item?
Because I don't know any better but now you have explained it in your own special way, which always makes me smile. Thank you I can see, what is now, the obvious.
Below are all three that I have tried and which worked, any others wanting to know how to compare strings in a list can see all three in one place.
Thanks to everyone who subscribed to this thread, I have actually learnt a little more of VB.
Code:
'case-sensitive comparison
' changing case of both strings to Upper Case (ToUpper) or lower case (ToLower)
Dim strMatch = InputBox("Delete Member", "Delete Member").Trim
For Each itm In lstMembers.Items ' loop through membeslist
If itm.ToString.ToUpper = strMatch.ToUpper Then 'change both to upper case if they match
lstMembers.Items.Remove(itm) ' remove from members list
lstHidden.Items.Remove(itm) ' remove fron hidden list
Exit For 'exit the loop
End If 'if they don't match
Next 'go again
'-----------------------------------------------------------------------------------------------
' case-insensitive comparison
' iterating through items in a listbox in reverse
Dim strMatch = InputBox("Delete Member", "Delete Member").Trim
For itm = lstMembers.Items.Count - 1 To 0 Step -1 'go through list box in reverse
If String.Equals(lstMembers.Items(itm).ToString, strMatch, StringComparison.CurrentCultureIgnoreCase) Then ' if they match
lstMembers.Items.RemoveAt(itm) ' remove from members list
lstHidden.Items.RemoveAt(itm) ' remove from hidden list
End If 'if they don't match
Next 'go again
'---------------------------------------------------------------------------------------------------
'case-insensitive comparison
'going through items in list box when a match is found exit the loop
Dim strMatch = InputBox("Delete Member", "Delete Member").Trim
For Each itm In lstMembers.Items ' go through items in listbox
If String.Equals(itm, strMatch, StringComparison.CurrentCultureIgnoreCase) Then ' if they match
lstMembers.Items.Remove(itm) ' remove from members list
lstHidden.Items.Remove(itm) ' remove from hidden list
Exit For 'found a match exit the for loop
End If 'if they don't match
Next 'go again
Learning is a never ending subject.
-
Dec 11th, 2018, 06:27 PM
#23
Re: [DEFINITELY NOW RESOLVED] Ignore case in message box.
Originally Posted by Mallard8
Because I don't know any better
You need to read your code objectively, i.e. ask yourself what each line is doing and why. You should actually say it out loud because, as we know, actually hearing ourselves say something often makes what's wrong with it obvious when it wasn't by just thinking it. That's why we can often solve problems by talking to someone without their saying a word. Speaking out loud helps us organise our thoughts better. Speaking out loud makes it easier to see what code is actually rather than what you intended it to do. If you had read out loud what that code was actually doing then you'd have seen that it didn't really make sense. As you say, it's obvious now that it's been pointed out but reading it out loud for yourself is almost as good.
-
Dec 12th, 2018, 03:23 AM
#24
Thread Starter
Hyperactive Member
Re: [RESOLVED] Ignoe case in message box
John,
Thank you for the advice, I must admit that is something I've never tried but will give it a go.
Graham
Learning is a never ending subject.
-
Dec 12th, 2018, 03:38 AM
#25
Re: [RESOLVED] Ignoe case in message box
Originally Posted by Mallard8
I must admit that is something I've never tried but will give it a go.
You may be surprised. When we just look, we often see what we expect to see. Even if you read it in your head but read it properly rather than just looking at the words, you'll often see what's actually there when you previously saw what you thought was there. If you read aloud though, you ensure that you don't cheat when reading in your head and get no benefit from it.
-
Dec 12th, 2018, 04:31 AM
#26
Thread Starter
Hyperactive Member
Re: [RESOLVED] Ignoe case in message box
It would appear similar to 'You can't see the wood for the trees'
I have now posted it in the Application testing forum so I can get feedback on how I've coded it.
Thanks for all your help in this thread and others.
Learning is a never ending subject.
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
|