简体   繁体   中英

VB.NET: Unable to cast object of type 'System.Windows.Forms.MouseEventArgs' to type 'System.Windows.Forms.KeyPressEventArgs'.'

I'm trying to create a program that has a textbox and I need to monitor the user input to allow only characters, whitespaces and control operations. This is all the code I have:


Private Sub Button1_Click(sender As Object, e As KeyPressEventArgs) Handles Button1.Click
        TextBox1.ReadOnly = False
        If TimesPressed = 0 Then
            Label2.Visible = False
            TextBox1.Text = “Welcome to VB.Net, ” & TextBox1.Text
            Button1.Text = "Repeat" 
            TextBox1.ReadOnly = True 
            TextBox1.BackColor = SystemColors.Window 
            TimesPressed = 1
        ElseIf TimesPressed = 1 Then
            Label2.Visible = True
            TextBox1.Clear() 
            Button1.Text = "Run" 
            TimesPressed = 0
        Else
            TimesPressed = 0
        End If
End Sub

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    If Char.IsLetter(e.KeyChar) = False And Char.IsWhiteSpace(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
        e.Handled = True
    End If
End Sub

Then I have a button that submits the input but when I try to click on it I am presented with an error box that says:

Unable to cast object of type 'System.Windows.Forms.MouseEventArgs' to type 'System.Windows.Forms.KeyPressEventArgs'.

I don't know what to do and I can't find answers that I understand anywhere. How can I resolve this problem?

I'm not really sure why Spevacus deleted his answer; it was correct. A button click handler is descended from the Control.Click event, that takes an object of type EventArgs as its second parameter. When you click a button an object of type MouseEventArgs is created, and this descends from EventArgs as a child in the inheritance hierarchy, so it can be passed to the click handler (because a child type can always automatically be cast to any one of its parent types)

KeyPressEventArgs is also descended from EventArgs but it is a sibling of MouseEventArgs , not an inherited relation. As such when you click the button and a MouseEventArgs is created it cannot be passed to a Sub/Function that is declared to take a KeyPressEventArgs because there is no automatic conversion between the two. Mouse and KeyPress share the same parent but are different things and cannot be converted.

Long story short, change your button click handler so it takes an EventArgs instead of a KeyPressEventArgs

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

As an aside, please always rename your controls after you add them to the form. There are few things worse for someone else who is reading your code and trying to help, than having to work out and remember which of 30 button X or textbox Y does what / is what - renaming them to "_inputTextBox" or "_saveButton" takes about 2 seconds and helps you out immeasurably too, as your program grows in complexity.

The arguments you have for Button1_Clicked are slightly incorrect.

Your e argument should be an EventArgs variable.

For easy copy paste, here's how its declaration should look now:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

As @TechGnome suggests, you should try double clicking the button in your form designer to see what the auto-generated code looks like and edit it from there. It does most of the heavy lifting for you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM