TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > C#(C Sharp)

RichTextBox scroll position

C#(C Sharp)


Reply
 
LinkBack Thread Tools Display Modes
Old 08-09-2007, 01:00 PM   #1
Jesper, Denmark
Guest
 
Posts: n/a
RichTextBox scroll position

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
  Reply With Quote
Old 09-09-2007, 12:02 AM   #2
Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
Re: RichTextBox scroll position

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@discussions.microsoft.com> wrote in message
news:EDDC0E3F-1E7D-4004-A927-1588576BFADF@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


  Reply With Quote
Old 10-09-2007, 12:01 AM   #3
Matt Brunell
Guest
 
Posts: n/a
Re: RichTextBox scroll position

A couple of months ago I implemented some syncronized rich edit controls.
Follwing the guidance by
http://www.codeproject.com/vb/net/RT...dScrolling.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;
}
}
}
}
}

  Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Contract to hire position kumar C#(C Sharp) 0 05-09-2007 10:00 AM
mouse cursor position in XP Jeff Windows XP 6 27-08-2007 09:45 PM
Position of Displays in XP Langforc Windows XP 3 27-08-2007 06:26 PM
Menu position problems Pete Nicholls Windows Vista All 1 19-08-2007 09:41 PM
EA dominates No.1 chart position throughout '06 Dark Star Gamerz 0 04-01-2007 04:53 AM


< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +1. The time now is 03:06 AM.


vBulletin, Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2011, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional