I have windows form with vertical scrollbar and user can scroll up and down normally while control that does not handle WM_MOUSEWHEEL
is selected because form itself process that event, therefore scrolling works perfectly fine.
However, when I select multiline textbox for example I can't scroll my form on mousewheel because WM_MOUSEWHEEL
is handled by multiline textbox. I found this solution on StackOverflow and it works just fine:
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == 0x020a)
{
NativeMethods.SendMessage(this.Handle, m.Msg, m.WParam, m.LParam);
return true;
}
return false;
}
internal class NativeMethods
{
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);
}
The form scrolls up and down on mousewheel event no matter which child control is focused, but my boss does not want to use this solution because he fears it could somehow crash on another PC with different Windows or something. Anyhow, I would be satisfied if I could remove focus from children controls when user clicks on form, so I tried this:
private void MyWindowsForm_MouseDown(object sender, MouseEventArgs e)
{
this.Focus();
}
It does not work since focus is automatically set to the first child control on the form.
My question is: "Is there any way to remove focus from child controls when user clicks on the form so I can scroll my form using mousewheel normally?"
Hopefully you can understand what I'm trying to do. Thanks in advance!