hi
i guess the title pretty much states the question but i will give the senario to be more clear.

i have a mousemove() event which i add in my program programmatically and i need a way for raising that event (again) programmatically too.

raiseEvent control_mousemove() doesn't work as it says that "'control_MouseDown' is not an event of 'main'."

I read in the msdn site that You cannot use RaiseEvent to raise events that are not explicitly declared in the module.

So can i actually raise the event programmatically if the event was added programatically and how? if not any ideas on how to work around this?

thanks in advance

Does using (sender, e) help?

Public Class Form1

    Private Sub Button2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Button2.BackColor = Color.Orange
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button2_MouseMove(sender, e)
    End Sub
End Class

this doesn't raise the event. it just calls it like a simple sub.
i did manage to raise the event but for some reason the code gets executed but nothing happens on the interface. so i drag the control (which its mousedown event is called )but it doesn't get dragged.

Posting code always helps, even if having to make a sample project just for such.

through this class i can raise the events that were added programmatically

Class MouseEvent

Public Event btnMove_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
Public Event btnMove_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)

Public Sub GenerateEventDown(ByVal sender As Object, ByVal e As MouseEventArgs)

RaiseEvent btnMove_MouseDown(sender, e)

End Sub

Public Sub GenerateEventMove(ByVal sender As Object, ByVal e As MouseEventArgs)

RaiseEvent btnMove_MouseMove(sender, e)

End Sub

End Class

Dim WithEvents RaiseEventProgrammatically As New MouseEvent()

THis where i use the class above to call the events
Private Sub btnPin_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles btnRedPin.MouseDown, btnGreenPin.MouseDown, btnBluePin.MouseDown

Me.Cursor = Cursors.Hand

''''
code missing
''
AddHandler RaiseEventProgrammatically.btnMove_MouseDown, AddressOf btnMove_MouseDown


RaiseEventProgrammatically.GenerateEventDown(objPin, e)

End Sub

and these are the two events that i add programmatically

Public Sub btnMove_MouseDown( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs)

' Get sender
Dim objSender As DragButton
objSender = sender

'''''
code missing


'' '
' Re-Draw object
objSender.Invalidate()

AddHandler RaiseEventProgrammatically.btnMove_MouseMove, AddressOf btnMove_MouseMove
RaiseEventProgrammatically.GenerateEventMove(objSender, e)

End If

End Sub

Private Sub btnMove_MouseMove( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs)

' Get sender
Dim objSender As DragButton
objSender = sender

If Not objSender.Dragging Then Exit Sub

' Move control to new position
Dim MPosition As New Point()
MPosition = Me.PointToClient(MousePosition)
MPosition.Offset(objSender.PosX, objSender.PosY)

' Ensure control cannot leave container
objSender.Location = MPosition

RemoveHandler RaiseEventProgrammatically.btnMove_MouseDown, AddressOf btnMove_MouseDown

End Sub

with these code the mousemove does get raised (breakpoint gets hit)but the movement doesn't appear on the screen

any suggestions?
thanks anyway

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.