How to call an Event ?

  • Thread starter Thread starter Stan Sainte-Rose
  • Start date Start date
S

Stan Sainte-Rose

Hi,
How can I call an event from an action.
Example, I want to call the datagrid.Double.click event when a button is
pressed.

Stan.
 
I forgot to say I use a class inherit from datagrid, so I can not call
RaiseEvent.

Stan
 
Stan Sainte-Rose said:
Hi,
How can I call an event from an action.
Example, I want to call the datagrid.Double.click event when a button
is pressed.

You can not call an event, but you can call the same procedure that you call
in the double-clicke event handler.
 
* "Stan Sainte-Rose said:
How can I call an event from an action.
Example, I want to call the datagrid.Double.click event when a button is
pressed.

Why not put the code from the handler into a separate procedure and call
this procedure from the handler and the button's 'Click' event handler?
 
Herfried,

In fact, I use this bit of code and I want when I press the Enter key from a
datagrid to raise the DoubleClick
And as I have many datagrids I think it's not really cool to write a
procedure for each datagrid.


Public Class DataGridEnter
Inherits DataGrid
Protected Overrides Function ProcessCmdKey(ByRef msg As
System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As
Boolean
If msg.WParam.ToInt32() = CInt(Keys.Enter) Then
------- Raise my Event..
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function 'ProcessCmdKey

End Class



Stan
 
Stan,
If you are deriving from a control (or other class) you can raise a base
event, by calling a special a special protected method. For example the
DataGrid.DoubleClick event is raised by the DataGrid.OnDoubleClick method.
This DoubleClick/OnDoubleClick relationship is a standard design pattern in
..NET that I would recommend you follow in your own classes. For details see:

http://msdn.microsoft.com/library/d.../cpgenref/html/cpconEventNamingGuidelines.asp

http://msdn.microsoft.com/library/d...s/cpgenref/html/cpconEventUsageGuidelines.asp


You would need something like:

Public Class DataGridEnter
Inherits DataGrid

Protected Overrides Function ProcessCmdKey( _
ByRef msg As System.Windows.Forms.Message, _
ByVal keyData As System.Windows.Forms.Keys) _
As Boolean
If msg.WParam.ToInt32() = CInt(Keys.Enter) Then
MyBase.OnDoubleClick(EventArgs.Empty)
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function 'ProcessCmdKey

End Class

Note in VB.NET your OnMyEvent method can simply call RaiseEvent MyEvent, as
RaiseEvent will check to see if there are any handlers...

Hope this helps
Jay
 
Back
Top