locked
How can i replace letter with another letter using 2 textboxes? RRS feed

  • Question

  • How can i replace letter with another letter using 2 textboxes?

    what I want to create is: a Flip text program

    example :

    Normal text ------------> Flip text

    Flipped text ------------> ʇxǝʇ dılɟ

    as you can see:
    F converted to --> ɟ

    l  converted to --> l

    i  converted to --> ı

    p  converted to --> d

    t  converted to --> ʇ

    e  converted to --> ǝ

    x  converted to --> x

    t  converted to -->  ʇ

    I tried it with one Letter and it works (I used the If condition  in a Button to make the letter flips--> if Textbox1.Text = "a" then Textbox2.text = "ɐ") but the problem is I don't know how to make it work if the user type a whole sentence

    NB: Im using Visual Basic 2010


    • Edited by facelool Tuesday, June 26, 2012 2:34 PM
    Tuesday, June 26, 2012 2:16 PM

Answers

  • I'm very curious to know how you can type those reversed characters !

    To anwer your question, follow Stanav's and Armin's advices :

    Tranform your code into a function. Something like this :

        Private Function FlipChar(sign As Char) As Char
    'Add here all your logic
    '.....
    if sign = "u" then return "n"
    if sign = "z" then return "z"
        End Function
    

    Then, in your textBox.ChangedText event, call it for each letter :

        Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
            Dim result As String = String.Empty
            For Each sign As Char In TextBox1.Text
                result &= FlipChar(sign)
            Next
            TextBox2.Text = result
        End Sub
    

    • Marked as answer by facelool Tuesday, June 26, 2012 3:57 PM
    Tuesday, June 26, 2012 3:22 PM
  • Good !
    Once this works, you may want to use a Dictionary (of Char,Char). This is a list of characters (the key) associated with another character (the value). It is probably easier to handle than a long list of "If..... Then....".

    In this case, your code is :

     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            LoadSignsDict()
        End Sub
        Private SignsDict As Dictionary(Of Char, Char)
        Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
            Dim result As String = String.Empty
            For Each sign As Char In TextBox1.Text
                result &= FlipChar(sign)
            Next
            TextBox2.Text = result
        End Sub
        Private Function FlipChar(sign As Char) As Char
            'This function reads the dictionary of signs and returns the flipped sign
    
            If SignsDict.ContainsKey(sign) Then Return SignsDict(sign)
            Return sign
        End Function
        Private Sub LoadSignsDict()
            'This function is called only once and loads the dictionary of signs
            SignsDict = New Dictionary(Of Char, Char)
            SignsDict.Add("u"c, "n"c)
            SignsDict.Add("n"c, "u"c)
            SignsDict.Add("t"c, Convert.ToChar(647))
        End Sub

    Notice that you should load the Dictionary with Char : this is what the letter 'c' means.

    You can also load a converted Unicode value into the Dictionary.

    • Marked as answer by facelool Tuesday, June 26, 2012 4:13 PM
    Tuesday, June 26, 2012 3:53 PM
  • but it works just in case I just one Letter! but it don't work when I type 2 letters or more (i mean a word ) !! how can i make it Please??

    Public Class Form1
    
       Private Shared ReadOnly FlipDigits As New Dictionary(Of Char, Char)
    
       Shared Sub New()
    
          FlipDigits.Add("a"c, "ɐ"c)
          FlipDigits.Add("b"c, "q"c)
          '...
    
       End Sub
    
       Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    
          Dim sb As New System.Text.StringBuilder
    
          For Each c In TextBox1.Text
             Dim c2 As Char
             If FlipDigits.TryGetValue(c, c2) Then
                c = c2
             End If
             sb.Append(c)
          Next
    
          TextBox2.Text = sb.ToString
    
       End Sub
    End Class
    



    Armin

    • Marked as answer by facelool Tuesday, June 26, 2012 4:15 PM
    Tuesday, June 26, 2012 4:02 PM

All replies

  • Upsidedown-backward letters are not "another letter", unless you happen to have an upsidedown font.  Otherwise its just a regular character drawn upsidedown and backward.

    You can create a custom control to serve as an "upsidedown label" and then draw the string upsidedown and backward.

    A textbox won't work because you can't draw on it and it only supports a single font.  If you do have an upsidedown-backward font, then you could use a rich textbox and change the font of the "flipped" characters.  Otherwise you will probably want to create a new custom control that paints it's text upsidedown and backward.


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

    Tuesday, June 26, 2012 2:23 PM
  • You can maintain a dictionary for finding the "flipped" char. For example, the "ʇ" has Unicode 647. I think there's no built-in function to do so.

    Armin

    • Proposed as answer by Stanav Tuesday, June 26, 2012 2:55 PM
    Tuesday, June 26, 2012 2:32 PM
  • Well, If I use the Ascii Codes will it work? and if it will work how can I make it work if the user types a word or a long sentence... Sorry guys, Im just a beginner, and still don't know much in Visual Basic, But Im soo damn addicted to it lol. Anyway, I want a simple code as an example please, to make things much clearer for me, if you don't mind, 
    Tuesday, June 26, 2012 2:40 PM
  • Supposed you use a Dictionary(Of Char, Char) (as suggested by Armin) where the key is the normal char and the value is its flipped char, you then can run a loop through the the characters in the original string and create the flipped string like this:

    Dim flippedChars as New Dictionary(Of Char, Char)

    'Manually add entries to the dictionary

    flippedChars.Add("t"c, ChrW(647))

    'add more entry until you have covered all the printable characters in Ascii table

    .......

    Dim normalString As String = "blabla...."

    Dim flippedString As String = String.Empty

    For i as Integer = normalString.Length - 1 To 0

          If flippedChars.ContainsKey(normalString(i)) Then

              flippedString &= flippedChars(normalString(i))

         Else

             flippedString &= normalString(i)

         End If

    Next

    Tuesday, June 26, 2012 3:10 PM
  • Look guys! I(m using this code and its working:

    Public Class Form1
    
    
        Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            If TextBox1.Text = "a" Then
                TextBox2.Text = "ɐ"
            End If
            If TextBox1.Text = "b" Then
                TextBox2.Text = "q"
            End If
    
            If TextBox1.Text = "b" Then
                TextBox2.Text = "q"
            End If
    
            If TextBox1.Text = "c" Then
                TextBox2.Text = "ɔ"
            End If
    
            If TextBox1.Text = "d" Then
                TextBox2.Text = "p"
            End If
    
            If TextBox1.Text = "e" Then
                TextBox2.Text = "ǝ"
            End If
    
            If TextBox1.Text = "f" Then
                TextBox2.Text = "ɟ"
            End If
    
            If TextBox1.Text = "g" Then
                TextBox2.Text = "ƃ"
            End If
    
            If TextBox1.Text = "h" Then
                TextBox2.Text = "ɥ"
            End If
    
            If TextBox1.Text = "i" Then
                TextBox2.Text = "ı"
            End If
    
            If TextBox1.Text = "j" Then
                TextBox2.Text = "ɾ"
            End If
    
            If TextBox1.Text = "k" Then
                TextBox2.Text = "ʞ"
            End If
    
            If TextBox1.Text = "l" Then
                TextBox2.Text = "l"
            End If
    
            If TextBox1.Text = "m" Then
                TextBox2.Text = "ɯ"
            End If
    
            If TextBox1.Text = "n" Then
                TextBox2.Text = "u"
            End If
    
            If TextBox1.Text = "o" Then
                TextBox2.Text = "o"
            End If
    
            If TextBox1.Text = "p" Then
                TextBox2.Text = "d"
            End If
    
            If TextBox1.Text = "q" Then
                TextBox2.Text = "b"
            End If
    
            If TextBox1.Text = "r" Then
                TextBox2.Text = "ɹ"
            End If
    
            If TextBox1.Text = "s" Then
                TextBox2.Text = "s"
            End If
    
            If TextBox1.Text = "t" Then
                TextBox2.Text = "ʇ"
            End If
    
            If TextBox1.Text = "u" Then
                TextBox2.Text = "n"
            End If
    
    
            If TextBox1.Text = "v" Then
                TextBox2.Text = "ʌ"
            End If
    
            If TextBox1.Text = "w" Then
                TextBox2.Text = "ʍ"
            End If
    
            If TextBox1.Text = "x" Then
                TextBox2.Text = "x"
            End If
    
            If TextBox1.Text = "y" Then
                TextBox2.Text = "ʎ"
            End If
    
            If TextBox1.Text = "z" Then
                TextBox2.Text = "z"
            End If
    
        End Sub
    End Class

    but it works just in case I just one Letter! but it don't work when I type 2 letters or more (i mean a word ) !! how can i make it Please??


    • Edited by facelool Tuesday, June 26, 2012 3:18 PM
    Tuesday, June 26, 2012 3:17 PM
  • I'm very curious to know how you can type those reversed characters !

    To anwer your question, follow Stanav's and Armin's advices :

    Tranform your code into a function. Something like this :

        Private Function FlipChar(sign As Char) As Char
    'Add here all your logic
    '.....
    if sign = "u" then return "n"
    if sign = "z" then return "z"
        End Function
    

    Then, in your textBox.ChangedText event, call it for each letter :

        Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
            Dim result As String = String.Empty
            For Each sign As Char In TextBox1.Text
                result &= FlipChar(sign)
            Next
            TextBox2.Text = result
        End Sub
    

    • Marked as answer by facelool Tuesday, June 26, 2012 3:57 PM
    Tuesday, June 26, 2012 3:22 PM
  • about how to write reversed caracters, you can check out My Blog where i made one with Javascript (Well, its not me who made the script lol i just grab it from somewhere :)
    http://facemoi-new.blogspot.com/2012/06/upside-down-writing.html
    N.B: Use Firefox browser to see the template well organized, its a mess when using Chrome!


    and thank you for your help! imma try what u told me and see if it will work ! Thank You Again :)



    • Edited by facelool Tuesday, June 26, 2012 3:31 PM
    Tuesday, June 26, 2012 3:29 PM
  • Good !
    Once this works, you may want to use a Dictionary (of Char,Char). This is a list of characters (the key) associated with another character (the value). It is probably easier to handle than a long list of "If..... Then....".

    In this case, your code is :

     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            LoadSignsDict()
        End Sub
        Private SignsDict As Dictionary(Of Char, Char)
        Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
            Dim result As String = String.Empty
            For Each sign As Char In TextBox1.Text
                result &= FlipChar(sign)
            Next
            TextBox2.Text = result
        End Sub
        Private Function FlipChar(sign As Char) As Char
            'This function reads the dictionary of signs and returns the flipped sign
    
            If SignsDict.ContainsKey(sign) Then Return SignsDict(sign)
            Return sign
        End Function
        Private Sub LoadSignsDict()
            'This function is called only once and loads the dictionary of signs
            SignsDict = New Dictionary(Of Char, Char)
            SignsDict.Add("u"c, "n"c)
            SignsDict.Add("n"c, "u"c)
            SignsDict.Add("t"c, Convert.ToChar(647))
        End Sub

    Notice that you should load the Dictionary with Char : this is what the letter 'c' means.

    You can also load a converted Unicode value into the Dictionary.

    • Marked as answer by facelool Tuesday, June 26, 2012 4:13 PM
    Tuesday, June 26, 2012 3:53 PM
  • You're the best, man.. Thank you soo Much! its working :D

    Tuesday, June 26, 2012 3:56 PM
  • but it works just in case I just one Letter! but it don't work when I type 2 letters or more (i mean a word ) !! how can i make it Please??

    Public Class Form1
    
       Private Shared ReadOnly FlipDigits As New Dictionary(Of Char, Char)
    
       Shared Sub New()
    
          FlipDigits.Add("a"c, "ɐ"c)
          FlipDigits.Add("b"c, "q"c)
          '...
    
       End Sub
    
       Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    
          Dim sb As New System.Text.StringBuilder
    
          For Each c In TextBox1.Text
             Dim c2 As Char
             If FlipDigits.TryGetValue(c, c2) Then
                c = c2
             End If
             sb.Append(c)
          Next
    
          TextBox2.Text = sb.ToString
    
       End Sub
    End Class
    



    Armin

    • Marked as answer by facelool Tuesday, June 26, 2012 4:15 PM
    Tuesday, June 26, 2012 4:02 PM
  • Thank You guys! all the codes you gave me works very well..
    Thank You All for your Help!
    Tuesday, June 26, 2012 4:15 PM