问题描述
使用 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
),因此它不会获得焦点并且无法通过鼠标单击来选择。
您可以通过不同方式覆盖此行为。三个简单的方法:
-
构建一个从 TabPage 派生的自定义控件,然后:
-
在其构造函数中,调用 SetStyle():
SetStyle(ControlStyles.Selectable | ControlStyles.UserMouse | ControlStyles.StandardClick,true);
-
创建此自定义控件的实例并将其添加到 TabControl 的 TabPages
-
-
构建从 Panel 派生的自定义控件:
- 在其构造函数中设置相同的
ControlStyles
- 构建控件,在工具箱中找到它并将其放到 TabPage 中,然后设置
Dock = DockStyle.Fill
和AutoScroll = true
(或直接在自定义控件类中执行此操作)。 - 将所有子控件添加到此面板。
- 在其构造函数中设置相同的
-
使用反射,获取 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 执行此操作。