I want to replace every letter or number in 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", "h")
Case TextBox1.Text.Contains("b")
TextBox1.Text = TextBox1.Text.Replace("b", "o")
Case TextBox1.Text.Contains("c")
TextBox1.Text = TextBox1.Text.Replace("c", "t")
Case TextBox1.Text.Contains("d")
TextBox1.Text = TextBox1.Text.Replace("d", "e")
Case TextBox1.Text.Contains("e")
TextBox1.Text = TextBox1.Text.Replace("e", "i")
Case TextBox1.Text.Contains("f")
TextBox1.Text = TextBox1.Text.Replace("f", "a")
Case TextBox1.Text.Contains("g")
TextBox1.Text = TextBox1.Text.Replace("g", "j")
Case TextBox1.Text.Contains("h")
TextBox1.Text = TextBox1.Text.Replace("h", "f")
Case TextBox1.Text.Contains("i")
TextBox1.Text = TextBox1.Text.Replace("i", "k")
Case TextBox1.Text.Contains("j")
TextBox1.Text = TextBox1.Text.Replace("j", "b")
Case TextBox1.Text.Contains("k")
TextBox1.Text = TextBox1.Text.Replace("k", "n")
Case TextBox1.Text.Contains("l")
TextBox1.Text = TextBox1.Text.Replace("l", "r")
Case TextBox1.Text.Contains("m")
TextBox1.Text = TextBox1.Text.Replace("m", "d")
Case TextBox1.Text.Contains("n")
TextBox1.Text = TextBox1.Text.Replace("n", "s")
Case TextBox1.Text.Contains("o")
TextBox1.Text = TextBox1.Text.Replace("o", "u")
Case TextBox1.Text.Contains("p")
TextBox1.Text = TextBox1.Text.Replace("p", "g")
Case TextBox1.Text.Contains("q")
TextBox1.Text = TextBox1.Text.Replace("q", "w")
Case TextBox1.Text.Contains("r")
TextBox1.Text = TextBox1.Text.Replace("r", "m")
Case TextBox1.Text.Contains("s")
TextBox1.Text = TextBox1.Text.Replace("s", "q")
Case TextBox1.Text.Contains("t")
TextBox1.Text = TextBox1.Text.Replace("t", "x")
Case TextBox1.Text.Contains("u")
TextBox1.Text = TextBox1.Text.Replace("u", "c")
Case TextBox1.Text.Contains("v")
TextBox1.Text = TextBox1.Text.Replace("v", "y")
Case TextBox1.Text.Contains("w")
TextBox1.Text = TextBox1.Text.Replace("w", "z")
Case TextBox1.Text.Contains("x")
TextBox1.Text = TextBox1.Text.Replace("x", "l")
Case TextBox1.Text.Contains("y")
TextBox1.Text = TextBox1.Text.Replace("y", "v")
Case TextBox1.Text.Contains("z")
TextBox1.Text = TextBox1.Text.Replace("z", "p")
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", "h")
End If
If TextBox1.Text.Contains("b") Then
TextBox1.Text = TextBox1.Text.Replace("b", "o")
End If
If TextBox1.Text.Contains("c") Then
TextBox1.Text = TextBox1.Text.Replace("c", "t")
End If
If TextBox1.Text.Contains("d") Then
TextBox1.Text = TextBox1.Text.Replace("d", "e")
End If
If TextBox1.Text.Contains("e") Then
TextBox1.Text = TextBox1.Text.Replace("e", "i")
End If
If TextBox1.Text.Contains("f") Then
TextBox1.Text = TextBox1.Text.Replace("f", "a")
End If
If TextBox1.Text.Contains("g") Then
TextBox1.Text = TextBox1.Text.Replace("g", "j")
End If
If TextBox1.Text.Contains("h") Then
TextBox1.Text = TextBox1.Text.Replace("h", "f")
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", "b")
End If
If TextBox1.Text.Contains("k") Then
TextBox1.Text = TextBox1.Text.Replace("k", "n")
End If
If TextBox1.Text.Contains("l") Then
TextBox1.Text = TextBox1.Text.Replace("l", "r")
End If
If TextBox1.Text.Contains("m") Then
TextBox1.Text = TextBox1.Text.Replace("m", "d")
End If
If TextBox1.Text.Contains("n") Then
TextBox1.Text = TextBox1.Text.Replace("n", "s")
End If
If TextBox1.Text.Contains("o") Then
TextBox1.Text = TextBox1.Text.Replace("o", "u")
End If
If TextBox1.Text.Contains("p") Then
TextBox1.Text = TextBox1.Text.Replace("p", "g")
End If
If TextBox1.Text.Contains("q") Then
TextBox1.Text = TextBox1.Text.Replace("q", "w")
End If
If TextBox1.Text.Contains("r") Then
TextBox1.Text = TextBox1.Text.Replace("r", "m")
End If
If TextBox1.Text.Contains("s") Then
TextBox1.Text = TextBox1.Text.Replace("s", "q")
End If
If TextBox1.Text.Contains("t") Then
TextBox1.Text = TextBox1.Text.Replace("t", "x")
End If
If TextBox1.Text.Contains("u") Then
TextBox1.Text = TextBox1.Text.Replace("u", "c")
End If
If TextBox1.Text.Contains("v") Then
TextBox1.Text = TextBox1.Text.Replace("v", "y")
End If
If TextBox1.Text.Contains("w") Then
TextBox1.Text = TextBox1.Text.Replace("w", "z")
End If
If TextBox1.Text.Contains("x") Then
TextBox1.Text = TextBox1.Text.Replace("x", "l")
End If
If TextBox1.Text.Contains("y") Then
TextBox1.Text = TextBox1.Text.Replace("y", "v")
End If
If TextBox1.Text.Contains("z") Then
TextBox1.Text = TextBox1.Text.Replace("z", "p")
End If
end sub
It doesn't work either, just with a letter at a time.
I want to be able to write, for example, "bike", in a textbox and it replaces the text in the same textbox (or another textbox) to, in this case, "pawm", but I can't see where the problem is.
Replacing the whole text doesn't work, as you have found out, because a letter can be replaced more than once. For example, the "b" is replaced with a "p" but later on you replace the "p" with an "h".
You want to do replacements character by character. Here is an example:
Imports System.Collections.Generic
Imports System.Linq
Dim replacement = New Dictionary(Of Char, Char) From
{
{"b"c, "p"c},
{"i"c, "a"c},
{"k"c, "w"c},
{"e"c, "m"c}
}
Dim word = "bike"
'For each character, we select the replacement letter
Dim letters = word.Select(Function(c) replacement(c)).ToArray()
'Construct a new string with the replaced letters
Dim newWord = New String(letters)
Imagine that the cipher was just a->b, b->c, c->d. Now, if the word is "abc", then we change all the a->b to get "bbc", then all the b->c to get "ccc" and then all the c->d to get "ddd". This is not what we wanted! Instead, we need to make a new string of letters, so have an "a" that we know ends up as "b": the new string is "b" so far. Then we look at the "b" in "abc" and we know it will become "c", so we append it to the new string to get "bc", and finally we look at the "c" in "abc", translate it to "d", append it to the new string and get "bcd". This is what we want, so we have found a method to do the enciphering.
So, we now need a way to look up the translation from the letters the user enters to the enciphered letters.
If we have a string of the letters in the alphabet in order, we can look up the position of a letter in that string by using the String.IndexOf function. So, if we ask it for the index of, say, "c" in "abcdefghijklmnopqrstuvwxyz" it will give us the number 2. It is 2 because it starts counting at zero instead of one. Now, if we have a string with the enciphered characters, we can get it to look up the character in the position we specify, so we look at the character with index 2 in the string "ypsvmdgjatwnqzkhebxurolifc" and we get "s".
Now, it is better to put little parts of code in their own methods (it means you can concentrate on a smaller piece of code if something needs correcting or changing), in this case it would be a good idea to make a function which you give the string you want enciphered and it returns the enciphered string.
So, putting those parts together, I got:
Function Encipher(s As String) As String
Dim normal = "abcdefghijklmnopqrstuvwxyz"
Dim cipher = "ypsvmdgjatwnqzkhebxurolifc"
' Make the uppercase versions too
normal = normal & normal.ToUpper()
cipher = cipher & cipher.ToUpper()
Dim newString = ""
For Each c In s
Dim arrayIndex = normal.IndexOf(c)
If arrayIndex >= 0 Then
Dim cipherChar = cipher.Chars(arrayIndex)
newString &= cipherChar
Else
newString &= c
End If
Next
Return newString
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox1.Text = Encipher(TextBox1.Text)
End Sub
The If arrayIndex >= 0 Then part checks if the character was actually found, if not then the Else part appends the unaltered character.
this totally works
Dim newString As System.Text.StringBuilder = New System.Text.StringBuilder()
For Each character As Char In RichTextBox1.Text
If character = "a"c Then
newString.Append(Chr(104))
ElseIf character = "b"c Then
newString.Append(Chr(111))
ElseIf character = "c"c Then
newString.Append(Chr(116))
ElseIf character = "d"c Then
newString.Append(Chr(101))
ElseIf character = "e"c Then
newString.Append(Chr(105))
ElseIf character = "f"c Then
newString.Append(Chr(97))
ElseIf character = "g"c Then
newString.Append(Chr(108))
ElseIf character = "h"c Then
newString.Append(Chr(102))
ElseIf character = "i"c Then
newString.Append(Chr(107))
ElseIf character = "j"c Then
newString.Append(Chr(98))
ElseIf character = "k"c Then
newString.Append(Chr(110))
ElseIf character = "l"c Then
newString.Append(Chr(114))
ElseIf character = "m"c Then
newString.Append(Chr(100))
ElseIf character = "n"c Then
newString.Append(Chr(115))
ElseIf character = "o"c Then
newString.Append(Chr(117))
ElseIf character = "p"c Then
newString.Append(Chr(103))
ElseIf character = "q"c Then
newString.Append(Chr(119))
ElseIf character = "r"c Then
newString.Append(Chr(109))
ElseIf character = "s"c Then
newString.Append(Chr(113))
ElseIf character = "t"c Then
newString.Append(Chr(120))
ElseIf character = "u"c Then
newString.Append(Chr(99))
ElseIf character = "v"c Then
newString.Append(Chr(121))
ElseIf character = "w"c Then
newString.Append(Chr(122))
ElseIf character = "x"c Then
newString.Append(Chr(108))
ElseIf character = "y"c Then
newString.Append(Chr(118))
ElseIf character = "z"c Then
newString.Append(Chr(112))
ElseIf character = " "c Then
newString.Append(Chr(32))
Else
newString.Append(Chr(Asc(character) + 2))
End If
Next
RichTextBox1.Text = newString.ToString()
Related
I'm new to VB, been learning for about three months. I would like to take the input of a textbox, ex. “1234” and turn it into “one, two, three, four” in the second textbox (with commas). No buttons, just two textboxes. This is the code that I've been trying, it doesn't work. I get the error message:
Object reference not set to an instance of an object
If I could get a simple explanation that would be great, anything super complex will just blow my mind and make it more difficult for me to learn. You guys/gals are awesome and I'm ready to learn, thanks.
Dim boxOne = txtNumber.Text.Split(" "c)
Private Sub TxtNumber_TextChanged(sender As Object, e As EventArgs) Handles txtNumber.TextChanged
For Each i As Integer In boxOne
If txtNumber.Text = "1" Then
lblMessage.Text = "one"
End If
If txtNumber.Text = "2" Then
lblMessage.Text = "two"
End If
If txtNumber.Text = "3" Then
lblMessage.Text = "three"
End If
If txtNumber.Text = "4" Then
lblMessage.Text = "four"
End If
If txtNumber.Text = "5" Then
lblMessage.Text = "five"
End If
If txtNumber.Text = "6" Then
lblMessage.Text = "six"
End If
If txtNumber.Text = "7" Then
lblMessage.Text = "seven"
End If
If txtNumber.Text = "8" Then
lblMessage.Text = "eight"
End If
If txtNumber.Text = "9" Then
lblMessage.Text = "nine"
End If
If txtNumber.Text = "10" Then
lblMessage.Text = "ten"
End If
Next
lblMessage.Text = boxOne
End Sub
I suggest to create a mapper, a collection of items that can map/convert one value to another.
A Dictionary comes in handy for this task: given a Key, it returns the associated Value:
Private valueConverter As Dictionary(Of String, String) = New Dictionary(Of String, String) From {
{"0", "Zero"}, {"1", "One"}, {"2", "Two"}, {"3", "Three"}, {"4", "Four"},
{"5", "Five"}, {"6", "Six"}, {"7", "Seven"}, {"8", "Eight"}, {"9", "Nine"}}
A String is a collection of Chars: to parse it, you just need to loop the collection.
If you need to work with the collection of chars directly, you can use the String.ToCharArray() method (e.g., Dim charCollection = [TextBox].Text.ToCharArray()).
Check whether the current char represents a number (Char.IsNumber(char)) and map if it does:
Imports System.Text
Dim sb As New StringBuilder()
For Each part As Char In txtNumber.Text
If Not Char.IsNumber(part) Then Continue For
sb.Append(valueConverter(part) & ", ")
Next
lblMessage.Text = sb.ToString().TrimEnd({","c, " "c})
The StringBuilder class is often used when we need to cancatenate string. Since strings are immutable, each time you add a string to an existing one, you actually generate a new string each time, which needs to be garbage-collected.
For this reason, using a StringBuilder, the code performs way better.
Public Class Form1
Private Sub Str_TextChanged(sender As Object, e As EventArgs) Handles Str.TextChanged
Dim value As String = ""
For Each Str As String In Me.Str.Text
If Str = "1" Then
If value.Length = 0 Then
value = "One"
Else
value = value & ",One"
End If
ElseIf Str = "2" Then
If value.Length = 0 Then
value = "Two"
Else
value = value & ",Two"
End If
ElseIf Str = "3" Then
If value.Length = 0 Then
value = "Three"
Else
value = value & ",Three"
End If
ElseIf Str = "4" Then
If value.Length = 0 Then
value = "Four"
Else
value = value & ",Four"
End If
ElseIf Str = "5" Then
If value.Length = 0 Then
value = "Five"
Else
value = value & ",Five"
End If
ElseIf Str = "6" Then
If value.Length = 0 Then
value = "Six"
Else
value = value & ",Six"
End If
ElseIf Str = "7" Then
If value.Length = 0 Then
value = "Seven"
Else
value = value & ",Seven"
End If
ElseIf Str = "8" Then
If value.Length = 0 Then
value = "Eight"
Else
value = value & ",Eight"
End If
ElseIf Str = "9" Then
If value.Length = 0 Then
value = "Nine"
Else
value = value & ",Nine"
End If
End If
Next
'Your needed value is in variable 'value', use it to set it to another TextBox :)
End Sub
End Class
I don't think we use For Each on integer values to get individual ones. Try using For Each on string.
Here's a code:
Private Sub TxtNumber_TextChanged(sender As Object, e As EventArgs) Handles txtNumber.TextChanged
Dim value As String
For Each Str As String In TxtNumber.Text
If Str = "1" Then
If value.Length = 0 Then
value = "One"
Else
value = value & ",One"
End If
ElseIf Str = "2" Then
If value.Length = 0 Then
value = "Two"
Else
value = value & ",Two"
End If
ElseIf Str = "3" Then
If value.Length = 0 Then
value = "Three"
Else
value = value & ",Three"
End If
ElseIf Str = "4" Then
If value.Length = 0 Then
value = "Four"
Else
value = value & ",Four"
End If
ElseIf Str = "5" Then
If value.Length = 0 Then
value = "Five"
Else
value = value & ",Five"
End If
ElseIf Str = "6" Then
If value.Length = 0 Then
value = "Six"
Else
value = value & ",Six"
End If
ElseIf Str = "7" Then
If value.Length = 0 Then
value = "Seven"
Else
value = value & ",Seven"
End If
ElseIf Str = "8" Then
If value.Length = 0 Then
value = "Eight"
Else
value = value & ",Eight"
End If
ElseIf Str = "9" Then
If value.Length = 0 Then
value = "Nine"
Else
value = value & ",Nine"
End If
End If
Next
'Your needed value is in variable 'value', use it to set it to another TextBox :)
End Sub
And.... its done.
When I select a value from the first dropdownlist, it populates the second dropdownlist and textbox to their correct values. However, when I select a different value from the second dropdown, the textbox value still retains the same value from which it was populated with. Could anyone please point out what I am doing wrong?
If ds.Tables(0).Rows(0).Item(3).ToString = "M" Then
DropDownList2.Items.Insert(0, "Member")
DropDownList2.Items.Insert(1, "Non-Member")
DropDownList2.Items.Insert(2, "")
TextBox1.Text = "M"
ElseIf ds.Tables(0).Rows(0).Item(3).ToString = "NM" Then
DropDownList2.Items.Insert(0, "Non-Member")
DropDownList2.Items.Insert(1, "Member")
DropDownList2.Items.Insert(2, "")
TextBox1.Text = "NM"
Else
DropDownList2.Items.Insert(0, "")
DropDownList2.Items.Insert(1, "Member")
DropDownList2.Items.Insert(2, "Non-Member")
TextBox1.Text = ""
End If
If DropDownList2.SelectedValue = "Member" Then
TextBox1.Text = ""
TextBox1.Text = "M"
ElseIf DropDownList2.SelectedValue = "Non-Member" Then
TextBox1.Text = ""
TextBox1.Text = "NM"
Else
TextBox1.Text = ""
End If
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.
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.
What you must do is to:
Loop through each character of the textbox one after the other
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.
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
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
Why my code always search the last record not the one I searched, like this 8850338005909.
Here is code Sample Data in Textfile:
8501045891528,2271,"ADAMS CNDY HALLS
HNYLMN100S/36",57.25,36.00,4822,1,100,101,465
8850338001604,2271,"ADAMS CNDY HALLS
HNYLMN100S/36",57.25,36.00,4822,1,100,101,465
8850338005909,2271,"ADAMS CNDY HALLS
HNYLMN100S/36",57.25,36.00,4822,1,100,101,465
8850338002519,2312,"CLRTS CNDY COOL MINT
40S/60",27.75,60.00,7049,1,100,101,465 8850338001000,2313,"ADAMS GUM
CLRETSTWIN PK14GX2/1",66.50,1.00,4822,1,100,101,470
4804880224138,2315,"BGUIO OIL CKNG
16KG/1",978.00,1.00,6135,1,192,193,639
And my code:
Private Sub Data2()
Dim text As String = IO.File.ReadAllText("C:\ITEMRTV.txt")
Dim index As Integer = text.IndexOf(TextBox1.Text)
Dim Values() As String = Split(text, ",")
If index >= 0 Then
' String is in file, starting at character "index"
Label1.Text = Values(0) ' now contains first column value,
Label2.Text = Values(1) ' contains second column, etc.
Label3.Text = Values(2)
Label4.Text = Values(3)
Label5.Text = Values(4)
Label6.Text = Values(5)
End If
End Sub
Try this:
Dim text As String = IO.File.ReadAllText("C:\ITEMRTV.txt")
Dim Values() As String = text.Split(New String() {",", Environment.NewLine}, StringSplitOptions.None)
Dim index As Integer = Array.IndexOf(Values, TextBox1.Text)
If index >= 0 Then
Label1.Text = Values(index)
Label2.Text = Values(index + 1)
Label3.Text = Values(index + 2)
Label4.Text = Values(index + 3)
Label5.Text = Values(index + 4)
Label6.Text = Values(index + 5)
End If
But you should divide into lines and columns:
Dim text() As String = IO.File.ReadAllLines("C:\ITEMRTV.txt")
Dim s() As String
For Each line As String In text
s = line.Split(","c)
If s(0).Equals(TextBox1.Text, StringComparison.CurrentCultureIgnoreCase) _
AndAlso s.Length > 5 Then
Label1.Text = s(0)
Label2.Text = s(1)
Label3.Text = s(2)
Label4.Text = s(3)
Label5.Text = s(4)
Label6.Text = s(5)
Exit For
End If
Next
You could even create your own structure to hold the data, what would be the better option. For example, declarin a Dictionary(Of Integer, List(Of String)) for each file, because the first field looks like a ID, or declaring your own class to hold each field appropiately.
I'm able to validate the cells if they are empty but I'm not able to check the length of the cell. I want the user to enter 5 digits and if it is less than 5, show up a message box.
I tried cellvalue.length method but its not working.
Private Sub dgvResults_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgvResults.CellValidating
'variables
Dim columnName As String = dgvResults.Columns(e.ColumnIndex).Name
Dim cellVal As String = e.FormattedValue.ToString()
'Datagrid view validation
If ((e.ColumnIndex = 0) And (e.FormattedValue = "") And (Not (cellVal.Length = 5))) Then
e.Cancel = True
MessageBox.Show(columnName & " must be 5 Digits Long!")
ElseIf (e.ColumnIndex = 1 And e.FormattedValue = "") Then
e.Cancel = True
MessageBox.Show(columnName & " cannot be blank!")
ElseIf (e.ColumnIndex = 2 And e.FormattedValue = "") Then
e.Cancel = True
MessageBox.Show(columnName & " cannot be blank!")
ElseIf ((e.ColumnIndex = 3) And (e.FormattedValue = "") And (Not IsNumeric(e.FormattedValue))) Then
e.Cancel = True
MessageBox.Show(columnName & " cannot be blank!")
ElseIf ((e.ColumnIndex = 4) And (Not IsNumeric(e.FormattedValue))) Then
e.Cancel = True
MessageBox.Show(columnName & " cannot be blank!")
End If
End Sub
Following causes your code to not work:
(e.FormattedValue = "") And (Not (cellVal.Length = 5))
You are checking if the value is empty and also it's length is <> 5.
But you want to ensure that cellVal.Length is = 5:
If e.ColumnIndex = 0 AndAlso cellVal.Length <> 5 Then
e.Cancel = True
MessageBox.Show(columnName & " must be 5 Digits Long!")
End If
I think you will need:
cellval.text.length