Fix for the RichTextBox Undo Information Lost When Retrieving Text

Posted Friday, August 06, 2004 6:20 PM by pwelter34
In my work with the RichTextBox control, I've often run into this bug in the RichEdit control. You can read more about this bug by looking at Microsoft's knowledge base article - 812943. If you are unable to get the latest Riched20.dll then you can get around the problem using the following code …
 
 
using System; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace TextUndoBuffer { /// <summary> /// Work around for KB 812943, The RichEdit Control Undo /// Information May Be Lost When the Control Retrieves Text. /// </summary> public class RichTextBoxEx : RichTextBox { [DllImport("user32.dll", EntryPoint="SendMessage", CharSet=CharSet.Auto )] static extern int SendMessage(IntPtr hWnd, int msg,
ref GETTEXTEX wParam, System.Text.StringBuilder lParam); [DllImport("user32.dll", EntryPoint="SendMessage", CharSet=CharSet.Auto )] static extern int SendMessage(IntPtr hWnd, int msg,
ref GETTEXTLENGTHEX wParam, int lParam); [StructLayout(LayoutKind.Sequential)] struct GETTEXTEX { public Int32 cb; public Int32 flags; public Int32 codepage; public IntPtr lpDefaultChar; public IntPtr lpUsedDefChar; } [StructLayout(LayoutKind.Sequential)] struct GETTEXTLENGTHEX { public Int32 flags; public Int32 codepage; } const int WM_USER = 0x0400; // RichEdit messages (Richedit.h) const int EM_GETTEXTEX =(WM_USER + 94); const int EM_GETTEXTLENGTHEX =(WM_USER + 95); // Flags for the GETEXTEX data structure const int GT_DEFAULT = 0; // Flags for the GETTEXTLENGTHEX data structure const int GTL_DEFAULT = 0; // Do default (return # of chars) const int GTL_CLOSE = 4; // Fast computation of a "close" answer public RichTextBoxEx() { } public override string Text { get { GETTEXTLENGTHEX getLength = new GETTEXTLENGTHEX(); getLength.flags = GTL_CLOSE; //get buffer size getLength.codepage = 1200; //Unicode int textLength = SendMessage(base.Handle, EM_GETTEXTLENGTHEX, ref getLength, 0); GETTEXTEX getText = new GETTEXTEX(); getText.cb = textLength+2; //add space for null terminator getText.flags = GT_DEFAULT; getText.codepage = 1200; //Unicode StringBuilder sb = new StringBuilder(getText.cb); SendMessage(base.Handle, EM_GETTEXTEX, ref getText, sb); return sb.ToString(); } set { base.Text = value; } } public override int TextLength { get { GETTEXTLENGTHEX getLength = new GETTEXTLENGTHEX(); getLength.flags = GTL_DEFAULT; //Returns the number of characters getLength.codepage = 1200; //Unicode return SendMessage(base.Handle, EM_GETTEXTLENGTHEX, ref getLength, 0); } } } }
 
~ Paul
Filed under:

Comments

# re: Fix for the RichTextBox Undo Information Lost When Retrieving Text

Wednesday, August 25, 2004 12:28 PM by Julian Gall

I have just checked and the version of RICHED20 shipped with Windows XP SP2 fixes this problem.

# re:Fix for the RichTextBox Undo Information Lost When Retrieving Text

Sunday, April 17, 2005 3:40 AM by TrackBack

^_~,pretty good!

# re: Fix for the RichTextBox Undo Information Lost When Retrieving Text

Saturday, August 04, 2007 5:35 PM by khadden

I've run into this in .net 2005 C# on Vista...

What in the world is going on? This is a brand new clean install of vista so I highly doubt that the riched20.dll has been replaced. In fact the version is 5.31.23.1225 so it shouldn't be a problem. I have tried your code, but it didn't help.

# re: Fix for the RichTextBox Undo Information Lost When Retrieving Text

Saturday, September 22, 2007 5:36 AM by ajam

I have to second that.  Tried, but didn't work at all.  Very frustrating little problem!

# re: Fix for the RichTextBox Undo Information Lost When Retrieving Text

Tuesday, October 23, 2007 10:43 AM by Steven Chen

You're so Good!!

I meet this bug in 64bit XP, and now I can use the RichTextBox as normal by your code.

Note for other people,

you should include "using System.Runtime.InteropServices;"

for DllImport in C#.

And what's more, I use "int" datatype instead of "Int32".

# re: Fix for the RichTextBox Undo Information Lost When Retrieving Text

Wednesday, November 28, 2007 7:18 PM by khadden

Well, I've just come back to the project that is using this control. (See my post above). The fix you posted works a little. It's odd that if I type text into the control, undo does not work. The only thing that happens is that the cursor moves back to the begining of the text I typed. However if I paste text into the control, undo works fine.

If anyone has any suggestions, please forward to me.

# re: Fix for the RichTextBox Undo Information Lost When Retrieving Text

Friday, February 01, 2008 5:52 PM by Saruwatari

Thanks for the solution!

Leave a Comment

(required) 
(required) 
(optional)
(required)