防止多行文本框“窃取”滚动事件

Dan*_*iel 5 c# scroll textbox

我在 TabControl 中有一些多行文本框。选项卡控件有一个滚动条,但是一旦多行文本框获得焦点,滚动就会滚动文本框而不是选项卡控件。有什么方法可以阻止文本框接受事件并更像普通文本框,但仍然是多行的?

Han*_*ant 4

将 TextBox.ScrollBars 属性设置为 Vertical。这为文本框提供了一个滚动文本的滚动条。确保文本框适合 TabPage,这样标签页中就不会出现滚动条。例如,您可以将 TextBox.Dock 属性设置为 Fill。


我想不是这个,也许你在谈论鼠标滚轮。将新类添加到您的项目中并粘贴下面所示的代码。编译。将新控件从工具箱顶部拖放到表单上,替换现有的文本框。

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);
}
Run Code Online (Sandbox Code Playgroud)