Forward message to textbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do I forward a mouse wheel event (or create one) for a textbox in C#?

I have a textbox that is within a tab page. The textbox is read-only and we
don't want to focus on the textbox because the cursor shows up, so we set the
focus on the tab page it is on. I can catch the MouseWheel event on the tab
page, but have no idea how to pass this on to the textbox to process.

As a side note, the text we are displaying contains tabs and newlines, so a
label doesn't work for us. Any help would be appreciated. Thanks!
 
Derive a class from Textbox

public class MyTextBox : TextBox
{
public void PerformMouseWheelUp(MouseEventArgs e)
{
this.OnMouseWheelUp(e);
}

public void PerformMouseWheelDown(MouseEventArgs e)
{
this.OnMouseWheenDown(e);
}
}

Make the control on your form an instance of this class and call the
PerformMouseXXX methods when you trap the MouseWheel events.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
 
Inherit TabPage and override its WndProc method
In WndProc handle the WM_MOUSEWHEEL message
and simply pass it to the textbox using one of the following
methods:

1. Use SendMessage (requires P/Invoke)
2. Call the WndProc method of your TextBox (it's protected though)
3. Create a temporary instance if the NativeWindow class, assign
the TextBox handle to it, call its DefWndProc method and then
release the handle again.

Method 3 is probably the easiest. It would look something like this
(in VB, you'll have to translate it to C#):

Private Const WM_MOUSEWHEEL As Integer = &H20A
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_MOUSEWHEEL Then
Dim wnd As New NativeWindow
wnd.AssignHandle(myTextBox.Handle)
wnd.DefWndProc(m)
wnd.ReleaseHandle()
Exit Sub
End If
MyBase.WndProc(m)
End Sub

/claes
 
Back
Top