Event passsing

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I have a main form which contains a panel which contains a child form. So
the hierarchy is as follows; frmMain->MyPanel-> frmChild

I have a toolbar on the main form. When a user presses a button on the
toolbar, how can I pass on the event to the child form?

Taking it further, is it possible to make it generic so that event is passed
to any child form (frmchild1, frmchild2,... so on) that is currently open in
the panel?

Some code examples would be appreciated.

Thanks

Regards
 
John said:
I have a main form which contains a panel which contains a child
form. So the hierarchy is as follows; frmMain->MyPanel-> frmChild

I have a toolbar on the main form. When a user presses a button on
the toolbar, how can I pass on the event to the child form?

When you get the click event, call a method of the child form. The child
form doesn't need to listen to the toolbar's events.
Taking it further, is it possible to make it generic so that event is
passed to any child form (frmchild1, frmchild2,... so on) that is
currently open in the panel?

Add the child forms to an array/arraylist/coolection and call the method
mentioned above in a loop.
 
Armin Zingler said:
When you get the click event, call a method of the child form. The child
form doesn't need to listen to the toolbar's events.

Would it be like this; call frmMain.mypanel.frmchild.mymethod() ?

Add the child forms to an array/arraylist/coolection and call the method
mentioned above in a loop.

There would be only one child form in the panel at any one time. Is there a
generic way to get whatever child form there is? Such as;

MyPanl.<childref?>.mymethod()?

Thanks

Regards
 
John said:
Would it be like this; call frmMain.mypanel.frmchild.mymethod() ?

You wrote that the toolbar is on the main form. I guess the ButtonClick
event is also handled in the main form. If frmChild is a public property of
the panel, you can write

Me.mypanel.frmchild.mymethod

If you have a reference to the child form in the main form, and if it is
called frmChild, then you can write

Me.frmChild.mymethod

There would be only one child form in the panel at any one time. Is
there a generic way to get whatever child form there is? Such as;

MyPanl.<childref?>.mymethod()?

You wrote frmChild1, frmChild2, ... You are now saying that these are
different Form types? Where do you hold the refrence to the child? How is it
declared?

Sorry, I first have to ask some questions before finding the right solution.
 
Hi

I think I am almost there. Thanks.

I am thinking that when a child form is made active in the panel, it leaves
its reference with the main form so the main form always knows which child
form's method it has to call.

Just one thing, how does a child form leaves its reference with the main
form? What variable type to declare and how to assign etc. Any code example?

Thanks

Regards
 
John said:
I think I am almost there. Thanks.

I am thinking that when a child form is made active in the panel, it
leaves its reference with the main form so the main form always knows
which child form's method it has to call.

Just one thing, how does a child form leaves its reference with the
main form? What variable type to declare and how to assign etc. Any
code example?



Have you already created the structure main->panel->child, or is it your
intention?

If you've already created it, please post the code that creates the panel
and the child and how and where the variables are declared.

As you ask how to declare the variables, I guess you have not created the
structure yet. This is _one_ example how to achieve it:

Private m_Panel As Panel
Private m_Child As ChildForm

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)

m_Panel = New Panel
m_Panel.Size = New Size( _
Me.ClientRectangle.Width - 10, _
Me.ClientRectangle.Height - 10 _
)
m_Panel.BackColor = Color.Blue
Me.Controls.Add(m_Panel)

m_Child = New ChildForm
m_Child.TopLevel = False
m_Child.Visible = True
m_Panel.Controls.Add(m_Child)

End Sub

Private Sub ToolBar1_ButtonClick( _
ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) _
Handles ToolBar1.ButtonClick

m_Child.MethodInChild()

End Sub


Of course, there can be many other approaches depending on the demands for
the application. For example, you could derive your own panel class from the
Windows.Forms.Panel class. Maybe it is not even necessary to put a _Form_
inside the panel.
 
Hi

This is all very clear. Thanks for that.

Taking it further, I have a number of child forms and I would like to allow
the user to select any to open. Since I would not know what child form user
chooses can I have a generic way to get a handle to the selected child form
in the main form, so that a method of the child form can be called?

Perhaps by having a variant variable child_ref on the master form and in
each child form's load event having something like

call frmMain.set_child_ref (me) ' me is the child form we are currently in.

Thanks

Regards
 
In this scenario, is there a way for the child form to get a handle of the
main form so a method on the main from can be called from the child form?

Thanks

Regards
 
John said:
Taking it further, I have a number of child forms

You mean different types but not multiple instance at a time?
and I would like to
allow the user to select any to open. Since I would not know what
child form user chooses can I have a generic way to get a handle to
the selected child form in the main form, so that a method of the
child form can be called?

Perhaps by having a variant variable child_ref on the master form and
in each child form's load event having something like

call frmMain.set_child_ref (me) ' me is the child form we are
currently in.


The answer is not simple. It depends...

As there can only be one child form, I would probably declare the variable
"as Form". Does the method to be called have the same signature in different
child types? Does it do the same in all types of child forms?

Same signature, different implementation: Write an Interface and implement
it in all child types.

Same signature, same implementation: Derive your child forms from a base
form containing the common code.

Different signatures: Use

If TypeOf <ChildVariable> Is ChildType1 then
Directcast (<ChildVariable>, ChildType1).MethodInType1
ElseIf TypeOf <ChildVariable> Is ChildType2 Then
Directcast (<ChildVariable>, ChildType2).MethodInType2
elseif ....
end if

to distinguish between different child types.
 
John said:
In this scenario, is there a way for the child form to get a handle
of the main form so a method on the main from can be called from the
child form?

It is possible, but why?
 
Some common methods can be defined on the main form which child forms can
call. This keeps the child forms simpler and it is easy to add more child
forms without adding too much code to them.

Regards
 
John said:
Some common methods can be defined on the main form which child forms
can call. This keeps the child forms simpler and it is easy to add
more child forms without adding too much code to them.

Why not derive the child forms from a base class containing the common code?
What kind of code is it?
 
Back
Top