Solved

Keep Showing Second Child Form.

Posted on 2009-07-04
14
238 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.

0
Comment
Question by:emi_sastra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
14 Comments
 
LVL 12

Expert Comment

by:needo_jee
ID: 24776772
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



0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24778550
Hi needo_jee,

Let me try first.

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24778562
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.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 1

Author Comment

by:emi_sastra
ID: 24782191
Hi needo_jee,

Are you still there to help?

Thank you.
0
 
LVL 12

Expert Comment

by:needo_jee
ID: 24788124
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
0
 
LVL 12

Expert Comment

by:needo_jee
ID: 24788140
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
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24791056
Just set the flag is easy.

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

Thank you.
0
 
LVL 12

Expert Comment

by:needo_jee
ID: 24791211
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
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24791399
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.

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24791533
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.
0
 
LVL 12

Accepted Solution

by:
needo_jee earned 500 total points
ID: 24796402
well, i tried to address the issues shorting that you posted. The give solution should work perfectly for your needs.
Let me details it a bit...

Fist of all you need to make sure the flow of you forms.

I dont see any reason to close the parent form first and child later?

if you noticed ... I removed this line Dim frmChild2 As New frmChild2  from Button_click event handler and put it outside .. to make it a class/form level variable. means the line that you reported as problem .. I had moved it outside in my previous comments


have a look at code below




-thanks

PARENT FORM/ MAIN FORM
 
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        frmChild1.ShowDialog()
 
    End Sub
 
 
 
CHILD 1
 
Public Class frmChild1
 
    Public isChildOpen As Boolean = False
    Public frmChild2 As frmChild2
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        If Me.isChildOpen = False Then
 
            frmChild2 = New frmChild2("Munawar", "Love,Pakistan")
            frmChild2.Show(Me)
            isChildOpen = True
 
        Else
            'set any new value here
            frmChild2.TestNewValue = "This is new value"
        End If
 
 
    End Sub
 
    Private Sub frmChild1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
 
        If Me.isChildOpen = True Then
            e.Cancel = True
        Else
            e.Cancel = False
        End If
 
    End Sub
 
End Class
 
CHILD2
 
Public Class frmChild2
 
    Private userName As String
 
    Private employee As Object
 
    Public WriteOnly Property TestNewValue() As String
 
        Set(ByVal value As String)
 
            Me.userName = value
 
        End Set
 
    End Property
 
    Public Sub New(ByVal userName As String, ByRef emp As Object)
 
        InitializeComponent()
 
        Me.userName = userName
 
        Me.employee = emp
 
    End Sub
 
 
 
    Private Sub frmChild2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
 
        frmChild1.isChildOpen = False
 
    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24796599
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.
0
 
LVL 12

Expert Comment

by:needo_jee
ID: 24797997
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
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24798024
Ok.

I will try it later.

Thank you very much for your help.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question