代码之家  ›  专栏  ›  技术社区  ›  Arjan

我怎么做我的工作VB.NET 自定义按钮工作?

  •  0
  • Arjan  · 技术社区  · 15 年前

    我做了一个自定义按钮来输入按键:

    <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()
    

    我不知道怎么解决这个问题,我试着重建一切。希望有人能帮我。

    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
    
    3 回复  |  直到 15 年前
        1
  •  0
  •   Ralf de Kleine    15 年前

    某些对象似乎为空。你试过逐级检查申请表吗?

        2
  •  1
  •   wheelibin    15 年前

    我想问题可能出在你身上 TestControls.KeyCombination 对象。

    我可以将您的全部代码粘贴到windows窗体应用程序中,并且可以在窗体上放置控件而不会出错—前提是我创建了一个名为 TestControls.KeyCombination测试控制键组合

    编辑:

    Dim testControl As New KeyInputButton()

    Me.Controls.Add(testControl)

    你能试试吗?
    某个东西一定是在某个地方被破坏了,也许是以任何你把它添加到表单中的方式?

    编辑: 错误是由于在初始化\u KeyCombination类之前调用了toString方法,请替换以下行:

    Text = _KeyCombination.toString
    

    If Not IsNothing(_KeyCombination) Then
        Text = _KeyCombination.toString
    End If
    
        3
  •  0
  •   Arjan    15 年前

    毕竟,错误在KeyInputButton类中。表单试图将KeyInputButton的KeyCombination属性设置为Nothing。设置此属性后,文本将设置为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
    

    我很高兴它终于被解决了,但是我真的不明白为什么这个异常没有提到KeyInputButton类。

    推荐文章