• 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

    • 007 First Light's first trailer shows off an action-packed James Bond game by Pulasthi Ariyasinghe IO Interactive has been teasing its next project outside of Hitman for some time now, and even before its first-ever showcase that's happening soon, the James Bond game showed up during the PlayStation State of Play event today. Dubbed 007 First Light, the third-person action-adventure title received a trailer showing off the new face of Bond, some of his gadgets, and plenty of action. Catch it above. While inspired by novels of Ian Fleming as well as the movie franchise, First Light will follow Bond to reveal just how he earned his MI6 007 role in an all-original story by IO Interactive. James Bond will be just 26 years old in this iteration. While not the smooth and tactical agent with a martini that we've seen in the movies just yet, this Bond is described as a man with "sharp instincts, sometimes reckless, who is still learning when to fight, when to bluff, and when to disappear into the shadows." As expected from the Hitman developer, missions in 007 First Light will offer both stealth and loud options, depending on the player's actions. A variety of futuristic gadgets will be available for use as well, while driving portions are also confirmed for the title. "In 007 First Light, Bond starts as a NAVY air crewman, when against all odds, an audacious act of bravery propels him on MI6’s most challenging training program," says the studio, regarding Bond's beginnings. "This training coupled with his natural instinct, wits, and heart will see him grow into a fully-fledged spy. It’s a completely original standalone story, developed in collaboration with Amazon MGM Studios." The studio says that the game will take players across the world, from snow-drenched mountains to sun-soaked beaches, while interacting with some of the most iconic characters in the franchise, including M, Q, and Moneypenny. There are original faces in the story too, such as Bond's mentor Greenway. 007 First Light is coming to PC (Steam and Epic Games Store, Xbox Series X|S, PlayStation 5, and the Nintendo Switch 2 sometime in 2026.
    • That sharp cold toothache you dread? Its origins trace back to ancient, unexpected purpose by Sayan Sen Image by Pavel Danilyuk via Pexels Scientists at the University of Chicago have discovered that the sensitive tissue inside our teeth first evolved as part of the armored skin of ancient fish. Their new study, published in Nature, confirms that dentine—a key part of teeth—was originally used by early vertebrates to sense their surroundings. This research supports the idea that dentine wasn’t always used for chewing. Instead, millions of years ago, it helped fish detect changes in the water. The study also clears up confusion about Anatolepis heintzi, a fossil once thought to be the earliest known vertebrate because of its dentine-like structures. For years, scientists debated whether Anatolepis was really an early vertebrate. The fossil had tiny tubules that some researchers believed were odontodes—small structures considered to be the ancestors of teeth. However, there wasn’t enough evidence to be sure. To settle the debate, scientists used synchrotron scanning, a powerful imaging technique, to study different fossils and modern creatures. The scans revealed that Anatolepis didn’t have dentine. Instead, its tubules were sensory structures similar to those found in arthropods like crabs and shrimp. These structures, called sensilla, help animals detect their surroundings. “This shows us that ‘teeth’ can also be sensory even when they’re not in the mouth,” said lead researcher Yara Haridy, PhD. “There’s sensitive armor in these fish. There’s sensitive armor in these arthropods. This explains the confusion with these early Cambrian animals.” Although Anatolepis turned out to be an arthropod, researchers did find real dentine in another ancient fish. The Ordovician vertebrate Eriptychius, which lived about 465 million years ago, had large dentine-filled tubules in its armor. This confirms that dentine first evolved in vertebrates as a sensory tissue. Further tests showed that modern fish, such as sharks and teleosts, still have nerve connections in their external dentine structures. This means early vertebrates may have used dentine to sense their environment before it became part of teeth. Scientists have two main ideas about how teeth came to be. The “inside-out” theory suggests teeth evolved first and were later adapted for exoskeletons. The new research supports the “outside-in” theory, which argues that sensory structures appeared first in exoskeletons and later evolved into teeth. While the team didn’t find the oldest vertebrate fish, study co-author Neil Shubin, PhD, believes the discovery is still important. “We didn’t find the earliest one, but in some ways, we found something way cooler,” he said. Source: University of Chicago, Nature This article was generated with some help from AI and reviewed by an editor. Under Section 107 of the Copyright Act 1976, this material is used for the purpose of news reporting. Fair use is a use permitted by copyright statute that might otherwise be infringing.
    • "How dare you profit off our user's data without compensating them. That's our job!"
  • Recent Achievements

    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
    • Apprentice
      DarkShrunken went up a rank
      Apprentice
    • Dedicated
      CHUNWEI earned a badge
      Dedicated
    • Collaborator
      DarkShrunken earned a badge
      Collaborator
  • Popular Contributors

    1. 1
      +primortal
      347
    2. 2
      snowy owl
      167
    3. 3
      +FloatingFatMan
      164
    4. 4
      ATLien_0
      161
    5. 5
      Xenon
      128
  • Tell a friend

    Love Neowin? Tell a friend!