Results 1 to 11 of 11

Thread: [RESOLVED] Raise Event/Function/Sub on Child Form, From MDI Parent

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    14

    Resolved [RESOLVED] Raise Event/Function/Sub on Child Form, From MDI Parent

    Hi!

    Maybe this is easy for you, I think so...
    well, I have this MDI Form, whitch open other forms (childs) every form have diferents buttons, but many of then are the same and do the same function/sub.
    so I wanna move this buttons on a bar on the MDI Parent, thats easy... the hard part (for me) is reaise some event/function/sub on the ACTIVE CHILD FORM when i do click on this MDI Parent's button...
    how can I do that???

    Name:  vbforums.jpg
Views: 765
Size:  15.7 KB

    as you can see... I wanna move the blue circle button (on child form) on the red circle zone (on MDI Parent) and keep the functionality of this button and run my sub/function defined on the child form (the yellow circle form)

    Thanks a lot for the help.

    Regarts.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Raise Event/Function/Sub on Child Form, From MDI Parent

    One option that you have is to create an event on the parent form that gets raised when the button was clicked. Then on the child form you would setup an event handler for when that event gets raised. For example:
    Code:
    Public Class ParentForm
        ...
    
        Private Sub ConsultarButton_Click(sender As Object, e As EventArgs) Handles ConsultarButton.Click
            OnConsultarButtonClicked()
        End Sub
    
        Protected Overridable Sub OnConsultarButtonClicked()
            RaiseEvent ConsultarButtonClicked(ConsultarButton, EventArgs.Empty)
        End Sub
    
        Public Event ConsultarButtonClicked(sender As Object, e As EventArgs)
    
    End Class
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Raise Event/Function/Sub on Child Form, From MDI Parent

    If I understand correctly you need to fins the focused mdi child form and do stuff. I would assume a loop on the children forms as find the focused on might do that?
    something like:

    Code:
       For Each cont As Control In Me.MdiChildren
                    If TypeOf cont Is Form Then
       If cont.Focused = True Then...
    
    '' or If cont.Focused then , so I won't get pommeled from some here
    Personally I would not rely on the focus because on small size child forms all thing can go wrong, I may have used a checkbox "allow changes" or something else but...
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    14

    Re: Raise Event/Function/Sub on Child Form, From MDI Parent

    Quote Originally Posted by sapator View Post
    If I understand correctly you need to fins the focused mdi child form and do stuff. I would assume a loop on the children forms as find the focused on might do that?
    something like:

    Code:
       For Each cont As Control In Me.MdiChildren
                    If TypeOf cont Is Form Then
       If cont.Focused = True Then...
    
    '' or If cont.Focused then , so I won't get pommeled from some here
    Personally I would not rely on the focus because on small size child forms all thing can go wrong, I may have used a checkbox "allow changes" or something else but...
    That's right! but find the focused child form it's easy... then... how can I run something on this child form???
    I would try what dday9 says... but if you got more ideas i would like read you.
    thanks a lot dday9!!!

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Raise Event/Function/Sub on Child Form, From MDI Parent

    Remember that events are fundamentally pretty simple things. When an object states that it will raise an event, all it is really saying is that it is maintaining a list of methods that will be called when the event happens. Anybody who can see the object can add methods to that list, so when the event happens, the object will go through the list calling each method in turn.

    All that AddHandler is doing is telling the object, "add this method to the list you are maintaining for this event." So, as long as you can SEE the object that is raising the event, then you can add tell it to add a method to its list with AddHandler. As long as your child form can see the object that will be raising the event (the MDI parent form, from the sound of it), then you can add an event handler. That would be the right way to go about it. The only potential difficulty to that would be if the form that wanted to handle the event didn't know about the object that was raising the event. In that case, you'd have to pass that object to the form that wanted to handle the event, or use an intermediary, or something like that, but in all cases, the underlying event mechanism is pretty simple.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    14

    Re: Raise Event/Function/Sub on Child Form, From MDI Parent

    Hi!!! I finally get it!
    my way is a mix of all your advices :P

    On the MdiParent I coded this:

    Code:
    Private Sub MyButton_Click(sender As Object, e As EventArgs) Handles MyButton.Click
            RaiseEvent RaiseEventOnChildForm()
    End Sub
    
    Public Shared Event RaiseEventOnChildForm()
    And on the Child Form I coded this:

    Code:
    Public Sub New()
            InitializeComponent()
            AddHandler MDIParent1.RaiseEventOnChildForm, AddressOf EventRaisedOnChildForm
    End Sub
    
    Public Shared Sub EventRaisedOnChildForm()
            'Whatever I Wanna Do On Child Form :)
    End Sub
    
    'this sub is for release stack memory (that's what I read somewhere, and make sense)
    Private Sub ChildForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
            RemoveHandler MDIParent1.RaiseEventOnChildForm, AddressOf EventRaisedOnChildForm
    End Sub
    And that Solved it! It works for me!!! that simple.

    thanks a lot to everyone for the help. I'll be back soon.
    Thanks!

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: [RESOLVED] Raise Event/Function/Sub on Child Form, From MDI Parent

    You almost certainly don't want that sub to be Shared. It's a bit hard to think of a scenario where you'd need it to be, and every other scenario you wouldn't want it to be.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    14

    Re: [RESOLVED] Raise Event/Function/Sub on Child Form, From MDI Parent

    Quote Originally Posted by Shaggy Hiker View Post
    You almost certainly don't want that sub to be Shared. It's a bit hard to think of a scenario where you'd need it to be, and every other scenario you wouldn't want it to be.
    I also thoght that... but... sorry... I had doubts but I dont get it... how should be declared the access modifiers???

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: [RESOLVED] Raise Event/Function/Sub on Child Form, From MDI Parent

    Just remove the shared. Not sure why they are there on either the event or the event handler. I'm not sure why you thought they were required. That usually indicates that you're over thinking this, but they aren't required. If you have a shared method in some class (and a form is just a class), then that method exists for all instances of the class, which means you'll have a hard time getting to members of any specific instance of the class. So, you'd use a shared member if you only want to use other shared members, and for an event handler, that seems unlikely.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    14

    Re: [RESOLVED] Raise Event/Function/Sub on Child Form, From MDI Parent

    Ok, I dont know how to deal with this now; please help...
    I deleted the "shared" modifier on both definitions (event and event handler) and I get 2 errors:

    "A reference to a nom shared member requieres a instance of object"

    Name:  vbforums2.png
Views: 662
Size:  39.3 KB

    --Yes I Code in spanish... anyway...

    what I'm missing now???

    If i dont delete this modifiers the program runs ok, but, yes, the event reaises on every form opened, and i dont want that, i want the obvious... the event raise ONLY on the active child form...
    whats next? (sorry Im new on this)... and thanks for your time.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: [RESOLVED] Raise Event/Function/Sub on Child Form, From MDI Parent

    Took me a moment to see where those error messages were. I always switch errors to red. I grew up with teachers using red pens for errors, so I guess that just feels right to me. Blue on black isn't terribly visible, but when I looked closer, there it was.

    I'm not quite sure how to put this question, but it might be, "where is MDIParent1 coming from?" That probably misleads. What I'm getting at, is MDIParent1 the name of the MDIParent1 form, or is it the specific instance of the MDI parent that you are showing? It has to be the latter.

    I haven't used MDI in a very long time, but I think you might be able to use Me.Parent rather than MDIParent1. The key is that you have to be referencing the instance of the MDI parent that you are showing, which may well be the Parent of the form that has the event handler, but I don't recall.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width