Jump to content
xisto Community
turbopowerdmaxsteel

How To Scroll A Richtextbox Control To Previous Position?

Recommended Posts

I am working on a Syntax Highlighter for the latest version of Pika Bot (built in C# .NET) wherein the responses can be saved to a human friendly format. Example:-

/*Comment gfhfghfg*/Condition _Condition1u : BuddyComesOnline{	BotGroup == "_ssi";	BuddyName == "*";	StatusType == "Idle";	ForSeconds == "60";}/*Comment asdasd*/Effect _Effect1 : MessageBox{	Title = "Pika Bot";	Message = "sdfsfsfsf";	Icon = "Asterisk";}/*ghjgj*/Integer _Variable1 = "0";/*asdas*/Trigger _Trigger1 : ON{	Conditions	{		_Condition1u;	}	Effects	{		_Effect1;	}}Bot _Bot2{	ID = "asdasd";	Password = "ĹžĹâşĹžĹĂ´ĂâşĹžĹâşĹž";	Encrypt = "True";}/*asdasdas*/Bot _Bot1{	ID = "sdasd";	Password = "asdasda";	Encrypt = "False";}/*asdasda*/BotGroup _BotGroup1{	_Bot2}BotGroup _ssi{	_Bot1}

The syntax highligher does some basic coloring of the keywords namely: Condition, Effect, Integer, String, Trigger, Bot and BotGroup, string constants, comments and Condition/Effect Types such as BuddyComesOnline and MessageBox. I learnt that there are two ways to go about doing this. One is to use the SelectionStart, SelectionLength, SelectionColor properties provided by the RichTextBox control and second to modify the RTF code directly. The first process is rather slow and is marred by flickers, so I chose the second option. Even though its much faster there is one problem that I can't get through. Loading the entire RTF code causes the scroll position to be modified. The control only provides the ScrollToCaret() method which I am unable to use to restore the previous scroll position.

Is there any way to make a RichTextBox control scroll to a specific position?

I have attached the sample application which does the highlighting. It requires .NET Framework 2.0 or higher.

Share this post


Link to post
Share on other sites

Err, I've tried your app, it works fine on my machine, both with and without "Highlight Dynamically" checked. I've the latest update of .Net 2.0 including the service pack, via autoupdate. Could that have made a difference?These are my global assemblies' file versionmscorlib.dll = 2.0.50727.832System.Windows.Form.dll = 2.0.50727.832I do notice a bit a glitch, but not critical one. Whenever i clicked on the Highlight button, the text tends to shift down 1/2 a line, when i scrolled in such a way the first line is shown partially. Also, if i have a lot of text, clicking on the Highlight button will halt the app for a while before it return response.Anyway, I do have some idea for using the first method and to suppress the flicker. You can subclass the RichTextBox, and add an additional boolean as a property while overriding the OnPaint event. The boolean, when set to true, will bypass the OnPaint call to the base class, thus preventing the screen from updating. That way you can resume the screen update after you've highlighted all the text. You might need to call the Update method of your RichTextBox after you set the boolean to false.By the way, you should have explain a bit more on using the app that you attached. We wouldn't know what you actually wanted us to do with it.

Share this post


Link to post
Share on other sites

Thanx, lee.

That glitch is exactly what I was trying to solve. I found a solution to the problem which requires the usage of SendMessage API. First we create a property which is used to obtain the current position of the scrollbar just before the text is to be updated. Then I used the technique that you mentioned and subclassed the RichTextBox to prevent the paint messages from being processed. Lastly, the property was used to restore the position of the scrollbar.

The code can be found at http://forums.codeguru.com/showthread.php?281765-Disable-Scroll-in-RichTextBox

Share this post


Link to post
Share on other sites

Actually the RichTextBox is just trying to align the first line of text to show a full line. Why not just set the height of the control in such a way that all line are visible, no partial line is shown like the current case.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.