VB.NET中MetroGrid鼠标滚动触发ArgumentOutOfRangeException问题求助
MetroGrid滚动触发ArgumentOutOfRangeException异常问题
场景与代码
我有3个文本框,通过数组将其中的数据插入到DataGridView(MetroGrid9)中,代码如下:
Dim row As String() = New String() {MetroTextBox12.Text, MetroTextBox11.Text, MetroTextBox13.Text} MetroGrid9.Rows.Add(row) MetroTextBox12.Text = "" MetroTextBox11.Text = "" MetroTextBox13.Text = ""
问题描述
使用鼠标滚轮滚动该DataGridView时,会触发ArgumentOutOfRangeException异常,堆栈跟踪信息如下:
StackTrace: at System.Windows.Forms.DataGridView.set_FirstDisplayedScrollingRowIndex(Int32 value) at MetroFramework.Controls.MetroGrid.OnMouseWheel(MouseEventArgs e) at System.Windows.Forms.Control.WmMouseWheel(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Form.ShowDialog(IWin32Window owner) at ElectronicClientLoanInformationAndPortalSystem.dbSettingsAdder.dbSettingsAdder_Load(Object sender, EventArgs e) in E:\2020 Program\ECLIPS SOLUTION\ECLIPS\ElectronicClientLoanInformationAndPortalSystem\Common Forms\dbSettingsAdder.vb:line 78 at System.EventHandler.Invoke(Object sender, EventArgs e) at System.Windows.Forms.Form.OnLoad(EventArgs e) at MetroFramework.Forms.MetroForm.OnLoad(EventArgs e) at System.Windows.Forms.Form.OnCreateControl() at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) at System.Windows.Forms.Control.CreateControl() at System.Windows.Forms.Control.WmShowWindow(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.Form.WmShowWindow(Message& m) at MetroFramework.Forms.MetroForm.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
中文翻译后的堆栈跟踪
堆栈跟踪: 在 System.Windows.Forms.DataGridView.set_FirstDisplayedScrollingRowIndex(Int32 value) 在 MetroFramework.Controls.MetroGrid.OnMouseWheel(MouseEventArgs e) 在 System.Windows.Forms.Control.WmMouseWheel(Message& m) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Form.ShowDialog(IWin32Window owner) 在 ElectronicClientLoanInformationAndPortalSystem.dbSettingsAdder.dbSettingsAdder_Load(Object sender, EventArgs e) 位置 E:\2020 Program\ECLIPS SOLUTION\ECLIPS\ElectronicClientLoanInformationAndPortalSystem\Common Forms\dbSettingsAdder.vb:第78行 在 System.EventHandler.Invoke(Object sender, EventArgs e) 在 System.Windows.Forms.Form.OnLoad(EventArgs e) 在 MetroFramework.Forms.MetroForm.OnLoad(EventArgs e) 在 System.Windows.Forms.Form.OnCreateControl() 在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) 在 System.Windows.Forms.Control.CreateControl() 在 System.Windows.Forms.Control.WmShowWindow(Message& m) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.Form.WmShowWindow(Message& m) 在 MetroFramework.Forms.MetroForm.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
可能的原因与解决思路
- MetroGrid控件版本问题:异常是MetroGrid处理鼠标滚轮事件时,向
DataGridView.set_FirstDisplayedScrollingRowIndex传入了超出有效范围的索引值导致的,大概率是MetroFramework控件库的已知Bug,建议升级到最新稳定版本。 - 添加行后强制刷新:检查添加行后网格是否未及时更新行计数,尝试在添加行后调用刷新方法:
MetroGrid9.Rows.Add(row) MetroGrid9.Refresh() - 自定义MetroGrid处理滚轮事件:如果升级控件库无效,可以自定义控件重写滚轮事件,添加索引范围校验:
之后将窗体中的MetroGrid9替换为这个自定义控件即可。Public Class CustomMetroGrid Inherits MetroFramework.Controls.MetroGrid Protected Overrides Sub OnMouseWheel(e As MouseEventArgs) Try If Me.Rows.Count = 0 Then MyBase.OnMouseWheel(e) Return End If Dim targetIndex = Me.FirstDisplayedScrollingRowIndex + (If(e.Delta > 0, -1, 1)) targetIndex = Math.Max(0, Math.Min(targetIndex, Me.Rows.Count - 1)) Me.FirstDisplayedScrollingRowIndex = targetIndex Catch ex As ArgumentOutOfRangeException Debug.WriteLine("滚动异常: " & ex.Message) End Try End Sub End Class
内容的提问来源于stack exchange,提问作者JeffyO
相关产品推荐
相关产品推荐

