Reputation: 929
In my windows forms application it occurs frequently that a user scrolls through a panel and is then blocked by a richtextbox
. I would like to catch the scroll event and send it to the panel when the richtextbox
vertical scrollbar is not visible.
I already found the code to check if the vertical scrollbar is visible in this thread: https://social.msdn.microsoft.com/Forums/en-US/a3facad3-0eae-4610-9a63-1b6c7a718bf5/how-do-you-determine-if-vertical-scroll-bar-is-visible-in-richtextbox?forum=winforms
Also the VScroll
event of a richtextbox
is only triggered when the verticalscrollbar is already visible.
What would be the correct way to catch the mousescroll and send it to the correct panel?
Upvotes: 0
Views: 5580
Reputation: 325
I think you can add a event to your rich text box in your Form1.Designer.cs like this code:
this.richTextBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.richTextBox1_MouseWheel);
and add this function to your form:
private void richTextBox1_MouseWheel(object sender, MouseEventArgs e)
{
Control control = sender as Control;
if (!NativeMethods.VerticalScrollBarVisible(control))
{
int numberOfTextLinesToMove = e.Delta * SystemInformation.MouseWheelScrollLines;
int numberOfPixelsToMove = numberOfTextLinesToMove * Convert.ToInt32(control.Font.Size);
if (panel1.VerticalScroll.Value - numberOfPixelsToMove < panel1.VerticalScroll.Minimum)
panel1.VerticalScroll.Value = panel1.VerticalScroll.Minimum;
else if (panel1.VerticalScroll.Value - numberOfPixelsToMove > panel1.VerticalScroll.Maximum)
panel1.VerticalScroll.Value = panel1.VerticalScroll.Maximum;
else
panel1.VerticalScroll.Value -= numberOfPixelsToMove;
}
}
The VerticalScrollBarVisible
method is explaned in this MSDN thread.
Upvotes: 2
Reputation: 929
The event MouseWheel
suggested by @Farshad does also trigger when the vertical scrollbar is not visible. I then added the following code to scroll panel1
. Note that the MouseWheel
event is not show in the visual studio designer and must be added to Form.Designer.cs manually.
private void richTextBox1_MouseWheel(object sender, MouseEventArgs e)
{
Control control = sender as Control;
if (!VerticalScrollBarVisible(control))
{
int numberOfTextLinesToMove = e.Delta * SystemInformation.MouseWheelScrollLines;
int numberOfPixelsToMove = numberOfTextLinesToMove * Convert.ToInt32(control.Font.Size);
if (panel1.VerticalScroll.Value - numberOfPixelsToMove < panel1.VerticalScroll.Minimum)
panel1.VerticalScroll.Value = panel1.VerticalScroll.Minimum;
else if (panel1.VerticalScroll.Value - numberOfPixelsToMove > panel1.VerticalScroll.Maximum)
panel1.VerticalScroll.Value = panel1.VerticalScroll.Maximum;
else
panel1.VerticalScroll.Value -= numberOfPixelsToMove;
}
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private extern static int GetWindowLong(IntPtr hWnd, int index);
public static bool VerticalScrollBarVisible(Control ctl) {
int style = GetWindowLong(ctl.Handle, -16);
return (style & 0x200000) != 0;
}
Upvotes: 0