Reputation: 358
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.
Upvotes: 2
Views: 9746
Reputation: 1
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
Upvotes: 0
Reputation: 754
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
Upvotes: 1
Reputation: 28530
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.
Upvotes: 3
Reputation: 2564
What you must do is to:
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
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.
Upvotes: 3