Results 1 to 12 of 12

Thread: deleting specified text from textbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    462

    Exclamation deleting specified text from textbox

    ok i have 2 textbox's and 1 button i have 1 big textbox that has a buch of number's in it and the number i want to delete is in the top textbox how can i delete number 2 ? from the big textbox ?


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

    Re: deleting specified text from textbox

    You can use IndexOf to find the location of the desired text in the TextBox. You can then set the SelectionStart and SelectionLength properties to select that text. You can then set the SelectedText property to replace the currently selected text. To remove it you would replace it with an empty string or Nothing.
    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
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    462

    Re: deleting specified text from textbox

    k i dident under stand what you just said can you just post the code cause im a noobie

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

    Re: deleting specified text from textbox

    All you have to do is call methods and set properties and you already know how to do that because you've already done it. Get the Text property of the TextBox to get a String. Call the IndexOf method of that String to get the location of the desired substring. Set the SelectionStart property of the TextBox to set the caret position. Set the SelectionLength property of the TextBox to select the desired text. Set the SelectedText property to replace the selected text. It's just a series of steps and each one is very simple. Try to implement them one by one and if you get stuck on one then post back with what you've done so far.

    Alternatively, you can just wait for someone to do it for you but, believe me, you'll learn more by trying things for yourself first.
    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

  5. #5
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: deleting specified text from textbox

    You're never going to stop being a noobie if you expect people to just post code for you.

    If you just want to delete the first instance of the word, then it is pretty straightforward. We are going to search the Text of the big TextBox for the IndexOf the first occurence of the Text in your search TextBox. Then we are going to Remove the correct number of characters from the big TextBox, starting from the IndexOf the first occurence.

    Code:
    Dim strToEdit, strSearch, strEditedText As String
    Dim intFirstIndex, intLength As Integer
    
    strToEdit = bigTextBox.Text
    strSearch = smallTextBox.Text
    
    intFirstIndex = strToEdit.IndexOf(strSearch)
    intLength = strSearch.Length
    
    strEditedText = strToEdit.Remove(intFirstIndex, intLength)
    
    bigTextBox.Text = strEditedText
    This is the long-winded version, which should give you an understanding of what is going on. The short version would be:

    Code:
    bigTextBox.Text = _
        bigTextBox.Text.Remove( _
            bigTextBox.Text.IndexOf( _
                smallTextBox.Text _
            ), _
            smallTextBox.Text.Length _
        )

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    462

    Re: deleting specified text from textbox

    MaximilianMayrhofer & jmcilhinney thank you thank you it works but how do i fix it so it doesn't do this

    ALT
    ARE

    ART
    ATE
    EAR
    EAT

    ERA
    ETA

    HAE
    HAM

    HEM
    HER

    LAM

    LAT
    LEA

    MAE
    MAR
    MAT
    MEL

    RAH
    RAM

    REM
    RET

    TAM
    TAR

    TEL
    THE

    ALME
    EARL
    EATH

    HAET

    how can i remove the

    ?
    i need it like this

    TAM
    TAR
    TEL
    THE
    ALME
    EARL
    EATH
    HAET
    etc thanks
    Last edited by tweaker99; Mar 17th, 2009 at 09:36 AM.

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: deleting specified text from textbox

    think about what you are asking? you can probably fix it by adding

    & Environment.Newline

    to one of the statements you were given.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    462

    Re: deleting specified text from textbox

    like this ?

    Code:
       bigTextBox.Text = _
                bigTextBox.Text.Remove( _
                    bigTextBox.Text.IndexOf( _
                        smallTextBox.Text _
                    ), _
                    smallTextBox.Text.Length & Environment.NewLine)
    casue that doesn't work ?
    Last edited by tweaker99; Mar 17th, 2009 at 10:07 AM.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    462

    Re: deleting specified text from textbox

    anyone know the answer ?

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: deleting specified text from textbox

    max gave you 8 lines. if it doesn't work on one, try another.

    FYI - pm'ing someone with code questions, unless they have stated to do so, is frowned upon.

    one other thing, this and some of the other problems you have posted might have been done better (easier?) with a listbox.
    Last edited by dbasnett; Mar 17th, 2009 at 12:28 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: deleting specified text from textbox

    Tweaker, try:

    Code:
    Dim yourCharacter as string
    bigTextBox.Text = bigTextBox.Text.Replace(yourCharacter & ControlChars.NewLine, "")
    You can set yourCharacter to whatever you want and then the code Replace will find it (along with the line below it) and replace it with "", in otherwords nothing, so it will delete it.

    Also, dbasnett has a point with using listboxes. What exactly are you trying to do with this list of numbers?
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  12. #12
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: deleting specified text from textbox

    That method will work if you want to get rid of every occurence of the search string. The example I gave was to specifically get rid of the first occurence. Now the OP can choose what he's actually looking for

    Other than that, I would suggest using Environment.NewLine rather than ControlChars.NewLine.

    I'm also going to throw my two cents in there and ask why the OP isn't using a ListBox.

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