[RESOLVED] Removing string from textbox?
I am trying to remove a certain string from a textbox when a button is pressed. Although using replace isn't working out too well for me. Can someone give me a hand?
This is my code already.
vb Code:
Dim rdystring As String = DisplayTextBox.Text
If InStr(rdystring, "Ready to begin.") Then
Replace(rdystring, "Ready to begin.", "")
DisplayTextBox.Clear()
DisplayTextBox.Text = rdystring
End If
I tried this before the one above:
vb Code:
If InStr(Displaytextbox.text, "Ready to begin.") Then
Replace(Displaytextbox.text, "Ready to begin.", "")
End If
Re: Removing string from textbox?
You seem to be assuming that Replace is going to change your String. It's not. Strings are immutable. Generally speaking, any method that conceptually changes a String actually creates a new String containing the changes. Replace is one such method. You are doing anything with the new String it's returning so you never see the change.
Also, don't use that Replace method. Call the Replace method of the original String instead. Also, don't use InStr. Call the Contains method of the original String or, if you need that actual position, the IndexOf method.
Re: Removing string from textbox?
Thanks alot. Got it working now. I have many other spots in my project where I used Instr, I think I should probably change those to contains too.
Here is the working code for anyone elses reference.
vb Code:
If DisplayTextBox.Text.Contains("Ready to begin.") Then
DisplayTextBox.Text = DisplayTextBox.Text.Replace("Ready to begin.", "").Trim()
End If
EDIT: Added trim to the end of the new string, to remove the empty line it leaves.
Re: Removing string from textbox?