Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使我的VB.NET自定义按钮工作?_.net_Vb.net_Winforms_User Controls - Fatal编程技术网

如何使我的VB.NET自定义按钮工作?

如何使我的VB.NET自定义按钮工作?,.net,vb.net,winforms,user-controls,.net,Vb.net,Winforms,User Controls,我制作了一个自定义按钮来输入按键: <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ Partial Class KeyInputButton Inherits System.Windows.Forms.Button 'UserControl1 overrides dispose to clean up the component list. <System.Di

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

<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
我不知道如何解决这个问题,我试着重建一切。希望有人能帮我

编辑: 键组合类:

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

某些对象似乎为空。您是否尝试过单步执行应用程序?

我认为问题可能出在您的
TestControls.KeyCombination
对象中

我可以将您的全部代码粘贴到windows窗体应用程序中,并且可以在窗体上放置一个控件而不会出现错误-前提是我创建了一个名为
TestControls.KeyCombination
…的虚拟类。这让我得出结论,错误就在某个地方

编辑: 好的,(KeyCombination类)也可以工作(它显示我正在按的键的名称)…我在窗体上启动了如下控件:

Dim testControl作为新的KeyInputButton()

Me.Controls.Add(testControl)

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

编辑: 错误是由于在初始化_KeyCombination类之前调用了toString方法,请替换该行:

Text = _KeyCombination.toString


毕竟,错误发生在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类。

我添加了KeyCombination类,你能看一下吗?你使用的代码确实有效。我从工具箱中添加了KeyInputButton。这是生成的代码:Me.KeyInputButton1=New TestControls.KeyInputButton1.KeyInputButton1.KeyCombination=Nothing Me.KeyInputButton1.Location=New System.Drawing.Point(153,99)Me.KeyInputButton1.Name=“KeyInputButton1”Me.KeyInputButton1.Size=New System.Drawing.Size(75,23)Me.KeyInputButton1.TabIndex=1 Me.KeyInputButton1.Text=“KeyInputButton1”Me.KeyInputButton1.UseVisualStyleBackColor=True Me.Controls.Add(Me.KeyInputButton1)当从工具箱中添加KeyInputButton时,您知道如何使其工作吗?正如其他人所建议的,您是否尝试过单步执行应用程序?如果是,错误发生在哪一行?是的,我使用了F11,发现了一个名为Nothing的toString方法。我很高兴你修复了它,下次我只知道说“看看你自己”,我会得到答案的!我是VB.NET和Visual Studio的新手,所以我不知道如何很好地调试。另外,异常消息对我也没有帮助。
If Not IsNothing(_KeyCombination) Then
    Text = _KeyCombination.toString
End If
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