我制作了一个自定义按钮来输入按键:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class KeyInputButton
Inherits System.Windows.Forms.Button
'UserControl1 overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
' Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
End Sub
End Class
Public Class KeyInputButton
Public Event KeyCombinationChanged(ByVal sender As System.Object, ByVal kc As TestControls.KeyCombination)
Private _KeyCombination As TestControls.KeyCombination = Nothing
Public Property KeyCombination() As TestControls.KeyCombination
Get
Return _KeyCombination
End Get
Set(ByVal kc As TestControls.KeyCombination)
_KeyCombination = kc
Text = _KeyCombination.toString
End Set
End Property
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub Me_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
KeyCombination = New TestControls.KeyCombination(e.Control, e.Alt, e.Shift, e.KeyValue)
RaiseEvent KeyCombinationChanged(Me, KeyCombination)
End Sub
End Class
当我在表单中放入KeyInputButton并启动调试器时,我得到以下异常(我在VS2005和VS2010中得到相同的异常):
"System.InvalidOperationException was unhandled
Message="Er is een fout opgetreden bij het maken van het formulier. Zie ExceptionInnerException voor details. De fout is: De objectverwijzing is niet op een exemplaar van een object ingesteld."
Source="WindowsApplication1"
StackTrace:
bij WindowsApplication1.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:regel 190
bij WindowsApplication1.My.MyProject.MyForms.get_Form1()
bij WindowsApplication1.My.MyApplication.OnCreateMainForm() in C:\Documents and Settings\Tetra\Mijn documenten\Visual Studio 2005\Projects\TestControls\WindowsApplication1\My Project\Application.Designer.vb:regel 35
bij Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
bij Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
bij Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
bij WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:regel 81
bij System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
bij System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
bij Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
bij System.Threading.ThreadHelper.ThreadStart_Context(Object state)
bij System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
bij System.Threading.ThreadHelper.ThreadStart()
我不知道如何解决这个问题,我尝试重建一切。希望有人可以帮助我。
编辑: KeyCombination类:
Public Class KeyCombination
Public Control As Boolean
Public Alt As Boolean
Public Shift As Boolean
Public Value As Integer
Private Const MOD_ALT = 1
Private Const MOD_CONTROL = 2
Private Const MOD_SHIFT = 4
Public ReadOnly Property modifiers() As Integer
Get
If Not control And Not alt And shift Then
Return MOD_SHIFT
End If
If Not control And alt And Not shift Then
Return MOD_ALT
End If
If Not control And alt And shift Then
Return MOD_ALT Or MOD_SHIFT
End If
If control And Not alt And Not shift Then
Return MOD_CONTROL
End If
If control And Not alt And shift Then
Return MOD_CONTROL Or MOD_SHIFT
End If
If control And alt And Not shift Then
Return MOD_CONTROL Or MOD_ALT
End If
If control And alt And shift Then
Return MOD_CONTROL Or MOD_ALT Or MOD_SHIFT
End If
End Get
End Property
Public Sub New(ByVal c As Boolean, ByVal a As Boolean, ByVal s As Boolean, ByVal v As Integer)
Shift = s
Control = c
Alt = a
Value = v
End Sub
Public Overrides Function toString() As String
Dim Ret = ""
If control Then
ret += "Control + "
End If
If alt Then
ret += "Alt + "
End If
If shift Then
ret += "Shift + "
End If
Return Ret & System.Windows.Forms.Keys.GetName(GetType(System.Windows.Forms.Keys), Value)
End Function
End Class
答案 0 :(得分:1)
我认为问题可能出在您的TestControls.KeyCombination
对象中。
我可以将整个代码粘贴到Windows窗体应用程序中,并且可以在窗体上放置一个没有错误的控件 - 假设我创建了一个名为TestControls.KeyCombination
的虚拟类....这使我得出结论错误在某处!
编辑: 好的,那个(KeyCombination类)也可以工作(它显示我正在按的键的名称)...我已经在这样的表单上启动了控件:
Dim testControl As New KeyInputButton()
Me.Controls.Add(testControl)
你能试试吗?
在某个地方某些东西一定是腐败的,也许你以任何方式将它添加到表格中?
编辑: 该错误是由于在初始化_KeyCombination类之前调用了toString方法,替换该行:
Text = _KeyCombination.toString
带
If Not IsNothing(_KeyCombination) Then
Text = _KeyCombination.toString
End If
答案 1 :(得分:0)
某些对象似乎是空的。您是否尝试过单步执行该应用程序?
答案 2 :(得分:0)
毕竟,错误发生在KeyInputButton类中。 Form尝试将KeyInputButton的KeyCombination属性设置为Nothing。设置此属性后,Text将设置为KeyCombination.toString。我通过在设置文本之前检查KeyCombination是否为Nothing来修复它:
Public Property KeyCombination() As TestControls.KeyCombination
Get
Return _KeyCombination
End Get
Set(ByVal kc As TestControls.KeyCombination)
_KeyCombination = kc
If _KeyCombination IsNot Nothing Then
Text = _KeyCombination.ToString
End If
End Set
End Property
我很高兴终于解决了,但我真的不明白为什么Exception没有对KeyInputButton类说什么。