Results 1 to 9 of 9

Thread: Realtime richtext box updating while editing

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2008
    Posts
    3

    Question Realtime richtext box updating while editing

    Hello, I am glad to have come across a seamingly helpful community as this one! I am a vb 6 devoloper and a few friends and i are currently stumped on an interesting problem. We need to create a rich text box that can be updated while a user is typing somewhere else in the document. Pretty much we need to update text that is not at the cursor. We have created our own user control, but still havent been able to figure out how to do that.

    weve briefly looked into data grid controls but they do not have the formatting we are looking for and ultimatly just isnt as clean.

    Any help is muchly appreciated. Thanks!

  2. #2
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Realtime richtext box updating while editing

    try this

    Private Sub Text3_KeyPress(KeyAscii As Integer)
    With rtb
    .SelText = .SelText & Chr(KeyAscii)

    End With
    End Sub
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2008
    Posts
    3

    Re: Realtime richtext box updating while editing

    Thanks for the help. Unfortunately that will not work because we want to make it so that the text in the same rich text box can change dynamically while still having the user to be possibly typing at the time. For example, there is a timer that adds a "+" at the beginning of the rich text box every second, but we are having problems with when typing in the textbox and that timer fires with displaying correctly. We don't want the cursor to have to jump back to the beginning of the rich text box to add the "+" and interfere with the user entering text into the rich text box.

    Again thanks for your thoughts, any help is muchly appreciated!

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Realtime richtext box updating while editing

    In the timer event, write a line a of code after the code for "+".

    Code:
    Text1.selstart=len(Text1.text)
    Not sure, but just try it. It will place the cursor at the end of the TextBox, so the user can continue typing.....

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Realtime richtext box updating while editing

    Humm, I guess this would be considered a flaw in the standard richtextbox control as it lacks a feature that can be found in the RichEdit control that it implements. There are alternative solutions that allow to use a Range to modify text anywhere in the textbox without it interrupting anything, not causing screen to scroll or blink etc. - too bad that would mean downloading some other OCX that may have other flaws.

    Anyways, the way to go with SelText:

    Code:
    Private Sub Timer1_Timer()
        Dim lngSelStart As Long, lngSelLen As Long
        lngSelStart = RichTextBox1.SelStart + 1
        lngSelLen = RichTextBox1.SelLength
        RichTextBox1.SelStart = 0
        RichTextBox1.SelText = "+"
        RichTextBox1.SelStart = lngSelStart
        RichTextBox1.SelLength = lngSelLen
    End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2008
    Posts
    3

    Re: Realtime richtext box updating while editing

    hmmm... ok, thank you everybody for your suggestions. The method described would work with it jumping back to the end, but we are somewhat afraid after the number of lines increases the process is going to become slower and slower to the point of a nuissance to the end user. Does anyone have any ideas what other ocx's are out there that might work? I thought about creating my own, but I haven't been able to find much on how to create my own richtext box giving me the ability to add such a feature.

    Again, thanks for the suggestions!

  7. #7
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Realtime richtext box updating while editing

    This seems working.
    Needed to add a LockWindowUpdate and EM_GETSCROLLPOS/EM_GETSCROLLPOS to get/set current scrolling position of RTB. It flickers a little (due to the call to LockWindowUpdate).
    vb Code:
    1. Option Explicit
    2. Private Const WM_USER As Long = &H400
    3. Private Const EM_GETSCROLLPOS As Long = (WM_USER + 221)
    4. Private Const EM_SETSCROLLPOS As Long = (WM_USER + 222)
    5. '
    6. Private Type POINT
    7.     X As Long
    8.     Y As Long
    9. End Type
    10. '
    11. Private Declare Function SendMessage _
    12.                 Lib "user32.dll" _
    13.                 Alias "SendMessageA" (ByVal hwnd As Long, _
    14.                                       ByVal wMsg As Long, _
    15.                                       ByVal wParam As Long, _
    16.                                       ByRef lParam As Any) As Long
    17. Private Declare Function LockWindowUpdate _
    18.                 Lib "user32" (ByVal hwndLock As Long) As Long
    19.  
    20. Private Sub Timer1_Timer()
    21.     Dim lngSelStart As Long, lngSelLen As Long
    22.     Dim CurrentPos As POINT
    23.     '
    24.     LockWindowUpdate RichTextBox1.hwnd
    25.     SendMessage RichTextBox1.hwnd, EM_GETSCROLLPOS, 0, CurrentPos ' Get current scrolling position
    26.     '
    27.     lngSelStart = RichTextBox1.SelStart + 1
    28.     lngSelLen = RichTextBox1.SelLength
    29.     RichTextBox1.SelStart = 0
    30.     RichTextBox1.SelText = "+"
    31.     RichTextBox1.SelStart = lngSelStart
    32.     RichTextBox1.SelLength = lngSelLen
    33.     '
    34.     SendMessage RichTextBox1.hwnd, EM_SETSCROLLPOS, 0, CurrentPos
    35.     LockWindowUpdate 0
    36. End Sub
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  8. #8
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Realtime richtext box updating while editing

    It would be cleaner to make a call to WM_SETREDRAW to disable the painting temporarily. I'll see if I make a sample later, now I have to eat.

  9. #9
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Realtime richtext box updating while editing

    Here:
    Code:
    Option Explicit
    
    Private Declare Function InvalidateRect Lib "user32" (ByVal hWnd As Long, ByVal lpRect As Long, ByVal bErase As Long) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Private Const WM_SETREDRAW = &HB&
    
    Private Sub Timer1_Timer()
        Dim lngSelStart As Long, lngSelLen As Long, lngScrollPos(1) As Long
        SendMessage RichTextBox1.hWnd, WM_SETREDRAW, 0, ByVal 0&
        lngSelStart = RichTextBox1.SelStart + 1
        lngSelLen = RichTextBox1.SelLength
        RichTextBox1.SelStart = 0
        RichTextBox1.SelText = "+"
        RichTextBox1.SelStart = lngSelStart
        RichTextBox1.SelLength = lngSelLen
        SendMessage RichTextBox1.hWnd, WM_SETREDRAW, -1&, ByVal 0&
        InvalidateRect RichTextBox1.hWnd, 0, 0
    End Sub
    No blinking. But the scroll issue remains. EM_GETSCROLLPOS and EM_SETSCROLLPOS are not enough. One should also find out whether the view is at the bottom or not before using them, because otherwise it just gets stuck in the beginning.

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