views:

96

answers:

3

I made a custom button to input keypresses:

<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

When I put a KeyInputButton in a Form and start the debugger, I get the following exception (I get the same exception in VS2005 and 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()

I have no idea how to fix this, I tried rebuilding everything. Hopefully someone can help me with this.

EDIT: KeyCombination 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
A: 

Some object seems to be Null. Have you tried stepping through the application?

rdkleine
The Exception occurs before even executing any of my code
Arjan
You've got a build error? Else step into using F11
rdkleine
Thanks a lot for your help
Arjan
Did that help? Geen dank ;)
rdkleine
Yes, I used F11 and found out a toString method of Nothing was called
Arjan
Would you accept this answer as the answer
rdkleine
Sure, I just did
Arjan
I'm glad you got it fixed, next time I know just to say "look yourself" and I'll get the answer!
wheelibin
I am new to VB.NET and Visual Studio, so I didn't know how to debug very well. Also, the Exception message didn't help me.
Arjan
+1  A: 

I think the problem might be in your TestControls.KeyCombination object.

I can paste the whole of your code into a windows form app and can put a control on the form with no error - providing I make a dummy class called TestControls.KeyCombination....this leads me to conclude the error is in there somewhere!

Edit: Ok, that (the KeyCombination class) also works (it's displaying the name of the key I am pressing)...I have initiated the control on the form like this:

Dim testControl As New KeyInputButton()

Me.Controls.Add(testControl)

Can you try that?
Something must be corrupt somewhere along the way, maybe in whatever way you have added it to the form?

Edit: The error is due to your toString method being called before the _KeyCombination class has been initialised, replace the line:

Text = _KeyCombination.toString

with

If Not IsNothing(_KeyCombination) Then
    Text = _KeyCombination.toString
End If
wheelibin
I added the KeyCombination class, could you have a look at it?
Arjan
The code you use works indeed. I added the KeyInputButton from my toolbox. This is the generated code:Me.KeyInputButton1 = New TestControls.KeyInputButton Me.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 = TrueMe.Controls.Add(Me.KeyInputButton1)
Arjan
Do you have any idea how to make it work when adding the KeyInputButton from the toolbox?
Arjan
Thanks a lot for your help
Arjan
As others have suggested have you tried stepping through the application? If so on which line does the error occur?
wheelibin
A: 

After all, the error was in the KeyInputButton class. The Form tried to set the KeyCombination property of the KeyInputButton to Nothing. When this property is set, the Text is set to KeyCombination.toString. I fixed it by checking if KeyCombination is not Nothing before setting the text:

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

I'm glad it is finally solved, but I really don't get why the Exception didn't say anything about the KeyInputButton class.

Arjan
ha, I just added this too ;)
wheelibin