Stopping TextBox flicker during update

前端 未结 6 1173
情书的邮戳
情书的邮戳 2020-12-20 12:26

My WinForms application has a TextBox that I\'m using as a log file. I\'m appending text without the form flickering using TextBox.AppendText(string);, however

6条回答
  •  感情败类
    2020-12-20 12:55

    Mathijs answer is works for me. I've modified it slightly so I can use with any control - a control extension:

    namespace System.Windows.Forms
    {
        public static class ControlExtensions
        {
            [System.Runtime.InteropServices.DllImport("user32.dll")]
            public static extern bool LockWindowUpdate(IntPtr hWndLock);
    
            public static void Suspend(this Control control)
            {
                LockWindowUpdate(control.Handle);
            }
    
            public static void Resume(this Control control)
            {
                LockWindowUpdate(IntPtr.Zero);
            }
    
        }
    }
    

    So all you need to do is:

    myTextBox.Suspend();
    // do something here.
    myTextBox.Resume();
    

    Works well. All flickering stops.

提交回复
热议问题