CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2001
    Location
    North of France
    Posts
    25

    Richtextbox : how to avoid scrolling

    Hi All,

    I have a big problem that I can reproduce in the following source (the ScrollBars properties must be equal to rtfVertical before start).

    My question is :

    How to avoid the scrolling ?

    I would like the scrollBar to rest at the bottom position when I add some text.

    thank you very much,
    alouillet.


    private Sub Form_Load()
    Dim i as Integer

    me.Show
    RichTextBox1.Text = ""
    ' Must be in the properties :
    'RichTextBox1.ScrollBars = rtfVertical ' read only
    for i = 0 to 200
    RichTextBox1.Text = RichTextBox1.Text + "My text " + Str(i) + vbCrLf
    RichTextBox1.Refresh
    RichTextBox1.SelStart = len(RichTextBox1.Text) - 1
    next

    End Sub





  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Richtextbox : how to avoid scrolling

    I'm not sure what exactly you mean, but if it is the fact that you can see the scrollbar grow smaller and smaller (that is, the block in the scrollbar), you can use this code.

    strText = ""
    for i = 0 to 200
    strText = strText + "My text " + Str(i) + vbCrLf
    next

    RichTextBox1.Text = strText
    RichTextBox1.SelStart = len(RichTextBox1.Text) - 1





    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    Jul 2001
    Location
    North of France
    Posts
    25

    Re: Richtextbox : how to avoid scrolling

    Hi,

    I anderstand what you mean but this program is just to reproduce the problem, in the reality the text arrive in an Socket_DataArrival at any time like that :


    private Sub MySocket_DataArrival(byval bytesTotal as Long)
    Dim strData as string

    Sock_CLASS.GetData strData
    RichTextBox1.Text = RichTextBox1.Text + strData
    RichTextBox1.SelStart = len(RichTextBox1.Text) - 1

    End Sub




    So I search a tip to prevent the text cursor going at the top.

    thank for response.
    Laurent.

    PS : Sorry for my english, I'm french...;-)


  4. #4
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Richtextbox : how to avoid scrolling

    The .SelStart method should position the cursor any place within the RTB you chose. As an aside, If you wish to append text to a RichtextBox, or any control for that matter, it is usually quicker to use sendKeys then concatenating like your sample does
    Richtextbox1.Text = RichtextBox1.Text + additionalText is very inefficient. Use this instead

    private Sub Command1_Click()' append new text
    RTB.SelStart = len(RTB.Text)
    RTB.SetFocus ' Important
    SendKeys vbCrLf & Text1.Text, true
    End Sub



    You can set .SelStart to any position within the RTB and that is where SendKeys will insert your new text.
    As another aside, using the + sign to concatenate sometimes will produce undesired results. Ir your RichTextBox contains data that can be recognized as numeric and you are trying to append data that appears numeric, then VB will add them together not concatenate them.
    EG.
    RichTextBox1.Text = "12345"
    NewText = 25

    RichtextBox1 = RichtextBox1 + NewText
    will result in Richtextbox1 = 12370
    To Concatenate them use the Concatenation symbol & (ampersand) like so
    RichTextBox1 = RichTextBox1 & NewText
    This will yield
    1234525

    John G

  5. #5
    Join Date
    Jul 2001
    Location
    North of France
    Posts
    25

    Re: Richtextbox : how to avoid scrolling

    Hi john,

    Great, it is exactly what I search.

    However, I 'm not agree with you, I have a lot a data who transit and this method is less efficient (in term of rapidity) that .text=.text + "mytext".
    ( 4 seconds with the .Text method 13 seconds with the SendKeys method)

    Secondly, I have to SendKeys to a Form that have a Modal form in front of. So I can't make the RTB.SetFocus. Have you any solution ? (e.g. sendKeys to the hwnd of the RTB) or another method to append data to the RTB.


    Thank you very much for help.
    Laurent VOISIN.


  6. #6
    Join Date
    Jul 2001
    Location
    North of France
    Posts
    25

    Re: Richtextbox : how to avoid scrolling

    Hi,

    I found a very simple response to my question :

    RTB.Selstart = Len(RTB.Text)
    RTB.SelLength = 0
    RTB.SelText = "My Text"

    Thanks for responses.
    Laurent.


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