Search This Blog

Friday, October 31, 2008

Reduce in Flicker in Rich Text Box

From forums.microsoft.com

MSFT forgot to add the BeginUpdate and EndUpdate methods to RTB. Here's a sample workaround that uses SendMessage():

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.Load += new System.EventHandler(this.Form1_Load);
this.button1.Click += new System.EventHandler(this.button1_Click);
}
private void Form1_Load(object sender, EventArgs e) {
for (int ix = 0; ix < 200; ++ix) richTextBox1.Text += "nobugz waz here\r\n";
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private extern static IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);

private void button1_Click(object sender, EventArgs e) {
richTextBox1.Focus();
SendMessage(richTextBox1.Handle, 0xb, (IntPtr)0, IntPtr.Zero);
for (; ; ) {
int pos = richTextBox1.Find("bugz", richTextBox1.SelectionStart + 1, RichTextBoxFinds.None);
if (pos < 0) break;
richTextBox1.SelectionStart = pos;
richTextBox1.SelectionLength = 4;
richTextBox1.SelectionBackColor = Color.Aqua;
}
richTextBox1.SelectionStart = 0;
richTextBox1.SelectionLength = 0;
SendMessage(richTextBox1.Handle, 0xb, (IntPtr)1, IntPtr.Zero);
richTextBox1.Invalidate();
}
}
}

No comments: