|
-
Aug 30th, 2012, 10:43 PM
#1
Thread Starter
Addicted Member
[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
-
Aug 30th, 2012, 10:54 PM
#2
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.
-
Aug 30th, 2012, 11:13 PM
#3
Thread Starter
Addicted Member
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.
Last edited by JoePage; Aug 30th, 2012 at 11:26 PM.
-
Aug 30th, 2012, 11:55 PM
#4
Re: Removing string from textbox?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|