Results 1 to 8 of 8

Thread: [RESOLVED] Using richtext box to write message on different line

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    31

    Resolved [RESOLVED] Using richtext box to write message on different line

    Hi,

    I am trying to make my code more clean and want to output various variables at different locations inside the code. Instead of using Msgbox I decided that using richtextbox would be more useful and doesn't require user to press enter as in Msgbox. So, at each location in the code, when output the variable to the rich text box, I want that particular output should be in the following line with the date time stamp. This way it would allow me to understand how much time was taken to execute each portion of the code.

    I am using richtextbox and the code format I am using is as follows:

    Code:
    RBox1.Text = "file1 has been read successfullY"
    At the next location I used :

    Code:
    RBox1.Text = RBox1.Text & vbNewLine & "Now writing the model output"
    Is this the correct approach ? I would appreciate if somebody could explain other useful options as well.

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Using richtext box to write message on different line

    Well you can do the same thing with a TextBox so I wonder why you chose a RichTextBox instead? Do you expect a huge amount of output?


    What you propose should work fine. However it breaks down a bit once the accumulated logged text gets huge.

    In that case you are better off setting the .SelStart property to the end of the text (you can use &H7FFF for TextBox and &H7FFFFFFF for RichTextBox) and then setting the .SelText property to your new line of text (and a vbNewLine):

    Code:
    With Text1
        .SelStart = &H7FFF
        .SelText = vbNewLine
        .SelText = "Some line of text"
    End With
    Because the advantage to doing this is large, you may as well do it any time you plan to "log" text line by line to a TextBox or RichTextBox.

    Your proposal works fine as I said, but it's a little bit "caveman" and can become a performance bottleneck. It is pretty expensive to fetch the entire contents, concatenate more onto the end, and then replace the contents with the new String value.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    31

    Re: Using richtext box to write message on different line

    Thank you for explaining it very clearly. At this point I don't want to output huge amount so may be I will stick with textbox as you suggested. I am not sure how to use the code you just wrote. Am I supposed to declare that at first ? How do I use at each instance ? This question might be trivial and stupid to you but I am not sure how to implement it.

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Using richtext box to write message on different line

    You could do it a lot of ways. One might be to create some subroutines:

    Code:
    Private Sub Log(ByVal Text As String)
        With Text1
            .Selstart = &H7FFF
            .SelText = Text
        End With
    End Sub
    
    Private Sub LogLine(Optional ByVal Text As String)
        Log Text
        Log vbNewLine
    End Sub
    This lets you log partial lines as well as new lines and even blank lines. Usually it works out better all around to have "newline" action after text rather than before it anyway.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    31

    Re: Using richtext box to write message on different line

    Thanks dilettante. Now, I understand how it works. This is way more efficient. I will close the thread as resolved now.

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Using richtext box to write message on different line

    Efficiency isn't everything. Lots of the time it doesn't make a difference.

    It is just good to be aware of alternatives.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    31

    Re: Using richtext box to write message on different line

    Of course being aware of alternatives is a good thing. Thanks again.

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] Using richtext box to write message on different line

    Just be sure to use a large enough value to set the selection start to "end."

    For TextBox the max is &H7FFF but an RTB has a max of &H7FFFFFFF.

Tags for this Thread

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