none
find duplicate character or Symbols or space more then one in textbox In consecutive ? RRS feed

  • Question

  • I want find duplicate character or Symbols or space more then one in textbox when find duplicate message box use Event TextChanged:

    example :

    when character  in textbox = (cann)  then message box ("duplicate character ")

    when Symbols in textbox = (amric$$)  then message box ("duplicate Symbols ")

    when space in textbox = (amric jo  n) then message box ("duplicate space ")


    • Edited by srajmuneer Monday, May 22, 2017 8:18 PM
    Monday, May 22, 2017 8:16 PM

Answers

  • Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        For i = 0 To TextBox1.TextLength - 2
            Dim c = TextBox1.Text(i)
            If c = TextBox1.Text(i + 1) Then
                If Char.IsSymbol(c) Then
                    MessageBox.Show("Duplicate Symbol")
                ElseIf Char.IsWhiteSpace(c) Then
                    MessageBox.Show("Duplicate Space")
                Else
                    MessageBox.Show("Duplicate Character")
                End If
            End If
        Next
    End Sub

    Loop through the characters, check for duplicates, then determine the kind of duplicate when found.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Monday, May 22, 2017 8:56 PM
    Moderator
  • Hi srajmuneer,

    Please refer to the code below.

     Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
            For i = 0 To TextBox3.TextLength - 2
                For j = i To TextBox3.TextLength - 2
                    Dim c = TextBox3.Text(i)
                    If c = TextBox3.Text(j + 1) Then
                        If Char.IsSymbol(c) Then
                            MessageBox.Show("Duplicate Symbol")
                        ElseIf Char.IsWhiteSpace(c) Then
                            MessageBox.Show("Duplicate Space")
                        Else
                            MessageBox.Show("Duplicate Character")
                        End If
                    End If
                Next
            Next
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by srajmuneer Tuesday, May 23, 2017 9:33 AM
    Tuesday, May 23, 2017 6:07 AM
    Moderator

All replies

  • Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        For i = 0 To TextBox1.TextLength - 2
            Dim c = TextBox1.Text(i)
            If c = TextBox1.Text(i + 1) Then
                If Char.IsSymbol(c) Then
                    MessageBox.Show("Duplicate Symbol")
                ElseIf Char.IsWhiteSpace(c) Then
                    MessageBox.Show("Duplicate Space")
                Else
                    MessageBox.Show("Duplicate Character")
                End If
            End If
        Next
    End Sub

    Loop through the characters, check for duplicates, then determine the kind of duplicate when found.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Monday, May 22, 2017 8:56 PM
    Moderator
  • I was curious if I could do it without a loop. It can but not with a TextChanged event like you wrote.

    Public Class Form1
        Private Sub TextBox1_TextChanged(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
            Dim c = e.KeyChar
            If TextBox1.Text.IndexOf(c) <> -1 Then
                If Char.IsSymbol(c) Then
                    MessageBox.Show("Duplicate Symbol")
                ElseIf Char.IsWhiteSpace(c) Then
                    MessageBox.Show("Duplicate Space")
                Else
                    MessageBox.Show("Duplicate Character")
                End If
                e.Handled = True
            End If
        End Sub
    End Class
    Be aware it is just a variation of Reeds code, therefore if you want to mark this as answer, than don't forget to do the same with Reeds code. It does what you ask. I tested it. 


    Success
    Cor

    Monday, May 22, 2017 11:06 PM
  • Hi srajmuneer,

    Please refer to the code below.

     Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
            For i = 0 To TextBox3.TextLength - 2
                For j = i To TextBox3.TextLength - 2
                    Dim c = TextBox3.Text(i)
                    If c = TextBox3.Text(j + 1) Then
                        If Char.IsSymbol(c) Then
                            MessageBox.Show("Duplicate Symbol")
                        ElseIf Char.IsWhiteSpace(c) Then
                            MessageBox.Show("Duplicate Space")
                        Else
                            MessageBox.Show("Duplicate Character")
                        End If
                    End If
                Next
            Next
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by srajmuneer Tuesday, May 23, 2017 9:33 AM
    Tuesday, May 23, 2017 6:07 AM
    Moderator
  • The problem can be solved with Regular Expressions too:

    Dim example = " cann amric$$ jo    n" ' includes more problems
    Dim message As String = ""
    
    If Regex.IsMatch(example, "(\p{L})\1") Then message &= "duplicate letter" & vbCrLf
    If Regex.IsMatch(example, "(\p{S})\1") Then message &= "duplicate symbol" & vbCrLf
    If Regex.IsMatch(example, "\s\s") Then message &= "duplicate space" & vbCrLf
    
    If message <> "" Then
       ' show the message
       ' . . .
    End If


    • Edited by Viorel_MVP Tuesday, May 23, 2017 6:24 AM
    • Proposed as answer by Cor Ligthert Tuesday, May 23, 2017 11:33 AM
    Tuesday, May 23, 2017 6:23 AM
  • thanks Viorel_ , but show error :

    can find duplicate  character and Symbols and space OR duplicate  character and Symbols only ...... so

    when duplicate character and Symbols  in textbox = (cann %%)  then message box ("duplicate character - duplicate Symbols") ...... so

    Tuesday, May 23, 2017 3:40 PM
  • Click “Show potential fixes” or press <Ctrl+.>, then select the suggestion to insert ‘Imports System.Text.RegularExpressions’, or type it manually at the beginning of the file.


    • Edited by Viorel_MVP Tuesday, May 23, 2017 5:06 PM
    Tuesday, May 23, 2017 5:05 PM
  • how to pass textbox in your code ?

    Imports System.Text.RegularExpressions
    
    Public Class Form1
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
               Dim message As String = ""
    
            If Regex.IsMatch(TextBox1.Text, "(\p{L})\1") Then message &= "duplicate letter" & vbCrLf
            If Regex.IsMatch(TextBox1.Text, "(\p{S})\1") Then message &= "duplicate symbol" & vbCrLf
            If Regex.IsMatch(TextBox1.Text, "\s\s") Then message &= "duplicate space" & vbCrLf
    
            If message <> "" Then
                MessageBox.Show = (message)
            End If
        End Sub
    End Class


    I want :

    when duplicate character and Symbols  in textbox = (cann %%)  then message box ("duplicate character - duplicate Symbols") ...... so




    • Edited by srajmuneer Tuesday, May 23, 2017 5:23 PM
    Tuesday, May 23, 2017 5:18 PM
  • thanks Reed Kimble , can find duplicate  character and Symbols and space OR duplicate  character and Symbols only ...... so

    when duplicate character and Symbols  in textbox = (cann %%)  then message box ("duplicate character - duplicate Symbols") ...... so

    Wednesday, May 24, 2017 4:36 AM