Events

S

sfauchille

I have several classes (users controls)

Public Cls1
Private _Commande As Boolean
Private _Alarme As Boolean
Private _Type As String
Public Event Type_Changed(ByVal Type As String)

Property Type() As String
...
End Property
Property Commande() As Boolean
...
End Property

Property Alarme() As Boolean
...
End Property

Private Sub UC_Z3_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
...
End Sub

Private Sub CboType_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
CboType.SelectedIndexChanged
RaiseEvent Type_Changed(CboType.ValueMember)
End Sub
End Class

The same for cls2 and Cls3

In one form, depending of "something" I want to display the user
control cls1

Public class cls_Main
dim c1
dim c2

private sub cls_Main_Load(...)
if ... then
c1 = New Cls1
elseif ... then
c2 = New Cls2
else
c3 = New Cls3
end if
end sub
end class

Question : How can i get the event of c1, c2 or c3?

sorry for my english
 
B

Bill McCarthy

The simplest approach would be to make them have a common base class that
has the events. Otherwise you will need to use AddHandler and RemoveHandler
etc.
 
S

sfauchille

The simplest approach would be to make them have a common base class that
has the events. Otherwise you will need to use AddHandler and RemoveHandler
etc.

















- Afficher le texte des messages précédents -

thanks for your answers but i never use the 2 methods you told me,
Do you have an example of doing a common base having the event
 
B

Bill McCarthy

Using a common base would be of the form of :

Class CommonBase: Inherits UserControl
Public Event Foo As EventHandler

Protected Sub OnFoo(ev as EventArgs)
RasieEvent Foo(Me, ev)
End Sub
End Class

Class C2 : Inherits CommonBase

Sub Rasietheevent()
mybase.OnFoo(EventArgs.Empty)
End Sub
End Class

and class C3 would be similar to C2. The in your code you woudl just
declare a common object, eg:

WithEvents cX As CommonBase

Then in your code you can write:
If condition Then
cX = new C1
Else
cX = new C2
End if

If you haven't got a common base, and are declaring multiple variables, you
can instead define them WithEvents also.

I would suggest reading up on some of the VB documentation on inheritance,
event handling ,WithEvents , AddHandler and RemoveHandler.




The simplest approach would be to make them have a common base class that
has the events. Otherwise you will need to use AddHandler and
RemoveHandler
etc.

















- Afficher le texte des messages précédents -

thanks for your answers but i never use the 2 methods you told me,
Do you have an example of doing a common base having the event
 

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