473,396 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
2 1670
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: serge calderara | last post by:
Dear all, I have a class wich is raising events as normally it should do. having a form in the same assembly wich is catching those events works fne. Raise events gets catch normaly within the...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
4
by: rawCoder | last post by:
Hi all, How Can You Raise Events Asynchronously ? Now for the details ... I want to do inter modular communication using events in such a way that the contributing modules need not...
7
by: cider123 | last post by:
I'm coding a project using the following article as reference: http://www.codeproject.com/csharp/DynamicPluginManager.asp In this type of project, plugins are loaded dynamically into a Plugin...
0
by: Joe Campbell | last post by:
I am encountering a problem raising WMI events from an asp.net application. The error received (as captured in the event log) is as follows: System.Runtime.InteropServices.COMException...
4
by: Dave A | last post by:
I am developing a somewhat complex component at the moment and coincidently I am also reading the Framework Design Guidelines book. After reading the section about event raising I have re-written...
0
by: Steve Wilkinson | last post by:
I have a managed class (.NET 2.0) which has an internal thread. From within this thread events are being raised to the hosting UI. As the event is raised under the context of the internal thread...
3
by: Dom | last post by:
I'm teaching myself how to write a usercontrol, and how to raise events within the control. Some questions came up. Let's say I the event is called "Flyover", and everybody knows what is meant by...
7
by: SetonSoftware | last post by:
I have a class with shared properties and methods that manages the open forms in my WinForms application, VB.NET 2005. The data contained within must persist for the lifetime of the application....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.