RichTextBox Scroll events

R

Rachel Suddeth

I want to react to scrolls in a RichTextBox (I want to let the user know
when he has changed to a new printable page.) I have handled the VScroll()
event, but that only responds to scrollbar clicks. If the user slides the
"thumb", I seem to get no indication. Anyone know how to get notified of the
thumb slide from a RichTextBox? I can derive from RichTextBox and trap
messages if need be, but I have not been able to figure out what to respond
to (and I'm still hoping for a better way that I don't have to create a
derived class?)

-Rachel
 
H

hyuki

Rachel

Try to use the WM_VSCROLL event.

//example:
public class HRichTextBox : System.Windows.Forms.RichTextBox
{
private const int WM_VSCROLL = 277;

public event System.EventHandler OnVerticalScroll;

public HRichTextBox()
{
}

protected override void WndProc(ref System.Windows.Forms.Message m)
{
if(m.Msg == WM_VSCROLL && OnVerticalScroll != null)
OnVerticalScroll(this, new System.EventArgs()); // your new vscroll
event handler

base.WndProc (ref m);
}
}

Yong Hyeok Seo
(e-mail address removed)
MCAD, MVP[C#]
 
R

Rachel Suddeth

Thanks. It looks like this is part of the solution. WM_VSCROLL notification
is sent more often than I want to handle this event (eg twice for every
scroll button click), and it isn't sent at some times I want to be notified
(like up/down arrows causing a scroll.) To catch it only when I want it,
I'll need to look at the wParam. I can choose either SB_THUMBPOSITION or
SB_THUMBTRACK (depending on whether I want to update only when the mouse is
released or during scrolling), but I will need to avoid looking at the
high-order word if I want to test for equality (which I do, since these are
not defined as flags -- they are 4 and 5 repectively.)

I believe it will work to use the existing VScroll event, and do something
like this:

protected override void WndProc(ref System.Windows.Forms.Message m)
{
if ( m.Msg == WM_VSCROLL &&
(short) m.WParam == SB_THUMBPOSITION )
OnVScroll( this, EventArgs.Empty );
base.WndProc ( ref m );
}

-Rachel

hyuki said:
Rachel

Try to use the WM_VSCROLL event.

//example:
public class HRichTextBox : System.Windows.Forms.RichTextBox
{
private const int WM_VSCROLL = 277;

public event System.EventHandler OnVerticalScroll;

public HRichTextBox()
{
}

protected override void WndProc(ref System.Windows.Forms.Message m)
{
if(m.Msg == WM_VSCROLL && OnVerticalScroll != null)
OnVerticalScroll(this, new System.EventArgs()); // your new vscroll
event handler

base.WndProc (ref m);
}
}

Yong Hyeok Seo
(e-mail address removed)
MCAD, MVP[C#]

Rachel Suddeth said:
I want to react to scrolls in a RichTextBox (I want to let the user know
when he has changed to a new printable page.) I have handled the VScroll()
event, but that only responds to scrollbar clicks. If the user slides the
"thumb", I seem to get no indication. Anyone know how to get notified of
the
thumb slide from a RichTextBox? I can derive from RichTextBox and trap
messages if need be, but I have not been able to figure out what to
respond
to (and I'm still hoping for a better way that I don't have to create a
derived class?)

-Rachel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top