Results 1 to 12 of 12

Thread: adding text to multiline textbox without erasing previous contents

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2011
    Posts
    132

    adding text to multiline textbox without erasing previous contents

    how do i add text to a multiline textbox without erasing what is already in there?

    Lets say the textbox already has...

    "Visual basic is "

    and then i want to add the word "fun" to the textbox without erasing "Visual basic is "

    after adding "fun" i want it to read....
    "Visual basic is fun"

    any ideas?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: adding text to multiline textbox without erasing previous contents

    well... think of your text box as a variable... if you have "Visual Basic 6 is " in a variable and you want to concatenate text to it, how would you go about doing that?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Lively Member
    Join Date
    Apr 2011
    Posts
    88

    Re: adding text to multiline textbox without erasing previous contents

    I had a similar problem while trying to add text in my Text Adventure game...
    What I did was, whenever I want the game to add something new to the textbox without reasing the old thing I'd put:

    textbox1.text = textbox1.text + " fun"

    Hope this is what you were looking for.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: adding text to multiline textbox without erasing previous contents

    vb Code:
    1. textbox1.text &= "new text"

    to put "new text" on a new line:

    vb Code:
    1. textbox1.text &= environment.newline & "new text"

  5. #5
    Fanatic Member EntityX's Avatar
    Join Date
    Feb 2007
    Location
    Omnipresence
    Posts
    798

    Re: adding text to multiline textbox without erasing previous contents

    I wasn't familiar with &= I would have coded it like

    Code:
    textbox1.Text = textbox1.Text & "new text"
    I see that & and + are interchangeable. So you could code it

    Code:
    textbox1.text &= "new text" ' Or
    textbox1.text += "new text" ' And
    textbox1.text = textbox1.Text & "new text" ' is the same thing as
    textbox1.text = textbox1.Text + "new text"
    At least they're the same as far as I can see but maybe there's some behavioral difference under certain circumstances. I noticed that you can also put + and & in the same equation without a problem which makes sense since they do the same thing.

    Code:
    textbox1.text += "new text" & " some other text" + " and still more text"
    Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.

    "Persistence is the magic of success." Paramahansa Yogananda

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: adding text to multiline textbox without erasing previous contents

    + is also used for addition. so with option strict off these 2 strings:

    "10" & "10" = "1010"
    "10" + "10" = 20

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

    Re: adding text to multiline textbox without erasing previous contents

    If you want to append text to a TextBox then call the AppendText method of that 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

  8. #8
    New Member
    Join Date
    Apr 2013
    Posts
    9

    Re: adding text to multiline textbox without erasing previous contents

    HOOORAY!

    I got it to work using TextBox2.Text &= Environment.NewLine & "entered my stuff here"

    Thanks all for the help and ideas.

  9. #9
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: adding text to multiline textbox without erasing previous contents

    Yippee. Now do it the proper way using the AppendText method like jmc told you to!!!! I can't believe the number of people, including some very advanced coders, who seem to be completely ignorant of this method!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: adding text to multiline textbox without erasing previous contents

    considering that james' posting was almost exactly two years ago... I'm not surprised... people often just find the first thing that works and stop reading.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: adding text to multiline textbox without erasing previous contents

    Quote Originally Posted by dunfiddlin View Post
    Yippee. Now do it the proper way using the AppendText method like jmc told you to!!!! I can't believe the number of people, including some very advanced coders, who seem to be completely ignorant of this method!
    there are often several ways to skin a cat, + in this case both are proficient uses of the language.

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

    Re: adding text to multiline textbox without erasing previous contents

    Quote Originally Posted by .paul. View Post
    there are often several ways to skin a cat, + in this case both are proficient uses of the language.
    They don't produce quite the same behaviour though and, generally speaking, the behaviour of AppendText is more desirable. I wouldn't call setting the Text property wrong though.
    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