Help on Event with Interface ?????

  • Thread starter Thread starter Serge Calderara
  • Start date Start date
S

Serge Calderara

Dear all,

I have one assembly named UserInfo which contains a form1
and a class1
I have a main application wich refer to the UserInfo
library by using the import keyword or adding reference to
my project.

From my main application, I would like to be informed when
the Button_click event of my form1 in UserInfo library is
fire.

Is there a safe way to retrive that event inside my
application through an interface?

thanks fo your answer
regards
serge
 
Serge Calderara said:
I have one assembly named UserInfo which contains a form1
and a class1
I have a main application wich refer to the UserInfo
library by using the import keyword or adding reference to
my project.

From my main application, I would like to be informed when
the Button_click event of my form1 in UserInfo library is
fire.

Is there a safe way to retrive that event inside my
application through an interface?

Is Form1 Public or Friend? If Public, change the modifier of the button from
Friend to Public. Then you can handle the Button's click event.

If you don't wanna make the Form or the Button public, you could implement a
public interface. Example:

public interface IWhatever
Event Buttonclicked
end interface

Form1:
implements IWhatever
'...
Public Event ButtonClicked() Implements IWhatever.ButtonClicked
'...
sub button_click
raiseevent ButtonClicked
end sub

Now you can expos the Form object from the DLL via a reference of Type
IWhatever.


But the best solution always depends on the purpose...
 
Thanks for your reply..

My button in the forms a declared as follow, which is the default, I
took it as it is from the wizard:

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button


in that declaration all events have by default to the scope of the
assembly with the keyword Friend. By experience I used to never change
any decalaration generated by a wizard, I use to get problem with that
in the past whne I do it.

And in that present case chech the commnet that is mentionned just
before the declaration of the button event.
it is mentionned DO NOT MODIFY uising the code editor.

Thats the reason why I was thinking of an other way of caching event of
the form outsite its own assembly.

By using the interface I was thinking on having all needed information
deliver from on point without changing any thing in the form.

Does this help you to tell me what is the best way ?

thanks
 
calderara serge said:
And in that present case chech the commnet that is mentionned just
before the declaration of the button event.
it is mentionned DO NOT MODIFY uising the code editor.

You can modify it using the form designer. In the property window, set the
"modifiers" to public.

Thats the reason why I was thinking of an other way of caching event
of the form outsite its own assembly.

By using the interface I was thinking on having all needed
information deliver from on point without changing any thing in the
form.

Does this help you to tell me what is the best way ?

Is making the Button public sufficient to solve the problem?
 
The answer is yes. I ran into this a couple of weeks ago and was helped out
by one of the gurus:

1) In my assembly with the main application (the one being imported):

Public Interface IHandleFTPReader
Sub AddHandlers(ByVal cl As FTPFileReader.FTPReader)
Sub RemoveHandlers(ByVal cl As FTPFileReader.FTPReader)
End Interface

Private ReadOnly m_Handler As IHandleFTPReader

Public Delegate Sub MessageToFormEventHandler(ByVal sender As Object, ByVal
e As MessageToFormEventArgs)
Public Event Message As MessageToFormEventHandler

Protected Overridable Sub OnMessage(ByVal e As MessageToFormEventArgs)
RaiseEvent Message(Me, e)
End Sub

Public Sub New(ByVal handler As IHandleFTPReader)
''add the handlers
m_Handler = handler
m_Handler.AddHandlers(Me)
End Sub

1a) event args

Public Class MessageToFormEventArgs
Inherits EventArgs
Private ms_Message As String
Public Sub New(ByVal sMessage As String)
ms_Message = sMessage
End Sub
Public ReadOnly Property Message() As String
Get
Return ms_Message
End Get
End Property
End Class

1b) to raise the event:
dim evt as New MessageToFormEventArgs("ERROR: blah ...")
OnMessage(evt)

2) In the form application:

Implements IHandleFTPReader

Public Sub AddHandlers(ByVal cl As yourproject1.yourproject1class)
Implements IHandleFTPReader.AddHandlers
AddHandler cl.Message, AddressOf MessageReceivedFromClass
End Sub

Private Sub MessageReceived(ByVal sender As Object, ByVal e As
projectnamegoeshere.MessageToFormEventArgs)
''do whatever you want with the message ...
End Sub


Hope this helps,
Timbo
 
Back
Top