Solution to the Annoying Flickering Problem When Updating Textboxes

A solution to the annoying flickering problem when you update Textboxes in Windows forms.

I had to deal with the annoying flickering-textbox problem yesterday. When I had to deal with this previously, I used a Windows Control that I got from a sample application at some point. But as I like to know everything, I googled for a solution to this problem that I could understand.

Here's one easy way to get your textbox to stop flickering: We're going to call the Win32 API function LockWindowUpdate before making any changes to the text in the textbox.

try
{
    LockWindowUpdate(AwesomeNewTextBox.Handle);

    // Whatever 

    HighlightKeywords(AwesomeNewTextBox);

    // Etc.

}

finally
{
    LockWindowUpdate(System.IntPtr.Zero);
}

This function is found in the User32 dll, so we need to import it:

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool LockWindowUpdate(System.IntPtr hWndLock);

One thing to note is that you can only lock one window at a time. That means that if someone else has a window locked, then you can't lock this one. It also means that someone else can lock a window, which means that your window gets unlocked and therefore your textbox will flicker. There's a solution, found at the same site I found this one; look at the update.

Original article here

By Sadhana G   Popularity  (2180 Views)