How do you have the container handle its child controls' events (or other controls) ?
For example, I got my own (scrollable) UserControl, and I dynamically create some comboboxes, the problem is that once the focus is on one of the comboboxes, any scrolling that the user attempts to do(using the mouse wheel) are handled by the combobox itself (scrolling through its items). I want the usercontrol to get the scrolling event (and scroll) even if it doesn't currently have the focus.
Sometimes I just want events to pass to another control : I have a ContextMenu shown when a user clicks somewhere inside the usercontrol. When the user presses TAB, I , again, want the UserControl to handle it (using ProcessCmdKey override), but instead, the Tabbing is made in the menu.
Any ideas?
Please help me !
Loading
dannynPosted Jan 27, 2006, 5:07 AM
Basically what you need to do is capture the windows message(in this case WM_MOUSEWHEEL) in your control, and use the 'SendMessage()' function to send that same message, with the same parameters , to its parent (of course we need to change the first parameter which is the handle of the control that the message is meant for).
To capture the ComboBox WM_MOUSEWHEEL message, for example, we need to derive a class from it , so we can override the WinProc (the function that gets all the messages):
public class Combo : ComboBox
{
const int WM_MOUSEWHEEL = 522;
[DllImport("User32.dll")]
public static extern Int32 SendMessage(
int hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
int lParam)
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_MOUSEWHEEL)
{
SendMessage((int)Parent.Handle, m.Msg, (Int32)m.WParam,(Int32)m.LParam );
return;
}
base.WndProc(ref m);
}
}
We return from the function before reaching the 'base.WndProc...' because we don't want the combobox to scroll too (in some cases we would).
You could also examine and change the parameters before you pass the message around, but in this case we want the mousewheel event's arugments to pass exactly as they came.
Cheers.