We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Keep Showing Second Child Form.

emi_sastra
emi_sastra asked
on
Medium Priority
271 Views
Last Modified: 2012-05-07
Hi,

I have a mdiForm with 2 child Form.

The 1st Child Form is shown using :

FirstChildForm.MdiParent = mdiForm

I want to Show SecondChildForm from FirstChildForm.
I want the SecondChildForm keep showing until I close it.

Why I want to do this?

1. FirstChildForm show the summary of let say sales of a product of one month using dgv.
2. SecondChildForm show the detail of the FirstChildForm also using dgv.

My questions are:

1. How to show the  SecondChildForm that make it always there even I click FirstChildForm .
2. How to pass value from FirstChildForm to SecondChildForm .

A simple running sample provided is very appreciated.

Thank you.

Comment
Watch Question

Munawar HussainPrincipal Software Engineer
CERTIFIED EXPERT

Commented:
hi,


the child form that you want to open from MDI form should not be set as MDI child because MDI form itself is a top level form and any form that is being opened using ShowDialog tends to become Topleve form as well. Therefore.

Open first child using showDialogue and dont set any parent

lets say i have 3 forms

frmParent
frmChild1
frmChild2


In MDI form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
              frmChild1.ShowDialog()
    End Sub


in First Children
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
           Dim frmChild2 As New frmChild2
        frmChild2.Show(Me)
    End Sub

instead of using ShowDialoge, I have used show but I have passed current form as Owner of child form. That would let you click the
child 1 as well it will keep the child 2 at the top.



Question 2:

To pass any value from child 1 to child 2. Create an overloaded constructor in child 2 and pass your values while creating a new instance of child2..

1- create overloaded constructor i child 2
Public Class frmChild2

    Private userName As String
    Private employee As Object

    Public Sub New(ByVal userName As String, ByRef emp As Object)
        InitializeComponent()

        Me.userName = userName
        Me.employee = emp
    End Sub



End Class



2 - pass your values from child 1

Public Class frmChild1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frmChild2 As New frmChild2("Munawar", "Love,Pakistan")
        frmChild2.Show(Me)
       

    End Sub
End Class


-thanks



Author

Commented:
Hi needo_jee,

Let me try first.

Thank you.

Author

Commented:
The problem is at :

in First Children
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
           Dim frmChild2 As New frmChild2
        frmChild2.Show(Me)
    End Sub

Because we often click at dgv at first Child then   Dim frmChild2 As New frmChild2 will be a problem?

Thank you.

Author

Commented:
Hi needo_jee,

Are you still there to help?

Thank you.
Munawar HussainPrincipal Software Engineer
CERTIFIED EXPERT

Commented:
hi,

definitely you need add a check that if child form is already shown then dont let it create a new instance like this..



a global variable (better option to put it in a module or a shared member in any class.

***********
Module xyz

public IsChildOpen as boolean =false

****************
In chind form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
if IsChildOpen=false then
           Dim frmChild2 As New frmChild2
        frmChild2.Show(Me)
end if
    End Sub


in child form2

on close of form reset the flag value.

IsChildOpen=false

Hope this would help you

-thanks
Munawar HussainPrincipal Software Engineer
CERTIFIED EXPERT

Commented:
sorry , this will be something like

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
if IsChildOpen=false then
           Dim frmChild2 As New frmChild2
        frmChild2.Show(Me)


'Set the flag here to stop new instancess....
IsChildOpen=true
end if
    End Sub


there are so many ways to implement but you can use above if you like..

-thanks

Author

Commented:
Just set the flag is easy.

But we should refresh the 2nd child data from 1st child data when it is clicked.

Thank you.
Munawar HussainPrincipal Software Engineer
CERTIFIED EXPERT

Commented:
hmmmm... so what..

there should not be any issue whatever you want to set or get ...here that goes..

 Dim frmChild2 As New frmChild2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
if IsChildOpen=false then
''''''''''''''''''''''''''''''''''''''''''''''''''''       Dim frmChild2 As New frmChild2
       frmChild2.Show(Me)
'Set the flag here to stop new instancess....
IsChildOpen=true



else  ' set anything here because else part tells that your form is already open ..I have used two properties of child form you can create as you need... set properties or pass class ref or as you feel comfortable..

frmChild2.UserName="test"
frmChild2.xyz="This is Munawar"

end if

End Sub


-thanks

Author

Commented:
Ok.

if IsChildOpen=false then
''''''''''''''''''''''''''''''''''''''''''''''''''''       Dim frmChild2 As New frmChild2 ---> problem

It is local variable.
The problem raised when we tried to close Form2 when Form1 is closed.

Thank you.

Author

Commented:
Another problem.

The IsChildOpen is public variable of module1.

It could be set when form2 is closing, but another problem raised, since it is public, how we could know which child form is close. For example we have form3 that call form4.

Thank you.
Principal Software Engineer
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Hi needo_jee,

How about if the 2nd child form is a public form like Product. You know, we could call it from any child form that need it?

Thank you.
Munawar HussainPrincipal Software Engineer
CERTIFIED EXPERT

Commented:
hi,

I stated before, its up you.. there can be many approaches.. I just told one way.

you can adopt that suits you best.. we can make properties, we can simply use public members .. we can make controls public and access directly on diff forms.

we can call a form show directly without creating instance... in that way default constructor would be used. however if you want to pass some data to a dialogue form while shoing it then create an instance of form and create overloaded constructors..

i think you got the idea so, choose any approach that works best.

-thanks

Author

Commented:
Ok.

I will try it later.

Thank you very much for your help.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.