• 0

C# richtextbox prevent autoscrolling with select() method?


Question

Hi,

I've created a syntax highlighting text editor that only updates the text that is visible at the time.

I've implemented the VScroll event so that the syntax highlighting updates when new text is scrolled into view. I highlight each keyword by select()'ing it, modifying the selectionfont, selectioncolor, etc properties, and then select()'ing back to the original caret position. select() causes the scrollbar to move, which causes a lot of flicker and actually stops me from using the richtextbox control.

One solution would be to just update the entire control rather than only the visible area, but with large documents this causes flickering.

Does anyone have any idea how I can select() text in the richtextbox without it automatically scrolling the contents?

Thanks!

2 answers to this question

Recommended Posts

  • 0
  On 08/02/2011 at 23:48, figgy said:

You may use LockWindowUpdate Windows API call as a quick fix.

I tried this and it didn't seem to solve the problem.

I borrowed some code from a blog which causes the rtb to flicker less while it's being updated:

        // Begin borrowed code from:
        // http://geekswithblogs.net/pvidler/archive/2003/10/15/182.aspx
        // All credits go to the above blog author :-) 

        [DllImport("user32", EntryPoint = "SendMessage")]
        private static extern int SendMessageB(IntPtr hWnd, uint msg, int wParam, ref POINT lp); 
        int updating = 0;
        IntPtr OldEventMask = IntPtr.Zero;
        const uint EM_GETSCROLLPOS = 0x0400 + 221;
        const uint EM_SETSCROLLPOS = 0x0400 + 222;
        struct POINT
        {
            public long X;
            public long Y;
        }
        POINT ScrollPosition = new POINT(); 
        public void BeginUpdate()
        {
            ++updating;
            if (updating > 1)
            {
                return;
            }
            SendMessageB(EditorBox.Handle, EM_GETSCROLLPOS, 0, ref ScrollPosition); 
            OldEventMask = SendMessage(EditorBox.Handle, EM_SETEVENTMASK, IntPtr.Zero, IntPtr.Zero);
            SendMessage(EditorBox.Handle, WM_SETREDRAW, IntPtr.Zero, IntPtr.Zero);
        }
        public void EndUpdate()
        {
            --updating;
            if (updating > 0)
            {
                return;
            }
            SendMessageB(EditorBox.Handle, EM_SETSCROLLPOS, 0, ref ScrollPosition); 
            SendMessage(EditorBox.Handle, WM_SETREDRAW, new IntPtr(1), IntPtr.Zero);
            SendMessage(EditorBox.Handle, EM_SETEVENTMASK, IntPtr.Zero, OldEventMask);
        }

        // End borrowed code. 

The get/setscrollbarpos is my current "solution" to the problem as it very nearly works, it just doesn't seem to work when I try to scroll above the line with the caret when using the up arrow on the scrollbar. It works fine scrolling up using the scroll wheel or the scrollbar thumb, just not the up arrow. Even more frustratingly, scrolling down past the caret works using the down arror, the thumb and the scroll wheel.

I call BeginUpdate(), then do all of my select() highlighting, then call EndUpdate(). If I change the EM_GET/SETSCROLLPOS lines to LockWindowUpdate(EditorBox.Handle) and LockWindowUpdate(0) respectively, this causes the scrollbar to flicker wildy and I'm unable to use the scrollbar entirely.

Any other ideas? I really can't believe Microsoft didn't implement a way to select text without scrolling seeing as that's the only way you can apply formatting to text!

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I guess my brain works differently than other people. I often can't think of the name of the app, but know which icon I want. I don't like icons on my desktop, so the ones I use all the time are pinned to the taskbar, and frequently used ones are pinned to the start menu.
    • eM Client 10.3.2412.0 by Razvan Serea eM Client is a full featured e-mail client with a modern and easy to use interface. eM Client also offers calendar, tasks, contacts and chat. eM Client supports all major services including Gmail, Exchange, iCloud, and Outlook.com. You can easily import your data from most of the other e-mail clients. This includes Microsoft Outlook, Outlook Express, Windows Mail, Windows Live Mail, Thunderbird, The Bat and more. eM Client fully supports touch devices like touch-enabled laptops, tablets and hybrid devices. Use your email client easily in a modern way. eM Client PRO vs. Free version While the Free license allows you to set up the maximum of two accounts in the application, it is possible to add an unlimited number of accounts with the PRO license. The PRO license also enables you to use eM Client for commercial purposes. Commercial use is any activity that helps you make profit, the Free license therefore cannot be used in company settings or on personal computers for business correspondence. PRO users also gain access to the dedicated support system and to the licensing manager. eM Client has been fully optimized to run smoothly on Windows Vista, 7, 8, 10 and 11. eM Client 10.3.2412.0 changelog: Added support for invitation forwarding on IceWarp servers Moving message from POP3 account to local folders no longer deletes the message from POP3 server storage Fixed an issue with some spellcheck languages that might have caused crashes (hungarian, danish and more) Fixed a conflict between a shortcut for a new message with some special characters in some languages (ie. polish) More bug fixes Download: eM Client 10.3.2412 | 128.0 MB (Free, paid upgrade available) View: eM Client Website | eM Client Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • It sometimes comes down to leadership. Where Panos Panay was more about new features and strageic ads, the new lead for Windows seems to be more about fixing performance and consistency along with moving more components to the new settings app. Also for the first time, the Xbox team is allowed to play under the hood and tweaking the OS for what could be a Game Ready Edition. I like the new direction but it was a longer than expected journey.
  • Recent Achievements

    • Week One Done
      BlakeBringer earned a badge
      Week One Done
    • Week One Done
      Helen Shafer earned a badge
      Week One Done
    • First Post
      emptyother earned a badge
      First Post
    • Week One Done
      Crunchy6 earned a badge
      Week One Done
    • One Month Later
      KynanSEIT earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      658
    2. 2
      ATLien_0
      269
    3. 3
      Michael Scrip
      234
    4. 4
      Steven P.
      164
    5. 5
      +FloatingFatMan
      152
  • Tell a friend

    Love Neowin? Tell a friend!