Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Inheriting Eventhandler in VB.NET from C# class library

Inheriting Eventhandler in VB.NET from C# class library

Scheduled Pinned Locked Moved Visual Basic
csharpvisual-studiohelpquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mdnss
    wrote on last edited by
    #1

    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.

    S 1 Reply Last reply
    0
    • M Mdnss

      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.

      S Offline
      S Offline
      Steven J Jowett
      wrote on last edited by
      #2

      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.

      M 1 Reply Last reply
      0
      • S Steven J Jowett

        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.

        M Offline
        M Offline
        Mdnss
        wrote on last edited by
        #3

        Yes I do. Actually the question is in the last code: What can I do to inherit those Eventhandlers?

        S 1 Reply Last reply
        0
        • M Mdnss

          Yes I do. Actually the question is in the last code: What can I do to inherit those Eventhandlers?

          S Offline
          S Offline
          Steven J Jowett
          wrote on last edited by
          #4

          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.

          M 1 Reply Last reply
          0
          • S Steven J Jowett

            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.

            M Offline
            M Offline
            Mdnss
            wrote on last edited by
            #5

            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' for

            RaiseEvent 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

            S 1 Reply Last reply
            0
            • M Mdnss

              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' for

              RaiseEvent 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

              S Offline
              S Offline
              Steven J Jowett
              wrote on last edited by
              #6

              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 Sub

              Public Event SiteReached()

              Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.

              M 1 Reply Last reply
              0
              • S Steven J Jowett

                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 Sub

                Public Event SiteReached()

                Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.

                M Offline
                M Offline
                Mdnss
                wrote on last edited by
                #7

                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?

                R 1 Reply Last reply
                0
                • M Mdnss

                  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?

                  R Offline
                  R Offline
                  rhuiden
                  wrote on last edited by
                  #8

                  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 set miBack.Enabled = False." Making the Sub in VB is rather easy

                  Sub SomeControl_SiteReached(s as Sender, ea as System.EventArgs)

                  The hard part is duplicating the middle. If miBack is global or accessible from SomeControl_SiteReached then you are fine. If miBack is local to ManuItem1_Click, then you have a problem. You will have to find some way to change the Enabled state for miBack.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups