Results 1 to 4 of 4

Thread: [RESOLVED] Removing string from textbox?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    200

    Resolved [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:
    1. Dim rdystring As String = DisplayTextBox.Text
    2.         If InStr(rdystring, "Ready to begin.") Then
    3.             Replace(rdystring, "Ready to begin.", "")
    4.             DisplayTextBox.Clear()
    5.             DisplayTextBox.Text = rdystring
    6.         End If

    I tried this before the one above:

    vb Code:
    1. If InStr(Displaytextbox.text, "Ready to begin.") Then
    2.             Replace(Displaytextbox.text, "Ready to begin.", "")
    3.                End If

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    200

    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:
    1. If DisplayTextBox.Text.Contains("Ready to begin.") Then
    2.             DisplayTextBox.Text = DisplayTextBox.Text.Replace("Ready to begin.", "").Trim()
    3.            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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Removing string from textbox?

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width