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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to update form control from an embedded form

How to update form control from an embedded form

Scheduled Pinned Locked Moved Visual Basic
hardwaretutorialannouncement
18 Posts 3 Posters 1 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.
  • D Dave Kreskowiak

    Youve got it backwards. You expose the event in the child form, then subscribe to it in the parent form. Look up AddHandler and RemoveHandler to subscribe and unsubscribe to events of objects.

    A guide to posting questions on CodeProject[^]
    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
         2006, 2007, 2008

    S Offline
    S Offline
    sohaib_a
    wrote on last edited by
    #9

    ok..so should it be something like this in child class.. In child form... Public Class1 Public Event addtolist (Byval s as string) '' s is what is to printed in the listbox in parent class ' Private sub() dim obj as new class1 addhandler obj.addtolist,address of class2.addtolistbox ' some code ' ' RaiseEvent addtolist(s) 'where i want to add to the listbox in parent form ' ' End Sub End Class1 In parent form..... public class2 public sub addtolistbox(ByVal sender As Object, _ ByVal e As System.EventArgs) listbox1.items.add(e.argument) end sub end class2 Is this how what you mean Dave? EliottA,thanks for the comments and links.they were useful

    D 1 Reply Last reply
    0
    • S sohaib_a

      ok..so should it be something like this in child class.. In child form... Public Class1 Public Event addtolist (Byval s as string) '' s is what is to printed in the listbox in parent class ' Private sub() dim obj as new class1 addhandler obj.addtolist,address of class2.addtolistbox ' some code ' ' RaiseEvent addtolist(s) 'where i want to add to the listbox in parent form ' ' End Sub End Class1 In parent form..... public class2 public sub addtolistbox(ByVal sender As Object, _ ByVal e As System.EventArgs) listbox1.items.add(e.argument) end sub end class2 Is this how what you mean Dave? EliottA,thanks for the comments and links.they were useful

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #10

      OK, last time... The CHILD form exposes the public Event and calls RaiseEvent when it needs a subscriber to know of a change. This much you got right. The child form does NOT use AddHandler and RemoveHandler on it's own events because it's not subscribing to itself. The PARENT form needs to call AddHandler to subscribe to the events exposed by the child form. But, it can only do this if the parent form has a method that matches the header exposed by the event AND has an child form object (an instance of a class is called an "object"). So, after your parent form creates a child form object, it calls AddHandler to wire up the event exposed by the child form object to the method in the parent form that handles the event.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      L S 2 Replies Last reply
      0
      • D Dave Kreskowiak

        OK, last time... The CHILD form exposes the public Event and calls RaiseEvent when it needs a subscriber to know of a change. This much you got right. The child form does NOT use AddHandler and RemoveHandler on it's own events because it's not subscribing to itself. The PARENT form needs to call AddHandler to subscribe to the events exposed by the child form. But, it can only do this if the parent form has a method that matches the header exposed by the event AND has an child form object (an instance of a class is called an "object"). So, after your parent form creates a child form object, it calls AddHandler to wire up the event exposed by the child form object to the method in the parent form that handles the event.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #11

        Great description, 5.

        1 Reply Last reply
        0
        • D Dave Kreskowiak

          OK, last time... The CHILD form exposes the public Event and calls RaiseEvent when it needs a subscriber to know of a change. This much you got right. The child form does NOT use AddHandler and RemoveHandler on it's own events because it's not subscribing to itself. The PARENT form needs to call AddHandler to subscribe to the events exposed by the child form. But, it can only do this if the parent form has a method that matches the header exposed by the event AND has an child form object (an instance of a class is called an "object"). So, after your parent form creates a child form object, it calls AddHandler to wire up the event exposed by the child form object to the method in the parent form that handles the event.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          S Offline
          S Offline
          sohaib_a
          wrote on last edited by
          #12

          Thanks for description dave.. ok..so should it be something like this..i am sorry I havent tried it yet because visual studio is on my other laptop that I dont have with me right now..so just doing some research on it..I'll try it as soon as i get have it. In child form... Public Class1 Public Event addtolist (Byval s as string) '' s is what is to printed in the listbox in parent class ' Private sub() ' some code ' ' RaiseEvent addtolist(s) 'where i want to add to the listbox in parent form ' ' End Sub End Class1 In parent form..... public class2 public obj as new class1 addhandler obj.addtolist,addressof addtolistbox public sub addtolistbox(ByVal sender As Object, _ ByVal e As System.EventArgs) listbox1.items.add(e.argument) end sub end class2 Just one thing i didn't get..sorry to bother you.. "it can only do this if the parent form has a method that matches the header exposed by the event",I don't know what you meant by this. Do you mean the method in the parent form that handles the event should have the same name as the event ie; 'addtolist'?

          D 1 Reply Last reply
          0
          • S sohaib_a

            Thanks for description dave.. ok..so should it be something like this..i am sorry I havent tried it yet because visual studio is on my other laptop that I dont have with me right now..so just doing some research on it..I'll try it as soon as i get have it. In child form... Public Class1 Public Event addtolist (Byval s as string) '' s is what is to printed in the listbox in parent class ' Private sub() ' some code ' ' RaiseEvent addtolist(s) 'where i want to add to the listbox in parent form ' ' End Sub End Class1 In parent form..... public class2 public obj as new class1 addhandler obj.addtolist,addressof addtolistbox public sub addtolistbox(ByVal sender As Object, _ ByVal e As System.EventArgs) listbox1.items.add(e.argument) end sub end class2 Just one thing i didn't get..sorry to bother you.. "it can only do this if the parent form has a method that matches the header exposed by the event",I don't know what you meant by this. Do you mean the method in the parent form that handles the event should have the same name as the event ie; 'addtolist'?

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #13

            Yeah, pretty much got it. Except for one thing:

            public sub addtolistbox(ByVal sender As Object, _
            ByVal e As System.EventArgs)

            This is wrong. Like I said, the method signatures have to match. So, you event is defined like this:

            Public Event addtolist(ByVal s as string)

            so your event handler has to look the same:

            Private Sub AddToListbox(ByVal str As String)

            You don't have to have the same variable names, just the same types in the same order.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008

            S 1 Reply Last reply
            0
            • D Dave Kreskowiak

              Yeah, pretty much got it. Except for one thing:

              public sub addtolistbox(ByVal sender As Object, _
              ByVal e As System.EventArgs)

              This is wrong. Like I said, the method signatures have to match. So, you event is defined like this:

              Public Event addtolist(ByVal s as string)

              so your event handler has to look the same:

              Private Sub AddToListbox(ByVal str As String)

              You don't have to have the same variable names, just the same types in the same order.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008

              S Offline
              S Offline
              sohaib_a
              wrote on last edited by
              #14

              i tried it the way you said but it isn't working. In the parent form, I created on object of the child form and put the AddHandler syntax in the form load sub.

              D 1 Reply Last reply
              0
              • S sohaib_a

                i tried it the way you said but it isn't working. In the parent form, I created on object of the child form and put the AddHandler syntax in the form load sub.

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #15

                What do you mean "it isnt' working"? What IS happening? Have you steped through the code using the debugger to see where it's going and what it's doing??

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008

                S 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  What do you mean "it isnt' working"? What IS happening? Have you steped through the code using the debugger to see where it's going and what it's doing??

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007, 2008

                  S Offline
                  S Offline
                  sohaib_a
                  wrote on last edited by
                  #16

                  Yes I have done it..when it reaches the RaiseEvent it doesn't go the event handler..it just goes to the next line.

                  D 1 Reply Last reply
                  0
                  • S sohaib_a

                    Yes I have done it..when it reaches the RaiseEvent it doesn't go the event handler..it just goes to the next line.

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #17

                    That would probably be because there is nothing subscribed to the event.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                         2006, 2007, 2008

                    S 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      That would probably be because there is nothing subscribed to the event.

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                           2006, 2007, 2008

                      S Offline
                      S Offline
                      sohaib_a
                      wrote on last edited by
                      #18

                      well I have done exactly as you said. This is in the parent form,I have added this in the form load event of the parent form

                      Dim obj1 As New childform

                      AddHandler obj1.addtolist, AddressOf addtolistbox

                      and this is sub that is subscribed to the event..

                      Private Sub addtolistbox(ByVal str As String)

                      ListBox1.Items.Add(str)
                      End Sub
                      

                      In the child form.... Public Event addtolist(ByVal s As String) and in the sub where I want to add to listbox in parent form, RaiseEvent addtolist(ds1.Tables(0).Rows(0)("Reader_location").ToString & " Connected ")

                      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