Solved

Keep Showing Second Child Form.

Posted on 2009-07-04
14
221 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
  • 8
  • 6
14 Comments
 
LVL 12

Expert Comment

by:needo_jee
Comment Utility
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
Comment Utility
Hi needo_jee,

Let me try first.

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
Comment Utility
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
 
LVL 1

Author Comment

by:emi_sastra
Comment Utility
Hi needo_jee,

Are you still there to help?

Thank you.
0
 
LVL 12

Expert Comment

by:needo_jee
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 12

Expert Comment

by:needo_jee
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Ok.

I will try it later.

Thank you very much for your help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now