允许鼠标滚轮在 TabPage 控件上滚动

如何解决允许鼠标滚轮在 TabPage 控件上滚动

使用 XML 源填充 TabControl 的 TabPage。将 XML 内容加载到 TabPage 后,TabPage 的两侧会出现两个 ScrollBar,以允许用户滚动。
但是,用户无法使用鼠标滚轮滚动。我已经检查了 TabPage 控件的属性,但找不到任何可以帮助处理此问题的属性

有人建议处理 MouseWheel 事件或覆盖 OnMouseWheel,但我不确定如何应用。
要点很简单,如何在标签页上激活鼠标滚轮滚动?

public partial class ModifyTransformerContentsView : Form
{
    private readonly ITransformerConfigurationviewmodel viewmodel;

    public ModifyTransformerContentsView(ITransformerConfigurationviewmodel viewmodel)
    {
        InitializeComponent();

        this.viewmodel = viewmodel;
        this.viewmodel.Notify += this.OnNotify;

        this.xmlEditExampleStdfOutFile.SetFormateText(File.ReadAllText(this.viewmodel.SampleProcessingFilePath));
        this.xmlEditExampleStdfOutFile.ReadOnly = true;

        this.rtbXsl.SetFormateText(File.ReadAllText(this.viewmodel.TransformerFilePath));
        this.rtbXsl.ReadOnly = false;
        this.rtbXsl.RichTextBox.ClearUndo();

        this.btnSave.Enabled = false;

        this.rtbCheatSheet.Text = File.ReadAllText(this.viewmodel.CheatSheetFilePath);
    }

    private void OnValidateClick(object sender,System.EventArgs e)
    {
        this.viewmodel.SetTemporaryTransformerFileContents(this.rtbXsl.Text);

        this.viewmodel.ValidateXsl(this.rtbXsl.Text,validationSuccessful =>
            {
                this.btnSave.Enabled = validationSuccessful;

                this.rtbExampleOutputFileContents.SetFormateText(this.viewmodel.ExampleFileOutputContents);
            });
    }

    private void OnSaveClick(object sender,System.EventArgs e) => this.viewmodel.Save(this.rtbXsl.Text);

    private void OnNotify(NotificationEventArgs obj)
    {
        switch (obj.NotificationType)
        {
            case NotificationType.Info:
                MessageBox.Show(obj.Message,"information",MessageBoxButtons.OK,MessageBoxIcon.information);

                if (obj.Exit)
                {
                    this.Close();
                }

                break;
            case NotificationType.Warning:
                MessageBox.Show(obj.Message,"Warning",MessageBoxIcon.Warning);
                break;
            case NotificationType.Error:
                MessageBox.Show(obj.Message,"Error",MessageBoxIcon.Error);

                if (obj.Exit)
                {
                    this.Close();
                }

                break;
        }
    }

    private void ModifyTransformerContentsView_FormClosing(object sender,FormClosingEventArgs e)
        => this.viewmodel.DeleteTemporaryModifiedTransformerFile();

    private void OnButtonCheatSheetSaveClick(object sender,System.EventArgs e) =>
        this.viewmodel.SaveCheatSheet(rtbCheatSheet.Text);

    private void ModifyTransformerContentsView_Load(object sender,System.EventArgs e)
    {

    }
}

如有任何帮助,我们将不胜感激。

解决方法

TagPage Control 类派生自 Panel 类。
这种类型的控件是不可选择的(ControlStyles.Selectable 在其构造函数中设置为 false),因此它不会获得焦点并且无法通过鼠标单击来选择。

您可以通过不同方式覆盖此行为。三个简单的方法:

  1. 构建一个从 TabPage 派生的自定义控件,然后:

    • 在其构造函数中,调用 SetStyle()

      SetStyle(ControlStyles.Selectable | 
               ControlStyles.UserMouse | 
               ControlStyles.StandardClick,true);  
      
    • 创建此自定义控件的实例并将其添加到 TabControl 的 TabPages

  2. 构建从 Panel 派生的自定义控件:

    • 在其构造函数中设置相同的 ControlStyles
    • 构建控件,在工具箱中找到它并将其放到 TabPage 中,然后设置 Dock = DockStyle.FillAutoScroll = true(或直接在自定义控件类中执行此操作)。
    • 将所有子控件添加到此面板。
  3. 使用反射,获取 TabPage 的非公开 SetStyle 方法并使用 MethodInfo.Invoke() 更改值。例如:

     using System.Reflection;
    
     var flags = BindingFlags.NonPublic | BindingFlags.Instance;
     var method = tabPage1.GetType().GetMethod("SetStyle",flags);
     var newStyles = ControlStyles.Selectable | ControlStyles.UserMouse;
     method.Invoke(tabPage1,new object[] { newStyles,true });
    

    您可以对循环中的所有 TabPage 执行此操作。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?