RichTextBox driving me insane

Started by
1 comment, last by maya18222 14 years, 1 month ago
Im currently developing a c# winforms application that has a richtextbox control that im using to display and syntax highlight code files.I initially began this by updating the syntax highlighting as follows, which gets called whenever a KeyUp event is raised.

void OnUpdateSyntaxHighlighting()
{
  richtextbox.Text = Code;

  for all key words
  {
     for all instances found, select, and set colour
  }
}
Whilst this worked, it was slow, and as suggested by a member here, i decided to do the rtf formating myself. So now I do the following,

void OnUpdateSyntaxHighlighting()
{
  SavePosition();
     string rtf = AddRtfHeader();
     rtf += CodeWithRtfFormating();
     rtf += CloseRtfHeader();
     richtextbox.Rtf = rtf;   // this cause window to scroll up top top
  RestorePosition(); // this scrolls back down to the original position
}
But im suffering from a number of problems. One being that whenever I call OnUpdateSyntaxHighlighting(), you catch a glimpse of the window scrolling and rescrolling as it adds the new rtf text and then restores the position. And another problem is that after I do my own rtf formating, when using the cursor keys to navigate, theres this very strange jumping going on, where sometimes when at the bottom of the document, pressing up causes the scrollbar to scroll all the way up to the top. i have no idea about the second problem, but I figure I should be able to solve the first problem by disabling drawing of the rtf control whilst I am editing the rtf text, then calling invalidate at the end, so that it only draws once. But unlike other controls, RichTextBox doesnt seem to have a BeginUpdate()/EndUpdate() pair of functions. Any ideas on this? Also, am I using the richtextbox correctly? ie is there anything else I should be doing other than setting the RTF member to my formated text? Reason I ask is becuase, whilst im doing my own formating when the user releases any keys, isnt the richtextbox still also doing formating whenever the TEXT member changes, that Im just overwriting? Should I or can I disable this?
Advertisement
You can use LockWindowUpdate to prevent the window from repainting when you're updating the RTF. You can also use the EM_GETSCROLLPOS and EM_SETSCROLLPOS window messages to store and replace the scroll positions when updating.

You could also consider using a StringBuilder to build up the RTF string – it should be a lot quicker than using (immutable) strings!

However, I'd recommend you got rid of the RichTextBox entirely – it's more trouble than it's worth! (Try pressing Print Screen then pasting into your editor, for example, and note how the tab stop positions are not at whole multiples of the character width). There are some pretty good free text editors for .NET out there already, such as ScintillaNET (based on Scintilla, which is used in Programmer's Notepad and Notepad++).

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Thanks. But the items you suggested are all win32. So i take it you mean to use the SendMessage import?

[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

I had a play with this earlier, but all the links I found on google had different types for hWnd, wParam and lparam. Different combinations of int and Intptr. Is there a definate way, or does it depend on the message im sending?

Looking at the win32 headers, I believe

hWnd should be of type void*
wMsg should be of type unsigned int
wParam should be of type unsigned int
lParam should be of type long

Also this is just a learning exercise, I use other editors for working.

oh ! and I do use stringbuilder in my code, the pseudo was just ... well pseudo ;)

This topic is closed to new replies.

Advertisement