CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2009
    Posts
    5

    Very basic question concerning text boxes...

    Well thanks to you all's help on my last post, my first C# program is almost completed. I'm working on the final touches right now. The last thing I'm doing is adding a log to my program. I've made a multi-line text box and I wish to output a sort of log to it as the program operate (like a status bar except multiline). I know you can set what's in the text box by saying something like txtLog.Text = "some text here" but since it's going to be continuously updated, is there a simple way to just add on to what already exists in the text box? Thanks again for all your help!

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Very basic question concerning text boxes...

    I woudl probably use a RichTextBox with ReadOnly set to true. You may want to set the background color back to 'Window' afterward. You can then use the AppendText method to add lines to the text box.

  3. #3
    Join Date
    Nov 2009
    Location
    BC, Canada
    Posts
    9

    Re: Very basic question concerning text boxes...

    txtLog.Text += "new text";

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Very basic question concerning text boxes...

    The AppendText method is nice because you do not have add your own newlines.

  5. #5
    Join Date
    Nov 2009
    Location
    BC, Canada
    Posts
    9

    Question Another TextBox Question.

    I have another question about textboxes which maybe I can slip in here ...

    In a RichTextBox is it possible to use right-to-left selectively. That is, can I have some lines right to left, and others left to right?

    And, of course, if so ... how?

  6. #6
    Join Date
    Dec 2009
    Location
    Malta (GMT +1)
    Posts
    21

    Re: Another TextBox Question.

    I believe, you can't and I suggest that you use a label to output logs, as it is neater. also about the addition to that, you can do

    txtLog.Text += System.Enviroment.NewLine + "Your text here";

    that will add a new line to the text box or whatever you got, and then add your text to the new line

    Hope this helps,
    ClayC

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Very basic question concerning text boxes...

    Keep in mind that the following approach may not be very performant for long logging sessions:

    Code:
     
    txtLog.Text += "\nnew text";
    This is because the += operator is going to have to copy the existing text out of the control, append to it and then copy it back. Each time this happens an entire new string is created.

    So the larger the log history becomes, the more noticeable the delay is going to be when each entry is logged.

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Another TextBox Question.

    Quote Originally Posted by ClayC View Post
    I believe, you can't and I suggest that you use a label to output logs, as it is neater. also about the addition to that, you can do

    txtLog.Text += System.Enviroment.NewLine + "Your text here";

    that will add a new line to the text box or whatever you got, and then add your text to the new line

    Hope this helps,
    ClayC
    I don't agree. While it would not be hard to switch over, using a RichTextBox gives you formatting options that a label does not. Labels should only be use for the most simple of text operations. Also, RichTextBox offers the AppendText( ) method which suggests that it may use a StringBuilder or some sort of growing buffer internally to avoid the performance issues that Arjay pointed out.

  9. #9
    Join Date
    Mar 2007
    Posts
    59

    Re: Very basic question concerning text boxes...

    Arjay gave the proper answer, but I would suggest you to look at the DataGridView. You can make a log window similar to visual studio (like the Error List). By using a BindingList with the DataSource of the DGV, it becomes quite easy to do some operations (sorting, filtering (ie errors/messages), etc) and display it the way you like. And it's fast.

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

Featured