Tim8w

I have a CustomButtonControl subclassed from Control. I need to be able to translate a KeyPress event into an OnMouseDown event, i.e. I want to simulate a ButtonClick when a key is pressed. Since this control is not a button, I don't have the button's functions available. Any ideas


Re: Visual Basic General CustomButtonControl - Simulated Mouse Click?

Aw Ali

First, there is no such onMouseDown event in VB .NET so I hope you are talking about MouseDown event.

If your custom control inherits from Control, then it should have all the events of the parent class (Click, MouseDown, KeyPressed, etc). You only need to create a sub routine that handles your desired event. Since you intermixed MouseDown and Click event (two distinct events), I am going to take the click event. Just define a sub that handles the controls click event.

Code Snippet

Private Sub myControl_Click(sender As Object, e As System.EventArgs) handles Me.Click

'Add some code here to hndle click event.

'myControl is assumed to be the name of the control. Change according to your code.

End Sub

Then on keyPressed event, simply call the event that you have defined.

Code Snippet

myControl_Click(New Object, New System.EventArgs)

Hope this helps.






Re: Visual Basic General CustomButtonControl - Simulated Mouse Click?

Tim8w

First, there is an OnMouseDown event in VB.NET. I am overriding it in order to simulate the CustomButton clicking. Here's the code in the CustomButton Class:

Code Snippet

Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)

MyBase.OnMouseDown(e)

Me.Left = Me.Left + 1

Me.Top = Me.Top + 1

End Sub

Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)

MyBase.OnMouseUp(e)

Me.Left = Me.Left - 1

Me.Top = Me.Top - 1

End Sub

This is not where my problem is. I need this code to get triggered when a key is pressed. That is why I want a way to "convert" a KeyPress into a MouseDown on a specified CustomButton. I've tried doing the following in the form that contains the CustomButton:

Code Snippet

Private Sub frmCalculator_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

Dim eMouseEvent As New MouseEventArgs(Windows.Forms.MouseButtons.Left, 1, btnA.Location.X + btnA.Size.Width / 2, btnA.Location.Y + btnA.Size.Height / 2, 0)

Select Case e.KeyChar

Case "A", "a"

btnA_MouseDown(sender, eMouseEvent)

End Select

End Sub

Unfortunately, this did not trigger the OnMouseDown event as I had hoped. It seems that only an actual MouseDown with the mouse will do this. Hence, my orginal question...






Re: Visual Basic General CustomButtonControl - Simulated Mouse Click?

Aw Ali

Hmmmm! onMouseDown event is just left there to support backward compatibility (Or coding upgrade) with VB 6.0. If you hover the mouse over the Me.onMouseDown, you will see that it explains that this event simply fires the MouseClick event (which is the .Net actual event).

btnA_MouseDown will never fire the Mouse event because there is no mouse event handler in your custom control, and thus cannot find one. Remember you are creating a custom control therefore you need your own handlers (Subs that has the keyword 'handles'). Simply put, you need an event handler as a member of the custom control. Then you can call that event hadler from any procedure within the scope. You are on the right track, but just add a handler in your class.






Re: Visual Basic General CustomButtonControl - Simulated Mouse Click?

Tim8w

Ok. I've changed the CustomButton code from OnMouseDown to MouseDown and OnMouseUp to MouseUp. I still have the identical problem in that when I try and simulate the MouseDown, it never calls the CustomnButton MouseDown...






Re: Visual Basic General CustomButtonControl - Simulated Mouse Click?

Aw Ali

The name change does not matter at all. You can have any name for the handler, but it just needs to know that this is the handler for the mouseDown event. It must be in the form of:

<Identifier> Sub <SubName>(sender as Object, e as System.EventArgs) handles Me.MouseDown

That must be in the custom class. The code you want to execute on mouse down, must be in this sub.

If you have tried this and still does not work, then something else is going on. Your control inherits from Control class thus must respond to mouse events as long as there are handlers.






Re: Visual Basic General CustomButtonControl - Simulated Mouse Click?

Tim8w

Aw Ali,

It responds to Mouse Events fine. Remember what I'm trying to do is convert a KeyPress event from the main form into a Mouse Event for a specified CustomButton. I have a custom button that has a number on it. So what I want is when I press that number, the CustomButton's MouseDown and MouseUp routines get called. I have come across a work around using the API, but I can't believe there's not a better way to do it than through the API.






Re: Visual Basic General CustomButtonControl - Simulated Mouse Click?

Aw Ali

In that case, you may need to check if the KeyPreview property of the form is set to true. The form will then respond to key strikes after which you can scan the keys and call appropriate routines.




Re: Visual Basic General CustomButtonControl - Simulated Mouse Click?

Tim8w

Aw Ali,

I have the KeyPreview property set to true in the main form and am getting the KeyPress event in the main form just fine. As I continue to mention, once I get the key and determine which button I want it to push, this is where the problem is. I can't call the CustomButton's OnMouseDown because the routine is Protected.

Ideally, I would want the KeyPress routine from the main form to look something like this:

Code Snippet

Private Sub frmCalculator_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

Select Case e.KeyChar

Case "A", "a"

btnA.Parent.OnMouseDown(New MouseEventArgs(Windows.Forms.MouseButtons.Left, 1, btnA.Location.X + btnA.Size.Width / 2, btnA.Location.Y + btnA.Size.Height / 2, 0)

System.Threading.Thread.Sleep(250)

btnA.Parent.OnMouseUp(New MouseEventArgs(Windows.Forms.MouseButtons.Left, 1, btnA.Location.X + btnA.Size.Width / 2, btnA.Location.Y + btnA.Size.Height / 2, 0)

e.Handled = True

End Select

End Sub






Re: Visual Basic General CustomButtonControl - Simulated Mouse Click?

Tim8w

A guy in another forum came up with this solution and it works good enough for what I needed. The following two subs were created in the CustomButton class:

Code Snippet

Public Sub DoMouseDown(e as MouseEventArgs)
OnMouseDown(e)
End Sub

Public Sub DoMouseUp(e as MouseEventArgs)
OnMouseUp(e)
End Sub

Then in the main form I called it as follows from the KeyPress sub:

Code Snippet


Private Sub frmCalculator_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

Select Case e.KeyChar
Case "A", "a"
btnA.DoMouseDown(New MouseEventArgs(Windows.Forms.MouseButtons.Left, 1, btnA.Location.X + btnA.Size.Width / 2, btnA.Location.Y + btnA.Size.Height / 2, 0))
System.Threading.Thread.Sleep(250)
btnA.DoMouseUp(New MouseEventArgs(Windows.Forms.MouseButtons.Left, 1, btnA.Location.X + btnA.Size.Width / 2, btnA.Location.Y + btnA.Size.Height / 2, 0))
End Select
End Sub