How to change letters in a text box (and get it to actually work!)?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im trying to make a text ecrypter/decrypter were I can choose how it gets encrypted. So im using the Replace method to replace a letter to a different letter. But what happens is, only some of the letters convert correctly! What some a doing is converting
themselves twice. So if A gets replaced by B and B gets replaced by A, if i entered A, it would convert to B, then back to A. So...
If TextBox1.Text.Contains("a") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("a", "b")<br/>
End If
If TextBox1.Text.Contains("b") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("b", "a")<br/>
End If

Then in the form...

Textbox1 = a

I press ENCRYPT

Textbox1 = a

because it converts itself twice. I hope you get what i mean. Here is my code and you can download my project here :

http://dl.dropbox.com/u/22717419/Text%20Encrypter%20test.zip


Code 1:

Public Class Form1<br/>
<br/>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<br/>
Dim before = TextBox1.Text<br/>
<br/>
If TextBox1.Text.Contains("a") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("a", "q")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("b") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("b", "w")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("c") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("c", "e")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("d") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("d", "r")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("e") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("e", "t")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("f") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("f", "y")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("g") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("g", "u")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("h") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("h", "i")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("i") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("i", "o")<br/>
End If<br/>
<br/>
If TextBox1.Text.Contains("j") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("j", "p")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("k") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("k", "a")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("l") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("l", "s")<br/>
End If<br/>
<br/>
If TextBox1.Text.Contains("m") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("m", "d")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("n") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("n", "f")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("o") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("o", "g")<br/>
End If<br/>
<br/>
If TextBox1.Text.Contains("p") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("p", "h")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("q") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("q", "j")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("r") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("r", "k")<br/>
End If<br/>
<br/>
If TextBox1.Text.Contains("s") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("s", "l")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("t") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("t", "z")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("u") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("u", "x")<br/>
End If<br/>
<br/>
If TextBox1.Text.Contains("v") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("v", "c")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("w") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("w", "v")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("w") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("w", "b")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("y") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("y", "n")<br/>
End If<br/>
<br/>
<br/>
If TextBox1.Text.Contains("z") Then<br/>
TextBox1.Text = TextBox1.Text.Replace("z", "m")<br/>
End If<br/>
<br/>
TextBox2.Text = TextBox1.Text<br/>
TextBox1.Text = before<br/>
<br/>
End Sub<br/>
End Class




Code 2:

Public Class Form1<br/>
<br/>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<br/>
TextBox1.Text = TextBox1.Text.Replace("a", "q")<br/>
TextBox1.Text = TextBox1.Text.Replace("b", "w")<br/>
TextBox1.Text = TextBox1.Text.Replace("c", "e")<br/>
TextBox1.Text = TextBox1.Text.Replace("d", "r")<br/>
TextBox1.Text = TextBox1.Text.Replace("e", "t")<br/>
TextBox1.Text = TextBox1.Text.Replace("f", "y")<br/>
TextBox1.Text = TextBox1.Text.Replace("g", "u")<br/>
TextBox1.Text = TextBox1.Text.Replace("h", "i")<br/>
TextBox1.Text = TextBox1.Text.Replace("i", "o")<br/>
TextBox1.Text = TextBox1.Text.Replace("j", "p")<br/>
TextBox1.Text = TextBox1.Text.Replace("k", "a")<br/>
TextBox1.Text = TextBox1.Text.Replace("l", "s")<br/>
TextBox1.Text = TextBox1.Text.Replace("m", "d")<br/>
TextBox1.Text = TextBox1.Text.Replace("n", "f")<br/>
TextBox1.Text = TextBox1.Text.Replace("o", "g")<br/>
TextBox1.Text = TextBox1.Text.Replace("p", "h")<br/>
TextBox1.Text = TextBox1.Text.Replace("q", "j")<br/>
TextBox1.Text = TextBox1.Text.Replace("r", "k")<br/>
TextBox1.Text = TextBox1.Text.Replace("s", "l")<br/>
TextBox1.Text = TextBox1.Text.Replace("t", "z")<br/>
TextBox1.Text = TextBox1.Text.Replace("u", "x")<br/>
TextBox1.Text = TextBox1.Text.Replace("v", "c")<br/>
TextBox1.Text = TextBox1.Text.Replace("w", "v")<br/>
TextBox1.Text = TextBox1.Text.Replace("x", "b")<br/>
TextBox1.Text = TextBox1.Text.Replace("y", "n")<br/>
TextBox1.Text = TextBox1.Text.Replace("z", "m")
<br/>
End Sub<br/>
End Class



Code 3:
Public Class Form1<br/>
<br/>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim temp As New System.IO.StreamWriter("C:temptempencryption.txt")<br/>
Dim a, b, c, d, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z As String<br/>
Dim Encypted As String<br/>
TextBox1.Text = " " & TextBox1.Text<br/>
a = "a"<br/>
b = "b"<br/>
c = "c"<br/>
d = "d"<br/>
f = "f"<br/>
g = "g"<br/>
h = "h"<br/>
i = "i"<br/>
j = "j"<br/>
k = "k"<br/>
l = "l"<br/>
m = "m"<br/>
n = "n"<br/>
o = "o"<br/>
p = "p"<br/>
q = "q"<br/>
r = "r"<br/>
s = "s"<br/>
t = "t"<br/>
u = "u"<br/>
v = "v"<br/>
w = "w"<br/>
x = "x"<br/>
y = "y"<br/>
z = "z"<br/>
<br/>
Encypted = TextBox1.Text<br/>
<br/>
Encypted = Replace(Encypted, a, "q")<br/>
Encypted = Replace(Encypted, b, "w")<br/>
Encypted = Replace(Encypted, c, "m")<br/>
Encypted = Replace(Encypted, d, "r")<br/>
Encypted = Replace(Encypted, f, "t")<br/>
Encypted = Replace(Encypted, g, "y")<br/>
Encypted = Replace(Encypted, h, "u")<br/>
Encypted = Replace(Encypted, i, "i")<br/>
Encypted = Replace(Encypted, j, "o")<br/>
Encypted = Replace(Encypted, k, "p")<br/>
Encypted = Replace(Encypted, l, "a")<br/>
Encypted = Replace(Encypted, m, "s")<br/>
Encypted = Replace(Encypted, n, "d")<br/>
Encypted = Replace(Encypted, o, "f")<br/>
Encypted = Replace(Encypted, p, "g")<br/>
Encypted = Replace(Encypted, q, "h")<br/>
Encypted = Replace(Encypted, r, "j")<br/>
Encypted = Replace(Encypted, s, "k")<br/>
Encypted = Replace(Encypted, t, "l")<br/>
Encypted = Replace(Encypted, u, "z")<br/>
Encypted = Replace(Encypted, v, "x")<br/>
Encypted = Replace(Encypted, w, "c")<br/>
Encypted = Replace(Encypted, x, "v")<br/>
Encypted = Replace(Encypted, y, "b")<br/>
Encypted = Replace(Encypted, z, "n")<br/>
<br/>
<br/>
<br/>
<br/>
temp.WriteLine(Encypted)<br/>
temp.Close()<br/>
<br/>
Dim read As New System.IO.StreamReader("C:temptempencryption.txt")<br/>
<br/>
read.Read()<br/>
Dim EncryptedText As String<br/>
EncryptedText = read.ReadLine<br/>
read.Close()<br/>
TextBox2.Text = EncryptedText
End Sub<br/>
End Class


View the full article
 
Back
Top