Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

RichTextBox scroll position

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jesper, Denmark - 08 Sep 2007 12:54 GMT
Hi,

I have a somewhat long calculation report printed out in a RichTextBox. To
find or monitor a particular value, users scroll down to the location of the
data in the RichTextBox. However, when the user changes the input data, a
recalculation is made and a new report is generated. This resets the
RichTextBox vertical scroll bar to the top.

I would like to be able to read the position the scroll bar is moved to
before recalculation. Then, after recalculation, I would like to set the
scroll bar to the previous position (or merely nearby). Are these operation
possible from code.

Please do not suggest another way that I can present my data, I know it
sounds a like a newbie way of presenting data, however, it's not - the
varying nature of the data require this presentation.

regards Jesper, DK
Nicholas Paldino [.NET/C# MVP] - 08 Sep 2007 23:03 GMT
Jesper,

   You can call the GetScrollBarInfo through the P/Invoke layer to get the
location of the slider on the vertical scrollbar for the rich text box
control.  You can then call the SetScrollPos API function to set the
location of the new scroll bar.

   Of course, you have to assure that that the new content is large enough
to place the slider at the position you want (or you should check before
setting the value after you repopulate the control).

i
> Hi,
>
[quoted text clipped - 16 lines]
>
> regards Jesper, DK
Matt Brunell - 09 Sep 2007 23:15 GMT
A couple of months ago I implemented some syncronized rich edit controls.
Follwing the guidance by
http://www.codeproject.com/vb/net/RTFSynchronizedScrolling.asp I altered it
somewhat for my requirements.

You are going to run into problems if the textbox is larger that 65536
pixels.
The way I solved this problem was to implement an error correction
algorithm.  Code shown below.

public class ExRichTextBox : System.Windows.Forms.RichTextBox
{

private double _Yfactor = 1.0d;

public Point ScrollPos
{
get
{
Point scrollPoint = new Point();

SendMessage(this.Handle, (int)WindowsMessages.EM_GETSCROLLPOS, 0, ref
scrollPoint);
return scrollPoint;
}
set
{
Point original = value;
if (original.Y < 0)
original.Y = 0;
if (original.X < 0)
original.X = 0;

Point factored = value;
factored.Y = (int)((double)original.Y * _Yfactor);

Point result = value;

SendMessage(this.Handle, (int)WindowsMessages.EM_SETSCROLLPOS, 0, ref
factored);
SendMessage(this.Handle, (int)WindowsMessages.EM_GETSCROLLPOS, 0, ref
result);

int loopcount = 0;
int maxloop = 100;
while (result.Y != original.Y)
{
// Adjust the input.
if (result.Y > original.Y)
factored.Y -= (result.Y - original.Y) / 2 - 1;
else if (result.Y < original.Y)
factored.Y += (original.Y - result.Y) / 2 + 1;

// test the new input.
SendMessage(this.Handle, (int)WindowsMessages.EM_SETSCROLLPOS, 0, ref
factored);
SendMessage(this.Handle, (int)WindowsMessages.EM_GETSCROLLPOS, 0, ref
result);

// save new factor, test for exit.
loopcount++;
if (loopcount >= maxloop || result.Y == original.Y )
{
_Yfactor = (double)factored.Y / (double)original.Y;
break;
}
}
}
}
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.