From: Jesper, Denmark on
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
From: Nicholas Paldino [.NET/C# MVP] on
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
"Jesper, Denmark" <JesperDenmark(a)discussions.microsoft.com> wrote in message
news:EDDC0E3F-1E7D-4004-A927-1588576BFADF(a)microsoft.com...
> 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

From: Matt Brunell on
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;
}
}
}
}
}