2

I want to replace every letter or number on a textbox using vb.net this was my first try, but it only replaces one letter at a time

 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Select Case True
        Case TextBox1.Text.Contains("a")
            TextBox1.Text = TextBox1.Text.Replace("a", "c")
        Case TextBox1.Text.Contains("b")
            TextBox1.Text = TextBox1.Text.Replace("b", "d")
        Case TextBox1.Text.Contains("c")
            TextBox1.Text = TextBox1.Text.Replace("c", "e")
        Case TextBox1.Text.Contains("d")
            TextBox1.Text = TextBox1.Text.Replace("d", "f")
        Case TextBox1.Text.Contains("e")
            TextBox1.Text = TextBox1.Text.Replace("e", "g")
        Case TextBox1.Text.Contains("f")
            TextBox1.Text = TextBox1.Text.Replace("f", "h")
        Case TextBox1.Text.Contains("g")
            TextBox1.Text = TextBox1.Text.Replace("g", "i")
        Case TextBox1.Text.Contains("h")
            TextBox1.Text = TextBox1.Text.Replace("h", "j")
        Case TextBox1.Text.Contains("i")
            TextBox1.Text = TextBox1.Text.Replace("i", "k")
        Case TextBox1.Text.Contains("j")
            TextBox1.Text = TextBox1.Text.Replace("j", "l")
        Case TextBox1.Text.Contains("k")
            TextBox1.Text = TextBox1.Text.Replace("k", "m")
        Case TextBox1.Text.Contains("l")
            TextBox1.Text = TextBox1.Text.Replace("l", "n")
        Case TextBox1.Text.Contains("m")
            TextBox1.Text = TextBox1.Text.Replace("m", "o")
        Case TextBox1.Text.Contains("n")
            TextBox1.Text = TextBox1.Text.Replace("n", "p")
        Case TextBox1.Text.Contains("o")
            TextBox1.Text = TextBox1.Text.Replace("o", "q")
        Case TextBox1.Text.Contains("p")
            TextBox1.Text = TextBox1.Text.Replace("p", "r")
        Case TextBox1.Text.Contains("q")
            TextBox1.Text = TextBox1.Text.Replace("q", "s")
        Case TextBox1.Text.Contains("r")
            TextBox1.Text = TextBox1.Text.Replace("r", "t")
        Case TextBox1.Text.Contains("s")
            TextBox1.Text = TextBox1.Text.Replace("s", "u")
        Case TextBox1.Text.Contains("t")
            TextBox1.Text = TextBox1.Text.Replace("t", "v")
        Case TextBox1.Text.Contains("u")
            TextBox1.Text = TextBox1.Text.Replace("u", "w")
        Case TextBox1.Text.Contains("v")
            TextBox1.Text = TextBox1.Text.Replace("v", "x")
        Case TextBox1.Text.Contains("w")
            TextBox1.Text = TextBox1.Text.Replace("w", "y")
        Case TextBox1.Text.Contains("x")
            TextBox1.Text = TextBox1.Text.Replace("x", "z")
        Case TextBox1.Text.Contains("y")
            TextBox1.Text = TextBox1.Text.Replace("y", "a")
        Case TextBox1.Text.Contains("z")
            TextBox1.Text = TextBox1.Text.Replace("z", "b")

    End Select
End Sub

This isn't what I want, so I tried this

   Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

    If TextBox1.Text.Contains("a") Then
        TextBox1.Text = TextBox1.Text.Replace("a", "c")
    End If

    If TextBox1.Text.Contains("b") Then
        TextBox1.Text = TextBox1.Text.Replace("b", "d")
    End If

    If TextBox1.Text.Contains("c") Then
        TextBox1.Text = TextBox1.Text.Replace("c", "e")
    End If

    If TextBox1.Text.Contains("d") Then
        TextBox1.Text = TextBox1.Text.Replace("d", "f")
    End If

    If TextBox1.Text.Contains("e") Then
        TextBox1.Text = TextBox1.Text.Replace("e", "g")
    End If

    If TextBox1.Text.Contains("f") Then
        TextBox1.Text = TextBox1.Text.Replace("f", "h")
    End If

    If TextBox1.Text.Contains("g") Then
        TextBox1.Text = TextBox1.Text.Replace("g", "i")
    End If

    If TextBox1.Text.Contains("h") Then
        TextBox1.Text = TextBox1.Text.Replace("h", "j")
    End If

    If TextBox1.Text.Contains("i") Then
        TextBox1.Text = TextBox1.Text.Replace("i", "k")
    End If

    If TextBox1.Text.Contains("j") Then
        TextBox1.Text = TextBox1.Text.Replace("j", "l")
    End If

    If TextBox1.Text.Contains("k") Then
        TextBox1.Text = TextBox1.Text.Replace("k", "m")
    End If

    If TextBox1.Text.Contains("l") Then
        TextBox1.Text = TextBox1.Text.Replace("l", "n")
    End If

    If TextBox1.Text.Contains("m") Then
        TextBox1.Text = TextBox1.Text.Replace("m", "o")

    End If

    If TextBox1.Text.Contains("n") Then
        TextBox1.Text = TextBox1.Text.Replace("n", "p")
    End If

    If TextBox1.Text.Contains("o") Then
        TextBox1.Text = TextBox1.Text.Replace("o", "q")
    End If

    If TextBox1.Text.Contains("p") Then
        TextBox1.Text = TextBox1.Text.Replace("p", "r")
    End If

    If TextBox1.Text.Contains("q") Then
        TextBox1.Text = TextBox1.Text.Replace("q", "s")
    End If

    If TextBox1.Text.Contains("r") Then
        TextBox1.Text = TextBox1.Text.Replace("r", "t")
    End If

    If TextBox1.Text.Contains("s") Then
        TextBox1.Text = TextBox1.Text.Replace("s", "u")
    End If

    If TextBox1.Text.Contains("t") Then
        TextBox1.Text = TextBox1.Text.Replace("t", "v")
    End If

    If TextBox1.Text.Contains("u") Then
        TextBox1.Text = TextBox1.Text.Replace("u", "w")
    End If

    If TextBox1.Text.Contains("v") Then
        TextBox1.Text = TextBox1.Text.Replace("v", "x")
    End If

    If TextBox1.Text.Contains("w") Then
        TextBox1.Text = TextBox1.Text.Replace("w", "y")
    End If

    If TextBox1.Text.Contains("x") Then
        TextBox1.Text = TextBox1.Text.Replace("x", "z")
    End If

    If TextBox1.Text.Contains("y") Then
        TextBox1.Text = TextBox1.Text.Replace("y", "a")
    End If

    If TextBox1.Text.Contains("z") Then
        TextBox1.Text = TextBox1.Text.Replace("z", "b")
    End If


End Sub

It doesn't work either, just with a letter at a time.

I want to be able to write in a textbox for example "bike" and it replaces the text in the same textbox (or other textbox) in this case: "dkmg" but I can't see where the problem is.

6
  • First, the "if" is not needed. Try to remove them and just keep the lines with "Replace". Also, keep in mind that these "ifs" are case-sensitive, so "A" is not equal to "a" - they are seen like 2 different characters. Commented Apr 19, 2015 at 22:00
  • Once you replace a with c, it will then be replaced with e, If you really want to replace every letter with the one two after in the alphabet, then you can't use this approach. What you need to do is loop through the string in the textbox and create a new string based on processing each letter - which you can do as each character of the string is a char, which can be cast to an int - then write the result back to the textbox. Commented Apr 19, 2015 at 22:01
  • @Joe i remove all the if's, letf only the replace lines, but he didn't work :s Commented Apr 19, 2015 at 22:10
  • @stuartd im understanding what you are saying, but if you could help just how to start what you said please, or some page with some example please Commented Apr 19, 2015 at 22:11
  • stuartd made me realize that removing the ifs wouldn't work anyway since you would edit the same letter several times (unless you do it backward, starting from z). Commented Apr 19, 2015 at 22:13

4 Answers 4

3

To expand upon Joe's answer and pseudo code, a simple For Each loop through the string will allow you to process one character at a time. The easiest way to do this is to use the ASCII value of the character to determine what to replace it with, but as Joe noted you have to handle the edge cases, since the ASCII table contains many symbols, not just letter, and they're in a specific order.

In your posted code, you appear to be replacing each letter with the corresponding letter 2 spaces from the current letter's position (i.e., a = c, b = d, etc.).

The ASCII table uses 65-90 for A to Z, and 97-122 for a to z. Hence the edge cases for Y, Z, y and z - if you add 2 to Z, for example, you will get |, rather than B. This is where If statements can help (there are other ways to do it as well, like Select).

Sample code to illustrate this:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Dim newString As StringBuilder = New StringBuilder()

    For Each character As Char In TextBox1.Text
        If character = "Y"c Then
            newString.Append(Chr(65))
        Else If character = "Z"c Then
            newString.Append(Chr(66))
        Else If character = "y"c Then
            newString.Append(Chr(97))
        Else If character = "z"c Then
            newString.Append(Chr(98))
        Else
            newString.Append(Chr(Asc(character) + 2))
        End If
    Next

    TextBox1.Text = newString.ToString()
End Sub

Pretty straightforward. I used StringBuilder as it can be a little more efficient, depending on the length of the string. The alternative:

Dim newString As String

newString = newString + Chr(character) 

Would result in a new copy of newString being made for each iteration through the loop - so if you had a 10 character string, you'd wind up with 10 copies of newString - one for each loop.

The Asc function gets the ASCII value of the character, and the Chr function gets the character specified by the ASCII value. The lowercase c next to the character (i.e., "A"c) simply indicates it's a Char, not a String.

Note that the above code will handle any ASCII value, so if you want to handle only characters, you'll need to do more checking in the loop. I'll leave that as an exercise for the readers.

Sign up to request clarification or add additional context in comments.

2 Comments

It worked, thank you very much! I just changed the: "Dim newString As StringBuilder = New StringBuilder()" TO "Dim newString As System.Text.StringBuilder = New System.Text.StringBuilder()"
You're welcome. You can use an Imports directive at the start of your code for System.Text and not have to fully qualify it later.
3

What you must do is to:

  1. Loop through each character of the textbox one after the other
  2. Each time, you take one character, "process it" (replace it in your case), then you write it in a separate string, a bit like this (pseudo-code):

    new_string = ""
    for char in textbox.text:
        # do something with char, your 'if' routines would probably work
        new_string = new_string + char
    
  3. Then you assign the new string to the textbox:

    textbox.text = new_string
    

If you always want to add 2 letters to each letter, there's a way to treat each character as an integer. There's an example here (look at the bottom). Once you have this, you can simply add "2" to your char before printing it back to the string (some conversion might be needed, i'll let you figure that out):

    for char in textbox.text:
        new_string = new_string + (char + 2)

This mostly works, but you'll have to treat edge cases (y, z, Y, Z) yourself. You can use this chart for reference.

4 Comments

I'm gonna try that, then I tell you if it works. Thanks in advance!
I came to this code Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click Dim new_string As String = "" For Each chr As Char In TextBox1.Text new_string = new_string + chr Next TextBox2.Text = new_string End Sub but I don't know how to do the if's statements now.. can you help me?
Sir, I wrote pseudo-code, not VB (sorry my VB is really old) - you must find how to translate this to VB code (or maybe a fellow stackoverflow member can copy/edit my answer)
Yes, i could translate it, but now I'm stuck on the if's (don't understand how to do it) can you help me?
1

Here you go:

    TextBox1.Text = (From c In TextBox1.Text.ToLower
                     Where c >= "a"c AndAlso c <= "z"c
                     Select Chr(97 + (Asc(c) - 95) Mod 26)).ToArray

Comments

0

To change first letter to upper and the rest to lower after loss of focus:

 Private Sub CapsLock(TextCaps)
    'تغير اول حرف الي حرف كير
    Dim fNameS As String = ""
    Dim liS As String = ""
    Dim lis2 As String = ""

    For i = 1 To Len(TextCaps)
        liS = Mid(TextCaps, i, 1)
        If i > 1 Then
            lis2 = Mid(TextCaps, (i - 1), 1)
        End If

        If i = 1 Or lis2 = " " Then

            liS = liS.ToUpper
            fNameS = fNameS & liS

        Else

            liS = liS.ToLower

            fNameS = fNameS & liS


        End If
    Next
    TextCaps2 = fNameS

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.