Results 1 to 26 of 26

Thread: [RESOLVED] Ignoe case in message box

  1. #1

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    Resolved [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.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    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] ?").

  3. #3

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    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.

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    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

  5. #5

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    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.

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

    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.

  7. #7

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    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.

  8. #8
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    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.

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

    Re: [RESOLVED] Ignoe case in message box

    Quote Originally Posted by Mallard8 View Post
    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.

  10. #10

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    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.

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

    Re: [RESOLVED] Ignoe case in message box

    Quote Originally Posted by Mallard8 View Post
    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.

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

    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:
    1. Module Module1
    2.  
    3.     Sub Main()
    4.         Dim str1 = "Hello World"
    5.         Dim str2 = "hello world"
    6.         Dim str3 = "Goodbye cruel world"
    7.  
    8.         Compare(str1, str1)
    9.         Compare(str1, str2)
    10.         Compare(str1, str3)
    11.         CompareInsensitive(str1, str1)
    12.         CompareInsensitive(str1, str2)
    13.         CompareInsensitive(str1, str3)
    14.  
    15.         Console.ReadLine()
    16.     End Sub
    17.  
    18.     Sub Compare(a As String, b As String)
    19.         If a = b Then
    20.             Console.WriteLine("Compare: '{0}' is equal to '{1}'", a, b)
    21.         Else
    22.             Console.WriteLine("Compare: '{0}' is NOT equal to '{1}'", a, b)
    23.         End If
    24.     End Sub
    25.  
    26.     Sub CompareInsensitive(a As String, b As String)
    27.         If String.Equals(a, b, StringComparison.CurrentCultureIgnoreCase) Then
    28.             Console.WriteLine("CompareInsensitive: '{0}' is equal to '{1}'", a, b)
    29.         Else
    30.             Console.WriteLine("CompareInsensitive: '{0}' is NOT equal to '{1}'", a, b)
    31.         End If
    32.     End Sub
    33.  
    34. End Module

  13. #13

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    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.

  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    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

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: [RESOLVED] Ignoe case in message box

    Quote Originally Posted by Mallard8 View Post
    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

  16. #16

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    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.

  17. #17

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    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.

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

    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.

  19. #19
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    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.

  20. #20

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    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.

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

    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

  22. #22

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    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.

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

    Re: [DEFINITELY NOW RESOLVED] Ignore case in message box.

    Quote Originally Posted by Mallard8 View Post
    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.

  24. #24

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    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.

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

    Re: [RESOLVED] Ignoe case in message box

    Quote Originally Posted by Mallard8 View Post
    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.

  26. #26

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    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
  •  



Click Here to Expand Forum to Full Width