-
Oct 20th, 2021, 12:40 PM
#1
Thread Starter
New Member
[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???
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.
-
Oct 20th, 2021, 01:08 PM
#2
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
-
Oct 21st, 2021, 09:37 AM
#3
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...
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Oct 21st, 2021, 10:16 AM
#4
Thread Starter
New Member
Re: Raise Event/Function/Sub on Child Form, From MDI Parent
Originally Posted by sapator
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!!!
-
Oct 21st, 2021, 10:51 AM
#5
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
-
Oct 22nd, 2021, 10:27 AM
#6
Thread Starter
New Member
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!
-
Oct 22nd, 2021, 01:58 PM
#7
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
-
Oct 22nd, 2021, 03:40 PM
#8
Thread Starter
New Member
Re: [RESOLVED] Raise Event/Function/Sub on Child Form, From MDI Parent
Originally Posted by Shaggy Hiker
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???
-
Oct 22nd, 2021, 06:04 PM
#9
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
-
Oct 23rd, 2021, 09:36 AM
#10
Thread Starter
New Member
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"
--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.
-
Oct 23rd, 2021, 02:07 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|