Newbie RaiseEvent query

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a RadioButton which executes some code on its MouseDown event.

I would like to use the same RadioButton's KeyPress event to trigger its own
MouseDown event when the Enter Key is pressed. I naively thought the
following would work:

Private Sub RadioButton7_KeyPress(ByVal sender As Object, ByVal e as
Windows.Systems.Forms.KeyPressEventArgs) Handles RadioButton7.KeyPress
If e.KeyChar=Microsoft.VisualBasic.ChrW(13) then
RadioButton7_MouseDown(Me,e)
End If
End Sub

but the second argument (the e) calls up an error as it is a KeyPress Event
and not a MouseEvent (as expected in the procedure that I am trying to
invoke). How can I pass across the correct e argument (i.e. MouseDown) to
invoke the RadioButton7_MouseDown event? The online coding assistant tells me
about raising the event but it isn't intuitively obvious to me (a VB4 oldie)
as to how to do this. I'm sorry if this is a stupid question but I am
stumped. Many thanks for any replies.
 
Sorry folks, I think that I have figured this one out. By declaring a new
variable v as Windows.Systems.Forms.MouseEventArgs in the KeyPress
procedure, I can then pass this to my MouseDown handler as follows:

Private Sub RadioButton7_KeyPress(ByVal sender As Object, ByVal e as
Windows.Systems.Forms.KeyPressEventArgs) Handles RadioButton7.KeyPress
Dim v as Windows.Systems.Forms.MouseEventArgs
If e.KeyChar=Microsoft.VisualBasic.ChrW(13) then
RadioButton7_MouseDown(Me,v)
End If
End Sub

This seems to work OK but is there a more elegant solution or, more
importantly, is this going to cause me any problems?

Many thanks for any help offered, either with this or with my RadioButton
DoubleClick event handler query (I really do not know how to achieve the
latter). :-)
 
Two ways that I have dealt with this issue...

1. Create a seperate method that is called instead of calling an
instance of an event. I.E. Make a method called RadioMouseDown and call
that code from both RadioButton7_MouseDown and from
RadioButton7_KeyPress

2. Pass the value of Nothing into the argument for e. The reason why
you get an error when you are passing e is that in the KeyPress event,
the e is a KeyPress Event Argument. In the MouseDown event, the e is a
MouseDown Event Argument. Not the same.

Hope this helps...
 
LCAdeveloper said:
I have a RadioButton which executes some code on its MouseDown
event. .. . .
I would like to use the same RadioButton's KeyPress event to trigger
its own MouseDown event when the Enter Key is pressed.

What you want to do is run the /same/ piece of code when either
event occurs. Extract this code into a third routine and have both
the Event handlers call that.

RadioButton7_KeyPress( ...
DoWhateverRBMouseDownUsedToDo( sender )

RadioButton7_MouseDown( ...
DoWhateverRBMouseDownUsedToDo( sender )

HTH,
Phill W.
 
Back
Top