I wan't to make a PictureBox and a Label show if TextBox1 and TextBox2 Texts equal definite word.
But I receive an error...
Please, help
Here is the code:
Public Class Appearance
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "Brown" & TextBox2.Text = "Brown" Then
PictureBox4.Image = My.Resources.brown
PictureBox2.Image = My.Resources.blue
PictureBox5.Image = My.Resources.green
PictureBox4.Visible = True
PictureBox2.Visible = True
PictureBox5.Visible = True
Label7.Visible = True
Label8.Visible = True
Label9.Visible = True
End If
End Sub
End Class
In VB.NET & means string concatenation. You probably want to use AndAlso:
If TextBox1.Text = "Brown" AndAlso TextBox2.Text = "Brown" Then
Edit : Sorry, missed you wanted to compare both TextBox1 and TextBox2 values to one String Value
Right evaluation is already provided by Neolisk :
If TextBox1.Text = "Brown" AndAlso TextBox2.Text = "Brown" Then
EDIT : the following suggestion is wrong..
Replace this
If TextBox1.Text = "Brown" & TextBox2.Text = "Brown" Then
by this :
If TextBox1.Text = TextBox2.Text Then
EDIT : I leave the explanation here
If you write If TextBox1.Text = "Brown" & TextBox2.Text = "Brown" Then, and we suppose that TextBox2.Text is "Brown" what's happening is :
Since the & is for String concatenation in VB, just like +, TextBox1.Text will be compared to the chain "Brown" & TextBox2.Text = "Brown".
With "Brown" & TextBox2.Text = "Brown", "Brown" & TextBox2.Text will be evaluated first, since the & string concatener takes precedence over the = boolean evaluation. You'll get the concatenated string "BrownBrown" (= "Brown").
But then, you'll have "BrownBrown" = "Brown", and at this stage, the full chain of evaluation is If TextBox1.Text = "BrownBrown" = "Brown" Then.
TextBox1.Text = "BrownBrown" will be evaluated first and will return False. In the end, you're evaluating :
If False = "Brown" Then ' <- Boolean comparison with String Error !
Since you're new to VB, good to know :
Note that AndAlso has the shorcut feature. If the evaluation on the left part is False, the right part is not evaluated.
To evaluate both left and right, there is the And comparer. But, honestly, comparing
If False = True And "SameValue" = "SameValue" ' will always return False.
There is NO meaning in comparing the right side if the left is already False. You ought to know that this is a unique glitch of VB. However, I don't know if it was fixed since. But if not, the same applies for Or and OrElse.
=> You'd better use AndAlso and OrElse instead of And/Or from the start.
Related
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()
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have this code
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim Location1 As String
Dim city As Boolean
Dim sss As Boolean
If Not IsNumeric(TextBox1.Text) Then
MsgBox("Invalid ip address")
Else
city = True
sss = False
geoip()
If Label1.Text = "" Or Label1.Text = " " Then
city = False
Else
city = True
Location1 = Label1.Text
End If
If city = False And Label16.Text = "" Or Label16.Text = " " Then
Location1 = Label17.Text
Else
sss = True
End If
If sss = True Then
Location1 = Label16.Text
End If
End If
TextBox1.Text = ""
If CheckBox2.Checked = True Then
TextBox2.Text = "We look forward to meet you tomorrow at the meeting place at 1 PM, the package has been shipped already to you to " + Location1 + ". If you wont be there for completing the transaction, we will need to proceed to stage 2."
End If
If CheckBox1.Checked = True Then
TextBox2.Text = "My name is " + TextBox3.Text + " from " + TextBox4.Text + " and im here for letting you know that we look forward to meet you tomorrow at the meeting place at 1 PM, the package has been shipped already to you to " + Location1 + ". If you wont be there for completing the transaction, we will need to proceed to stage 2."
End If
TextBox1.Text = ""
End Sub
And it doesn't work as intended. It displays even when label1 is not "" or " " as label 16, I searched for the error in the code for so much time, but I think all is good
I now understand your question - I'm leaving my old answer since it is correct to bracket conditions and your code would still have broken, but your problem was that you have your If ... elses such that you fall into the else in places you wouldn't want to.
For example - Consider this line in your code:
If city = False And Label16.Text = "" Or Label16.Text = " " Then
Location1 = Label17.Text
Else
sss = True
End If
That else will be hit if city is true or if there's something in in label 16. You'd only want it to get there if city is still false and there's something in label 16....
Rather focus on doing your logic by nesting your conditional logic. This code should work as expected:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim Location1 As String
Dim city As Boolean
Dim sss As Boolean
If Not IsNumeric(TextBox1.Text) Then
MsgBox("Invalid ip address")
Else
geoip()
If Label1.Text.Trim() = "" Then
city = False
If Label16.Text.Trim() = "" Then
Location1 = Label17.Text
Else
sss = True
Location1 = Label16.Text
End If
Else
city = True
Location1 = Label1.Text
End If
End If
TextBox1.Text = ""
If CheckBox2.Checked = True Then
TextBox2.Text = "We look forward to meet you tomorrow at the meeting place at 1 PM, the package has been shipped already to you to " + Location1 + ". If you wont be there for completing the transaction, we will need to proceed to stage 2."
End If
If CheckBox1.Checked = True Then
TextBox2.Text = "My name is " + TextBox3.Text + " from " + TextBox4.Text + " and im here for letting you know that we look forward to meet you tomorrow at the meeting place at 1 PM, the package has been shipped already to you to " + Location1 + ". If you wont be there for completing the transaction, we will need to proceed to stage 2."
End If
TextBox1.Text = ""
End Sub
I'm not understanding your question as stated, but am pretty sure your issue has to do with bracketing your clauses.
Your line If city = False And Label16.Text = "" Or Label16.Text = " " Then reads right to you, but isn't being understood correctly by the compiler... Always bracket complex logic statements to be safe.
Try and change this:
If city = False And Label16.Text = "" Or Label16.Text = " " Then
to this:
If city = False And (Label16.Text = "" Or Label16.Text = " ") Then
Or, please explain the problem a bit better...
I think you used the IsNumeric function won't work for a ip address. Try to split the ip and then check with IsNumeric.
So, I am making a console in VB.net. I decided to use, in a Lua type fashion, a sub string way. However, when doing this, if I type something that is not a command, then it will crash with the error: "Additional information: Index and length must refer to a location within the string."
Public Class Form1
Private Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown
Dim cmd As String = TextBox2.Text
Dim l As Integer = Len(TextBox2.Text)
Dim param As String
If e.KeyCode = Keys.Enter Then
'0 Param command
If cmd = "script" Then
Script.Show()
TextBox2.Text = ""
'Trying to avoide error "Additional information: Index and length must refer to a location within the string."
ElseIf Not cmd.Substring(0, 5) = "echo;" Or Not cmd.Substring(0, 9) = "download;" Then
Return
'1 Param cmd
ElseIf cmd.Substring(0, 5) = "echo;" Then
param = cmd.Substring(5)
Me.TextBox1.Text = Me.TextBox1.Text + Environment.NewLine + param
TextBox2.Text = ""
'2 Param cmd
ElseIf cmd.Substring(0, 9) = "download;" Then
param = cmd.Substring(9)
Me.TextBox1.Text = Me.TextBox1.Text + Environment.NewLine + param.Substring(0, param.IndexOf(";"c)) + param.Substring(param.IndexOf(";"c) + 1)
TextBox2.Text = ""
End If
End If
End Sub
End Class
You cannot try to get a substring from locations that are not inside the string.
However instead of trying to get a substring you could use a simpler approach that avoid also the first check on the input text
If cmd = "script" Then
.....
ElseIf cmd.StartsWith("echo;") Then
.....
ElseIf cmd.StartsWith("download;") Then
....
Else
Return
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
There are many blanks in table and I would like for those blanks (DBNull) in label values to be ignored and presented as blank values. Also with adding and changing table through web form it would be hard to control every single input (23 columns of data).
Is it possible to do that with FOR statement? I've tried by didn't had any success.
I could do that with IF statement but I would have to do that for every single of 23 labels.
Here is the code I have so far (thanks to people on this site).
Protected Sub TextBox1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Load
Using sqlconn = New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=KLIJENTI;Integrated Security=True")
Using sqlcmd = New SqlCommand("Select NAZIV,PUN_NAZIV, ADRESA, GRAD, OPSTINA, PRAVNA_FORMA,DAT_REG, TRAJANJE, MATICNI, PIB, SIFRA_DELATNOSTI, NAZIV_DELATNOSTI, VELICINA, TEKUCI, RZZO, PIO From Baza Where SIFRE = #SIFRE", sqlconn)
sqlcmd.Parameters.AddWithValue("#SIFRE", TextBox1.Text)
sqlconn.Open()
Dim result = sqlcmd.ExecuteReader()
While (result.Read())
If result IsNot Nothing Then
Label1.Text = result("NAZIV")
Label2.Text = result("PUN_NAZIV")
Label3.Text = result("ADRESA")
Label4.Text = result("GRAD")
Label5.Text = result("OPSTINA")
Label6.Text = result("PRAVNA_FORMA")
Label7.Text = result("DAT_REG")
Label8.Text = result("TRAJANJE")
Label9.Text = result("MATICNI")
Label10.Text = result("PIB")
Label11.Text = result("SIFRA_DELATNOSTI")
Label12.Text = result("NAZIV_DELATNOSTI")
Label13.Text = result("VELICINA")
Label14.Text = result("TEKUCI")
Label15.Text = result("RZZO") ' PROBLEM DBNull.Value
Label16.Text = result("PIO") ' PROBLEM DBNull.Value There are more labels below but I am stuck here
Else
TextBox1.Focus()
End If
End While
End Using
End Using
TextBox1.Text = ""
End Sub
Also there are blanks all over just in this instance they are in 15th and 16th label.
Thanks.
Simply add this function to your class:
Private Shared Function GetTextOrEmpty(reader As IDataReader, fieldName As String)
Dim ordinal = reader.GetOrdinal(fieldName)
Return If(reader.IsDbNull(ordinal), String.Empty, reader.GetString(ordinal))
End Function
And then, in your For loop:
'Other labels (...)
Label14.Text = GetTextOrEmpty(result, "TEKUCI")
Label15.Text = GetTextOrEmpty(result, "RZZO")
'Other labels (...)
The SqlDataReader has its own method to deal with DbNull Values
Label15.Text = If(result.IsDbNull(result.GetOrdinal("RZZO")), "", result("RZZO"))
You could use the ternary operator:
Label15.Text = If(result("RZZO") Is Convert.DBNull, "", result("RZZO"))