Reputation: 1
I want to replace a character into a string for example i have a string
ABCD^>
and I want to change the ^ to
{enter}
and > to
{tab}
I recently have this code but I can not find a way out.
Dim temp As String
temp = TextBox1.Text
For Each chr As String In temp
If chr = "^" Then
chr = Replace(chr, "^", "{enter}")
End If
TextBox1.Text += chr
MessageBox.Show(TextBox1.Text)
Next
I also use this for keypress event in textbox but still doesn't work
texts = Replace(TextBox2.Text, "^", "{enter}")
texts = Replace(TextBox2.Text, ">", "{tab}")
Upvotes: 0
Views: 119
Reputation: 403
Maybe something like this will work for you.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Const EnterTag As String = "{enter}"
Const TabTag As String = "{tab}"
If Me.TextBox1.Text.IndexOf("^") > -1 Then
curLoc = Me.TextBox1.SelectionStart
Me.TextBox1.Text = TextBox1.Text.Replace("^", EnterTag)
Me.TextBox1.SelectionStart = curLoc + EnterTag.Length
End If
If Me.TextBox1.Text.IndexOf(">") > -1 Then
curLoc = Me.TextBox1.SelectionStart
Me.TextBox1.Text = TextBox1.Text.Replace(">", TabTag)
Me.TextBox1.SelectionStart = curLoc + TabTag.Length
End If
End Sub
Upvotes: 1
Reputation: 8004
There are a few things that can be removed from your code, non necessary variables and your use of your loop.
Here is what I would do...no need to loop.
MessageBox.Show(TextBox1.Text.Replace("^", "{enter}").Replace(">", "{tab}"))
I also use this for keypress event in textbox but still doesn't work
According to your code, I don't see where you were doing this at in the keypress event. Anyway's you still can capture the key that was pressed and perform your logic as needed, but I do not see an attempt that you tried this.
Upvotes: 1
Reputation: 2750
Dim temp as String = TextBox1.Text
temp = temp.Replace("^", "{enter}").Replace(">", "{tab}")
TextBox1.Text = temp
Upvotes: 2