Solved

GetScrollInfo and SetScrollInfo in RichTextBox

Posted on 2004-10-18
5
3,807 Views
Last Modified: 2008-01-09
Hello,

I'm trying to write a small JAVA text editor with basic real-time syntax highlighting.

I created a 'SyntaxBox' class and derived it from the RichTextBox control.
Inside the class I implemented a procedure 'Parse' which converts the text in the textbox to the Rich Text Format including the correct syntax color formatting.
I then copy the RTF text to the 'Rtf' property of the RichTextBox control and it shows me a nice and smooth highlighted syntax.

However, every time I update my rich text, the cursor runs to the start of the textbox.
I solved this by remembering the SelectionStart property and resetting it after the update.
BUT, I still have a problem with the scrollbar.

As soon as I start typing, the scrollbar jumps to another value.
I'm trying to read the scrollbar position using the 'GetScrollInfo' procedure in user32.dll (the GetScrollPos procedure doesn't seem to work).
This works great, however, I can't seem to set the scrollbar back to its previous value. (or to any value whatsoever, for that matter)
If I call SetScrollInfo(<params>) and immediately afterwards I call the GetScrollInfo procedure, it shows me the correct value.
However, the scroll position of the textbox is not updated...

So I have a few questions:

1.) am I using the right approach to syntax highlighting?
2.) how come the SetScrollInfo procedure doesn't update my textbox?

Here's some relevant code:

public void SetRTF(string text)
{
      ScrollInfoStruct s=new ScrollInfoStruct();

      s.fMask=SIF_POS; //only position
      s.cbSize=Marshal.SizeOf(s);

      if(GetScrollInfo(this.Handle,1,ref s)==0)
            MessageBox.Show("GetScrollInfo error: "+GetLastError().ToString());

      int st=this.SelectionStart;
      int sl=this.SelectionLength;

      this.Rtf=text;

      this.SelectionStart=st;
      this.SelectionLength=sl;

      if(SetScrollInfo(this.Handle,1,ref s,false)==0)
            MessageBox.Show("SetScrollInfo error: "+GetLastError().ToString());

      this.Refresh();
}

Thanks in advance...

Nicolas.
0
Comment
Question by:The_LowRider
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12341358
Try the ScrollToCaret method of the RichTextBox.

Bob
0
 
LVL 1

Author Comment

by:The_LowRider
ID: 12341605
> Try the ScrollToCaret method of the RichTextBox.

Bob, thanks for the suggestion, however, it doesn't work.
Resetting the SelectionStart property already takes care of this.

Maybe I should be more specific about the 'scrollbar jumps' part.

If I scroll down to the bottom of the text and then place the cursor on the first visible line of the textbox, like in the top part of this screenshot:

http://users.pandora.be/bajaman/screenshot.gif

When I run my syntax highlighting procedure (simply by entering some text), the textbox scrolls upward, like in the bottom part of the screenshot.

thanks,

Nicolas.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 12341642
Oooh, right, you want exact position of scroll bars.

Have you seen this?

Scrolling Around with the RichTextBox Control
http://www.codeproject.com/vb/net/VbNetScrolling.asp?print=true

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12341646
It is VB.NET, but might be convertible to C#.

Bob
0
 
LVL 1

Author Comment

by:The_LowRider
ID: 12341825
Yes! That PostMessageA procedure works great!

I stumbled upon that code in the beginning of my search but left it alone because it was VB.net and because it used those "really weird Windows messages" :)

Thanks, just what I needed!

Nicolas.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question