Raising CustomControl Events from within Class

G

Gman

Hi,

I have created a usercontrol, a grid control essentially. Within it I
have a class: clsGridRecord. I have coded the events such that when a
user clicks on the grid, say, the events occur on the parent form. This
is fine.

The problem occurs when I want to raise an event for a user clicking on
one of the clsRecords which are on the grid. So I've placed:
Public Event GridRecordClicked(ByVal rec As clsGridRecord,
ByVal e As EventArgs)
within the control scope and I can raise this from within the control.

However, if I try and raise it from within the control's class I get an
error: "GridRecordClicked is not an event of...[RootName space]'
Code as follows:

Private Sub clsGridRecord_MouseClick(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
'I need to raise an event, ideally within the ctlGrid
RaiseEvent Me.GridOwner.GridRecordClicked(me,e) 'doesn't work
End Sub

OK - I suppose I'm not allowed to raise the control's events. Indeed I
think I found something mentioned to this effect on this NG and read
that this could maybe be resolved using reflection. (Yikes).

The below workaround is a little convoluted (required an extra procedure
in between) but it's very simple so...

QUESTION: It works so what's wrong with it - why not do this instead of
reflection. Am I missing something obvious (other than it's a little
clumsy)?

'--------scope of ctlGrid start ------
'this is the event exposed to the parent form
Public Event GridRecordClicked(various args)

Private Sub evGridRecordClick(various args)
RaiseEvent GridRecordClicked(various args)
End Sub

'--------scope of clsGridRecord start ------
Private Sub clsGridRecord_MouseClick(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
'call proc to raise the event
evGridRecordClick (various args)
End Sub

'--------scope of clsGridRecord end ------
'--------scope of ctlGridend end ------

Many thanks
 
A

Aboulfazl Hadi

Hi
I don't understand your problem. Do you use Nested Class?
About events :
you can raise an event only in the class that you define the event.
you can use a common pattern to solve the problem. Write a method
within a class that raise the event and name it OnYourEventName. then
call it when appropriatly.

Best Regards,
A.Hadi
 
G

Gman

Hi,

Thanks for your response. Sorry I wasn't very clear. To explain better:

I have created a control which contains a class. Nested class? I'm not
sure but it's certainly nested within a control. I can get the control
to raise events without problem. The problem is raising events for the
control from the class.

E.g. In Form1:

Private Sub myControl1_CtrlIsClicked(etc.)
'Is fine - I can raise this
End Sub

But I want to raise an event *for the control* when the class is clicked:
Private Sub myControl1_CtrlsClassIsClicked(args)
'Problem is raising this event
End Sub

In myControl:

'I can raise this event from within my control
Public Event CtrlIsClicked(Args)

'But I can't raise this event from within the class
Public Event CtrlsClassIsClicked(Args)
'the workaround I've found is to call a Private Sub of the control from
the class and have that raise the event for me. But it's clumsy.

DOes that make more sense?

Thanks again.
Gman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top