3

I have this function:

private void clearRichtextBox()
{
    richTextBox2.Clear();
    foreach (KeyValuePair<string, List<string>> kvp in LocalyKeyWords)
    {
        for (int i = 0; i < kvp.Value.Count(); i++)
        {
            richTextBox2.AppendText("Url: " + kvp.Key + " --- " + "Localy KeyWord: " + kvp.Value[i]+Environment.NewLine);
        }
    }   
}

Where its doing the Clear(); it looks like the richTextBox blink/flash for milisecond or so. Any way to avoid stop it ?

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
user1741587
  • 769
  • 2
  • 6
  • 27
  • Flashing or blinking is related to the "paint" event and if you do clearing repeatedly, it is inevitable! Do you really need to clear all the content every time?! – moorara Oct 14 '12 at 22:24
  • Milad im clearing it each time i click OK. Its not automatic. If there was another way to clear/delete all the text from the richTextBox without using Clear() i was using it. Is there any other way ? – user1741587 Oct 14 '12 at 22:28

5 Answers5

4

I ran into a similar problem recently. The solution I chose was to add extension methods to the richtextbox. I like this approach because it's clean, contained, and easily reusable.

This makes pausing the redraw is as simple as

richtextbox.SuspendDrawing();

and resuming

richtextbox.ResumeDrawing()

public static class RichTextBoxExtensions
{
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
    private const int WM_SETREDRAW = 0x0b;

    public static void SuspendDrawing(this System.Windows.Forms.RichTextBox richTextBox)
    {
        SendMessage(richTextBox.Handle, WM_SETREDRAW, (IntPtr)0, IntPtr.Zero);
    }

    public static void ResumeDrawing(this System.Windows.Forms.RichTextBox richTextBox)
    {
        SendMessage(richTextBox.Handle, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero);
        richTextBox.Invalidate();
    }
}

Source: https://stackoverflow.com/a/487757/3442448

Community
  • 1
  • 1
Jason L.
  • 1,125
  • 11
  • 19
2

You can't avoid the RichTextBox's intrinsic behavior to queue up at least one redraw whenever the text changes, but in the code snippet you've provided, you can just set prepare the text out of band instead of calling clear. This will cut the amount of visual activity change to a minimum without having to change it's normal operation. In addition, it looks like you wish to keep the contents the same as the view for your KeyValue set.

Use a StringBuilder to assemble your text before applying it to the RichTextBox, then assign the Text property if there is a difference. If there is no change to the contents of the RichTextBox, it will not redraw everything, which is the source of the flashing behavior that you are seeing.

private void clearRichtextBox()
{
    StringBuilder sb = new StringBuilder();

    foreach (KeyValuePair<string, List<string>> kvp in LocalyKeyWords)
    {
        for (int i = 0; i < kvp.Value.Count(); i++)
        {
            sb.AppendFormat("Url: {0} --- Localy KeyWord: {1}{2}", kvp.Key,kvp.Value[i],Environment.NewLine);
        }
    }

    string viewString = sb.ToString();
    if(viewString != richTextBox2.Text)
    {
         richTextBox2.Text = viewString;
    }
}
meklarian
  • 6,595
  • 1
  • 23
  • 49
1

It's just how it works. You can try to enable double-buffering on your form:

this.DoubleBuffered = true;

The other way is to inherit the RichTextBox control and override its New() method using SetStyle() to enable double-buffering for the control itself.

How to to this with this method:

public class MyRichTextBox : RichTextBox {

    protected override void New() {
        base.New();
        SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
    }
}

Then you need to do the following steps:

  1. Close the designer window
  2. Open the designer file
  3. You will need to replace the RichTextBox twice with MyRichTextBox, one close to the top of the file where it's declared and one close to the bottom where it's initialized.
  4. save and re-open designer / test
  • Abdias tried the DoubleBuffered = true; in the constructor it didnt help. Can you show me how to do it with the New() SetStyle() to enable DoubleBuffering for the control ? – user1741587 Oct 14 '12 at 22:29
0

This may help too:

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/a6abf4e1-e502-4988-a239-a082afedf4a7

It uses SendMessage to turn on/off the redraw mode of the control.

U1199880
  • 907
  • 1
  • 10
  • 21
0

I haven't tried it to see if it also blinks but you could just set the value of the RTB to be an empty string.

richTextBox2.Text = "";
foreach (KeyValuePair<string, List<string>> kvp in LocalyKeyWords)
{
    for (int i = 0; i < kvp.Value.Count(); i++)
    {
        richTextBox2.AppendText("Url: " + kvp.Key + " --- " + "Localy KeyWord: " + kvp.Value[i]+Environment.NewLine);
    }
} 
keyboardP
  • 68,824
  • 13
  • 156
  • 205