467,864 Members | 1,926 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,864 developers. It's quick & easy.

Raising CustomControl Events from within Class

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
Feb 25 '06 #1
  • viewed: 1408
Share:
2 Replies
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

Feb 25 '06 #2
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
Aboulfazl Hadi wrote:
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

Feb 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by serge calderara | last post: by
4 posts views Thread by rawCoder | last post: by
reply views Thread by Joe Campbell | last post: by
4 posts views Thread by Dave A | last post: by
reply views Thread by Steve Wilkinson | last post: by
3 posts views Thread by Dom | last post: by
7 posts views Thread by SetonSoftware | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.