Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent multiline TextBox from "stealing" scroll event

Tags:

c#

scroll

textbox

I have a few multiline textboxes in a TabControl. The tabcontrol has a scrollbar, but as soon as a multiline textbox is focused scrolling will scroll the textbox instead of the tabcontrol. Is there any way to stop the textbox from taking the event and act more like a normal textbox, but still multiline?

like image 876
Daniel Avatar asked Oct 14 '22 03:10

Daniel


1 Answers

Set the TextBox.ScrollBars property to Vertical. That gives the text box a scrollbar that scrolls the text. Make sure the TextBox fit the TabPage so you don't get a scrollbar in the tab page. You could set the TextBox.Dock property to Fill for example.


That wasn't it I guess, maybe you are talking about the mouse wheel. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form, replacing your existing text boxes.

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class MyTextBox : TextBox {
    protected override void WndProc(ref Message m) {
        // Send WM_MOUSEWHEEL messages to the parent
        if (m.Msg == 0x20a) SendMessage(this.Parent.Handle, m.Msg, m.WParam, m.LParam);
        else base.WndProc(ref m);
    }
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}
like image 61
Hans Passant Avatar answered Oct 19 '22 11:10

Hans Passant