[英]Allow Mouse Wheel scroll on a TabPage Control
使用 XML 源填充 TabControl 的 TabPage。 將 XML 內容加載到 TabPage 后,TabPage 的兩側會出現兩個滾動條,以允許用戶滾動。
但是,用戶不能使用鼠標滾輪滾動。 我已經檢查了 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", MessageBoxButtons.OK, MessageBoxIcon.Warning);
break;
case NotificationType.Error:
MessageBox.Show(obj.Message, "Error", MessageBoxButtons.OK, 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 控件 class 源自面板 class。
這種類型的控件是不可選擇的( ControlStyles.Selectable
在其構造函數中設置為false
),因此它不會獲得焦點並且無法通過鼠標單擊來選擇。
您可以通過不同的方式覆蓋此行為。 三種簡單的方法:
構建從 TabPage 派生的自定義控件,然后:
在其構造函數中,調用SetStyle() :
SetStyle(ControlStyles.Selectable | ControlStyles.UserMouse | ControlStyles.StandardClick, true);
創建此自定義控件的實例並將其添加到 TabControl 的 TabPages
構建從 Panel 派生的自定義控件:
ControlStyles
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 });
您可以在循環中對所有 TabPages 執行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.