Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a RectangleShape, That I named Flng1, on a usercontrol and I need the MouseDown Event to work for the usercontrol to the form I place it on. It seems I can make the Click event work with this OnClick(EventArgs.Empty) but when I use this OnMouseDown(EventArgs.Empty) or OnMouseDown(MouseEventArgs.Empty) in the Flng1.Mousedown area it just gives me an error:

An unhandled exception of type 'System.InvalidCastException' occurred in Ultra30007810.exe

Additional information: Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.MouseEventArgs'.

Does anyone know why it doesn't work and why the Click Event works?

What I have tried:

Here is what I have tried.

Private Sub Flng1_MouseUp(sender As Object, e As MouseEventArgs) Handles Flng1.MouseUp

OnMouseUp(EventArgs.Empty)

End Sub

Or
Private Sub Flng1_MouseDown(sender As Object, e As MouseEventArgs) Handles Flng1.MouseDown

OnMouseDown(EventArgs.Empty)
End Sub

Then in the form
Private Sub Nozzle1_MouseDown1(sender As Object, e As MouseEventArgs) Handles Nozzle1.MouseDown
MsgBox("MouseDown1")
End Sub
Posted
Updated 25-Mar-23 23:30pm
v2

If you want to pass the event from the UserControl to a parent container, like a Form, then you need to:

1. expose either a Method to a delegate (a method or function to call from within the UserControl) or

2. add a public Event property to the UserControl that the form can Listen to and raise it from within the UserControl when you want to notify the parent container. This is called event bubbling. ref: Declaring and Raising Events - Visual Basic | Microsoft Learn[^]

As you are a beginner, I would suggest option 2.

1. USerControl Designer
VB.NET
Partial Class UserControl1
    Inherits System.Windows.Forms.UserControl

    'UserControl overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    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()
        PictureBox1 = New PictureBox()
        CType(PictureBox1, ComponentModel.ISupportInitialize).BeginInit()
        SuspendLayout()
        ' 
        ' PictureBox1
        ' 
        PictureBox1.BackColor = Color.Red
        PictureBox1.Location = New Point(205, 104)
        PictureBox1.Name = "PictureBox1"
        PictureBox1.Size = New Size(246, 179)
        PictureBox1.TabIndex = 0
        PictureBox1.TabStop = False
        ' 
        ' UserControl1
        ' 
        AutoScaleDimensions = New SizeF(10F, 25F)
        AutoScaleMode = AutoScaleMode.Font
        Controls.Add(PictureBox1)
        Name = "UserControl1"
        Size = New Size(684, 432)
        CType(PictureBox1, ComponentModel.ISupportInitialize).EndInit()
        ResumeLayout(False)
    End Sub

    Friend WithEvents PictureBox1 As PictureBox
End Class


Adding the custom event to the UserControl:
VB.NET
Public Class UserControl1
    Private Sub PictureBox1_MouseDown(
        sender As Object,
        e As MouseEventArgs)
        Handles PictureBox1.MouseDown

        RaiseEvent ShapeMouseDown(sender, e)

    End Sub

    Public Event ShapeMouseDown(sender As Object, e As MouseEventArgs)

End Class

Now when the Shape (PictureBox) event is raised, we bubble it up to the Form (Parent Container)

Now to handle the event. When you compile the project, then add the USerControl, you will see the custom event named ShapeMouseDown. We can double-tap and create an event handler in the Form. Here is the form designer snippet wiring up the event:
VB
Private Sub InitializeComponent()
    UserControl11 = New UserControl1()
    SuspendLayout()
    ' 
    ' UserControl11
    ' 
    UserControl11.Location = New Point(12, 2)
    UserControl11.Name = "UserControl11"
    UserControl11.Size = New Size(776, 436)
    UserControl11.TabIndex = 0
    ' 
    ' Form1
    ' 
    AutoScaleDimensions = New SizeF(10F, 25F)
    AutoScaleMode = AutoScaleMode.Font
    ClientSize = New Size(800, 450)
    Controls.Add(UserControl11)
    Name = "Form1"
    Text = "Form1"
    ResumeLayout(False)
End Sub

Friend WithEvents UserControl11 As UserControl1

And now the Form code-behind:
VB.NET
Public Class Form1
    Private Sub UserControl11_ShapeMouseDown(
        sender As Object,
        e As MouseEventArgs)
        Handles UserControl11.ShapeMouseDown

        MsgBox("Shape Mouse Down Event")

    End Sub
End Class

Now when you Mouse-Down on the Shape (PictureBox), the ShapeMouseDown event is raised by the USerControl, captured by the Form, and a Message Box is displayed.

Hope this is clear enough.

UPDATE

I am not sure why you are having so much difficulty. Here are the complete file dumps:

1. Form1.Designer.vb
VB.NET
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()>
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    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()
        UserControl11 = New UserControl1()
        SuspendLayout()
        ' 
        ' UserControl11
        ' 
        UserControl11.Location = New Point(12, 2)
        UserControl11.Name = "UserControl11"
        UserControl11.Size = New Size(776, 436)
        UserControl11.TabIndex = 0
        ' 
        ' Form1
        ' 
        AutoScaleDimensions = New SizeF(10.0F, 25.0F)
        AutoScaleMode = AutoScaleMode.Font
        ClientSize = New Size(800, 450)
        Controls.Add(UserControl11)
        Name = "Form1"
        Text = "Form1"
        ResumeLayout(False)
    End Sub

    Friend WithEvents UserControl11 As UserControl1
End Class

2. Form1.vb
VB.NET
Public Class Form1
    Private Sub UserControl11_ShapeMouseDown(sender As Object, e As MouseEventArgs) Handles UserControl11.ShapeMouseDown
        MsgBox("Shape Mouse Down Event")
    End Sub
End Class

3. UserControl1.Designer.vb
VB.NET
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class UserControl1
    Inherits System.Windows.Forms.UserControl

    'UserControl overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    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()
        PictureBox1 = New PictureBox()
        CType(PictureBox1, ComponentModel.ISupportInitialize).BeginInit()
        SuspendLayout()
        ' 
        ' PictureBox1
        ' 
        PictureBox1.BackColor = Color.Red
        PictureBox1.Location = New Point(205, 104)
        PictureBox1.Name = "PictureBox1"
        PictureBox1.Size = New Size(246, 179)
        PictureBox1.TabIndex = 0
        PictureBox1.TabStop = False
        ' 
        ' UserControl1
        ' 
        AutoScaleDimensions = New SizeF(10F, 25F)
        AutoScaleMode = AutoScaleMode.Font
        Controls.Add(PictureBox1)
        Name = "UserControl1"
        Size = New Size(684, 432)
        CType(PictureBox1, ComponentModel.ISupportInitialize).EndInit()
        ResumeLayout(False)
    End Sub

    Friend WithEvents PictureBox1 As PictureBox
End Class

4. UserControl1.vb
VB.NET
Public Class UserControl1
    Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
        RaiseEvent ShapeMouseDown(sender, e)
    End Sub

    Public Event ShapeMouseDown(sender As Object, e As MouseEventArgs)

End Class

That is all of the code. I have done no formatting and just dumped the code. Create a new project and add a UseerControl1. then copy and paste the code, then run.
 
Share this answer
 
v3
An unhandled exception of type 'System.InvalidCastException' occurred in Ultra30007810.exe

You have an error in your code, but since we cannot see the source we cannot help, beyond repeating what the error message states.
 
Share this answer
 
This is the code
I have a RectangleShape on the Usercontrol called Flng1. This works when I use OnClick(EventArgs.Empty) but I need to have a Button2 action happen so the click won't work. Hope this helps.

Private Sub Flng1_MouseUp(sender As Object, e As MouseEventArgs) Handles Flng1.MouseUp

        OnMouseUp(EventArgs.Empty)

    End Sub

Or
Private Sub Flng1_MouseDown(sender As Object, e As MouseEventArgs) Handles Flng1.MouseDown

        OnMouseDown(EventArgs.Empty)
    End Sub

Then in the form
Private Sub Nozzle1_MouseDown1(sender As Object, e As MouseEventArgs) Handles Nozzle1.MouseDown
        MsgBox("MouseDown1")
    End Sub
 
Share this answer
 
Comments
Dave Kreskowiak 26-Mar-23 0:16am    
You posted this as a solution to your own question. Don't do that.

Hover the mouse over your question above and you'll see the "Improve question" link. Click that and you can modify the question and add the code there.
Mark Allan Wright 26-Mar-23 4:48am    
I found it but where I put what I had I thought was the place to reply to. Oh Well!

Graeme_Grant 26-Mar-23 6:05am    
And I have answered your question.
Mark Allan Wright 26-Mar-23 6:10am    
And I thank you Graeme, I am still trying to figure it out though. I think I'll start a new project and use a picture box instead of a Rectangle and see what happens. Oh Please take me back to VB6! LOL!
Graeme_Grant 26-Mar-23 7:00am    
I started way back on C64 Basic, then GW Basic, Quick Basic, PDS 7.1, vb6 (and earlier), VB.Net 1.0, through to VB.Net today, and C# (core). Would I go back to VB6? Go back 20+ years? Hell no. Also, my preference is WPF over WinForms, hands down.

The solution given is a working solution before I posted it here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900