开发者

Prevent multiline TextBox from "stealing" scroll event

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 t开发者_StackOverflow社区extbox from taking the event and act more like a normal textbox, but still multiline?


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);
}


(Edit: assuming WPF here: the other method would work with WinForms)

You could add a PreviewMouseWheel handler on the TextBox, then set e.Handled = true; - in theory anyway!

http://msdn.microsoft.com/en-us/library/system.windows.uielement.previewmousewheel.aspx

Hope that helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜