我創建的Winforms自動完成組合框控制從組合框繼承以下方法的指示:試圖讀取或寫入受保護的內存。這通常是其他存儲器已損壞
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
MyBase.OnKeyPress(e)
Dim text As String = Me.Text
If Char.IsLetter(e.KeyChar) Then
text = text + e.KeyChar
End If
If (text.Length = AutoCompleteSearchChars And Char.IsLetter(e.KeyChar)) Then
SetAutoCompleteMethod.Invoke(text)
Me.AutoCompleteSource = AutoCompleteSource.ListItems
Me.Text = text
Me.SelectionStart = text.Length
Me.SelectionLength = 0
If Me.Items.Count > 0 Then
Me.SelectedIndex = 0
End If
e.Handled = True
End If
End Sub
的「SetAutoCompleteMethod」是指向該填充 的方法的代表基於使用組合框中的當前文本作爲通配符搜索的前綴的組合框項目。這提供了與ajax自動完成組合框類似的功能。
這一切在第一次被調用時都可以正常工作,但第二次被調用時我得到了「嘗試讀取或寫入受保護的內存,這通常表示其他內存已損壞」錯誤。在所有更新組合框的代碼完成後出現錯誤。我得到以下堆棧跟蹤:
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG & MSG)在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms。 UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(的Int32 dwComponentID,的Int32原因,的Int32 pvLoopData)在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(的Int32 原因,ApplicationContext的上下文)
在 System.Windows.Forms.Application .ThreadContext.RunMessageLoop(的Int32 原因,ApplicationContext的上下文)
在 System.Windows.Forms.Application.Run在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() (ApplicationContext的 上下文)在 Microsoft.Vi sualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(字符串[] COMMANDLINE)在 ApplicationShell.My.MyApplication.Main(字符串[] Args)來在 17d14f5c-a337- 4978-8281-53493378c1071.vb:線 System.AppDomain._nExecuteAssembly(大會 組件,字串[] args)在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,證據 assemblySecurity,字串[] args)在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(對象 狀態)在 System.Threading.ExecutionContext.Run(的ExecutionContext 的ExecutionContext,ContextCallback 回調,對象狀態)在 System.Threading.ThreadHelper.ThreadStart()
任何人都可以指出我正確的方向來解決這個問題嗎?
我相信這與數據源有關。在加載新項目之前,我將組合框的數據源設置爲「無」,並且發生錯誤的頻率較低? – ptutt 2010-05-08 06:49:24