Inheriting Eventhandler in VB.NET from C# class library
-
Hi, Currently I'm working on a Smart Device project in VB.NET (in Visual Studio 2008) and I found a project on the net in C# which fits to my needs so I added to my solution as a class library. So right now I have a solution with a VB project and a C# project (class library). In the C# I have a part like this:
namespace Something
{
public class SomeList : SomeListControl
{
public event EventHandler SiteReached;
public event EventHandler SiteOpened;
public event EventHandler ListOpened;
...This namespace is included in my VB project as a Reference.
Public Class MainClass
Private SomeControl As SomeList Private Sub MenuItem1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click 'In the C# project the following code was implemented: 'SomeControl.SiteReached += (s, ea) => miBack.Enabled = false; 'SomeControl.SiteOpened += (s, ea) => miBack.Enabled = true; 'SomeControl.ListOpened += new EventHandler(SomeControl\_ListOpened); 'What can I do to inherit those Eventhandlers?
...
Thank you in advance for your kind help.
-
Hi, Currently I'm working on a Smart Device project in VB.NET (in Visual Studio 2008) and I found a project on the net in C# which fits to my needs so I added to my solution as a class library. So right now I have a solution with a VB project and a C# project (class library). In the C# I have a part like this:
namespace Something
{
public class SomeList : SomeListControl
{
public event EventHandler SiteReached;
public event EventHandler SiteOpened;
public event EventHandler ListOpened;
...This namespace is included in my VB project as a Reference.
Public Class MainClass
Private SomeControl As SomeList Private Sub MenuItem1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click 'In the C# project the following code was implemented: 'SomeControl.SiteReached += (s, ea) => miBack.Enabled = false; 'SomeControl.SiteOpened += (s, ea) => miBack.Enabled = true; 'SomeControl.ListOpened += new EventHandler(SomeControl\_ListOpened); 'What can I do to inherit those Eventhandlers?
...
Thank you in advance for your kind help.
Do you have a question?
Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.
-
Do you have a question?
Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.
-
Yes I do. Actually the question is in the last code: What can I do to inherit those Eventhandlers?
Sorry I missed the question. The declaration of the
SomeControl
object should be :-Private WithEvents SomeControl As SomeList
Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.
-
Sorry I missed the question. The declaration of the
SomeControl
object should be :-Private WithEvents SomeControl As SomeList
Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.
I'm sorry I wasn't clear what my question was. And thank you very much for your quick replies. I did the fix you suggested but still the error is there:
'SomeControl' is not an event of 'MyProject.Main'
forRaiseEvent SomeControl.SiteReached
Edit: I also added to the
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
SomeControl.SiteReached
No luck.modified on Tuesday, February 16, 2010 4:50 AM
-
I'm sorry I wasn't clear what my question was. And thank you very much for your quick replies. I did the fix you suggested but still the error is there:
'SomeControl' is not an event of 'MyProject.Main'
forRaiseEvent SomeControl.SiteReached
Edit: I also added to the
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
SomeControl.SiteReached
No luck.modified on Tuesday, February 16, 2010 4:50 AM
You need to handle the events of SomeControl within your MainClass then raise another event from the handle that is specific to your MainClass for example
Private Sub SomeControl_SiteReached() Handles SomeControl.SiteReached
'Additional validation goes here, if required
RaiseEvent SiteReached()
End SubPublic Event SiteReached()
Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.
-
You need to handle the events of SomeControl within your MainClass then raise another event from the handle that is specific to your MainClass for example
Private Sub SomeControl_SiteReached() Handles SomeControl.SiteReached
'Additional validation goes here, if required
RaiseEvent SiteReached()
End SubPublic Event SiteReached()
Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.
Thank you, that builds. Finally: Can you point me to the right direction what this does/means?
SomeControl.SiteReached += (s, ea) => miBack.Enabled = false;
SomeControl.SiteOpened += (s, ea) => miBack.Enabled = true;
SomeControl.ListOpened += new EventHandler(SomeControl_ListOpened);Should I ask in C# forum?
-
Thank you, that builds. Finally: Can you point me to the right direction what this does/means?
SomeControl.SiteReached += (s, ea) => miBack.Enabled = false;
SomeControl.SiteOpened += (s, ea) => miBack.Enabled = true;
SomeControl.ListOpened += new EventHandler(SomeControl_ListOpened);Should I ask in C# forum?
The last one is easy
SomeControl.ListOpened += new EventHandler(SomeControl_ListOpened);
converts to
AddHandler SomeControl.ListOpened, Addressof SomeControl_ListOpened
The other two are rather tricky. They use lambda functions which VB doesn't really support. Essentially
SomeControl.SiteReached += (s, ea) => miBack.Enabled = false;
Says, "Add a new handler for
SiteReached
that takes two parameters (s and ea). This function will setmiBack.Enabled = False
." Making the Sub in VB is rather easySub SomeControl_SiteReached(s as Sender, ea as System.EventArgs)
The hard part is duplicating the middle. If
miBack
is global or accessible fromSomeControl_SiteReached
then you are fine. IfmiBack
is local toManuItem1_Click
, then you have a problem. You will have to find some way to change theEnabled
state formiBack
.