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?