By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,901 Members | 1,459 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,901 IT Pros & Developers. It's quick & easy.

How do I access a "sibling" form?

P: n/a
I have a MDI Parent Child set of forms
FormContainer --MDIParent
FormStart --MDIChild
FormMain-->MDIChild
FormSliceInfo-->MDIChild

I use the following in the beginning of FomrContainer

Public Class FormContainer
Inherits System.Windows.Forms.Form
Dim frmStart As New FormStart
Dim frmMain As New FormMain
Dim frmSliceInfo As New FormSliceInfo

and so on.

Now when I am in the frmMain, I have a button that I want to use, to access
frmSliceInfo. How do I do that? I am able to access it from the Main Menu,
because it is under the FormContainer, but not from frmMain. In frmMain, I
tried Me.MDIParent.frmSliceInfo but that did not work.

Thanx for your help.
--
Anil Gupte
www.keeninc.net
www.icinema.com
Sep 5 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Create a module with Public object variables that are named for your forms.

Module1
Public Form1 as Form
public Form2 as Form
End Module

As you instantiate each form, assign the module object variable for that
form accordingly. You then have a reference to each form throughout your
application.

Sep 5 '07 #2

P: n/a
Anil,

Like controls are in a mdi forms all mdi forms in a collection in your
Parent form, you can just go through it to find the right one.

http://msdn2.microsoft.com/en-us/lib...en(vs.71).aspx

Cor
"Anil Gupte" <an*******@icinema.comschreef in bericht
news:%2****************@TK2MSFTNGP05.phx.gbl...
>I have a MDI Parent Child set of forms
FormContainer --MDIParent
FormStart --MDIChild
FormMain-->MDIChild
FormSliceInfo-->MDIChild

I use the following in the beginning of FomrContainer

Public Class FormContainer
Inherits System.Windows.Forms.Form
Dim frmStart As New FormStart
Dim frmMain As New FormMain
Dim frmSliceInfo As New FormSliceInfo

and so on.

Now when I am in the frmMain, I have a button that I want to use, to
access frmSliceInfo. How do I do that? I am able to access it from the
Main Menu, because it is under the FormContainer, but not from frmMain.
In frmMain, I tried Me.MDIParent.frmSliceInfo but that did not work.

Thanx for your help.
--
Anil Gupte
www.keeninc.net
www.icinema.com

Sep 5 '07 #3

P: n/a
I like this approach - although does it use more memory to declare these
forms
Public? Also, I have never used Modules? Where and how do you use them?
Is
there a tutorial somewhere on the web?

Thanx,
--
Anil Gupte
www.keeninc.net
www.icinema.com

"Charlie" <cfarrier at charlesfarriersoftware.comwrote in message
news:AA**********************************@microsof t.com...
Create a module with Public object variables that are named for your
forms.

Module1
Public Form1 as Form
public Form2 as Form
End Module

As you instantiate each form, assign the module object variable for that
form accordingly. You then have a reference to each form throughout your
application.

Sep 5 '07 #4

P: n/a
I figured out how to do it thanx to your pointers, but it seems like quite a
hack. I thought some thing like this would work:

Public Class FormContainer
Inherits System.Windows.Forms.Form
Dim frmStart As New FormStart
Dim frmMain As New FormMain
Public frmSliceInfo As New FormSliceInfo

i.e. make the form a Public object.
--
Anil Gupte
www.keeninc.net
www.icinema.com

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:eU****************@TK2MSFTNGP03.phx.gbl...
Anil,

Like controls are in a mdi forms all mdi forms in a collection in your
Parent form, you can just go through it to find the right one.

http://msdn2.microsoft.com/en-us/lib...en(vs.71).aspx

Cor
"Anil Gupte" <an*******@icinema.comschreef in bericht
news:%2****************@TK2MSFTNGP05.phx.gbl...
>>I have a MDI Parent Child set of forms
FormContainer --MDIParent
FormStart --MDIChild
FormMain-->MDIChild
FormSliceInfo-->MDIChild

I use the following in the beginning of FomrContainer

Public Class FormContainer
Inherits System.Windows.Forms.Form
Dim frmStart As New FormStart
Dim frmMain As New FormMain
Dim frmSliceInfo As New FormSliceInfo

and so on.

Now when I am in the frmMain, I have a button that I want to use, to
access frmSliceInfo. How do I do that? I am able to access it from the
Main Menu, because it is under the FormContainer, but not from frmMain.
In frmMain, I tried Me.MDIParent.frmSliceInfo but that did not work.

Thanx for your help.
--
Anil Gupte
www.keeninc.net
www.icinema.com


Sep 5 '07 #5

P: n/a
To add a module right click on the project name in the Solution Explorer,
then click Add Module. A module is lot like a class except it cannot be
instantiated. The object variables that you create do not take up much
memory. They merely act as pointers to your object.

In Module1 you should declare, but not instantiate your form object variables

Public F2 as Form2

I am still in the stone age working in Framework 1.0, so the following code
may be antiquated or even obsolete for 2.0 users...

but I tested this code and it works for me...

It would go in a form from where you want to open Form2, and where you have
a button called Button1.

'************

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If Not IsNothing(Module1.F2) AndAlso Module1.F2.Visible = False Then
'This applies after form has been closed
Module1.F2 = Nothing '(F2 does not test as nothing after being
closed.)
Module1.F2 = New Form2()
Module1.F2.Show()
ElseIf IsNothing(Module1.F2) Then 'This applies first time form is
opened
Module1.F2 = New Form2()
Module1.F2.Show()
Else 'This applies if form is visible and user clicks the button on
Form1 again
Module1.F2.BringToFront()
End If

End Sub

'*****************

You don't have to put Form2 in a module. You could declare an object
variable as Form2 wherever you wanted to use it. The module in the code is
just acting as a handy repository.

You could name the instance of Form2 in Module1 as Form2 instead of F2, if
you like.

You could carry this over to a menu item (instead of the button) on your
MDI, and it would be accessible to any open form in the MDI form.

Sep 5 '07 #6

P: n/a
Also, the code I provided prevents users from opening multiple instances of
the form class. You could modify the code to allow multiple instances.
Sometimes it's a good idea to allow multiple instances, such as when a form
is used for a database search, and the user may want more than one result
available at the same time.

Sep 5 '07 #7

P: n/a
OK, cool I think this will work. Especially because I want to keep the form
active (i.e. it should not lose the information in it's textboxes) even
after I
switch to another form. Otherwise multiple instances would not have the
previous info in them

Thanx a lot! I will try it today.
--
Anil Gupte
www.keeninc.net
www.icinema.com

"Charlie" <c2****@yahoo.comwrote in message
news:92**********************************@microsof t.com...
To add a module right click on the project name in the Solution Explorer,
then click Add Module. A module is lot like a class except it cannot be
instantiated. The object variables that you create do not take up much
memory. They merely act as pointers to your object.

In Module1 you should declare, but not instantiate your form object
variables

Public F2 as Form2

I am still in the stone age working in Framework 1.0, so the following
code
may be antiquated or even obsolete for 2.0 users...

but I tested this code and it works for me...

It would go in a form from where you want to open Form2, and where you
have
a button called Button1.

'************

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If Not IsNothing(Module1.F2) AndAlso Module1.F2.Visible = False
Then
'This applies after form has been closed
Module1.F2 = Nothing '(F2 does not test as nothing after being
closed.)
Module1.F2 = New Form2()
Module1.F2.Show()
ElseIf IsNothing(Module1.F2) Then 'This applies first time form is
opened
Module1.F2 = New Form2()
Module1.F2.Show()
Else 'This applies if form is visible and user clicks the button on
Form1 again
Module1.F2.BringToFront()
End If

End Sub

'*****************

You don't have to put Form2 in a module. You could declare an object
variable as Form2 wherever you wanted to use it. The module in the code
is
just acting as a handy repository.

You could name the instance of Form2 in Module1 as Form2 instead of F2, if
you like.

You could carry this over to a menu item (instead of the button) on your
MDI, and it would be accessible to any open form in the MDI form.

Sep 6 '07 #8

P: n/a
It works, it's great. You are a cool cat! :-)

--
Anil Gupte
www.keeninc.net
www.icinema.com

"Charlie" <c2****@yahoo.comwrote in message
news:1E**********************************@microsof t.com...
Also, the code I provided prevents users from opening multiple instances
of
the form class. You could modify the code to allow multiple instances.
Sometimes it's a good idea to allow multiple instances, such as when a
form
is used for a database search, and the user may want more than one result
available at the same time.

Sep 7 '07 #9

P: n/a
OK, unfortunately I have a problem.

As I said this works, but now the form2 is not recognized as an mdichild of
the container form. I tried to instantiate it in the conainer load event.
But that gives me some wierd behavior. What happens is that when I call
form 2 from form1, as soon as I complete some task in form 2 it returns to
form1 without asking. Here is the code in FormContainer:
Private Sub FormContainer_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' ***********This is your code
If Not IsNothing(L3GlobalModule.frmSliceInfo) AndAlso
L3GlobalModule.frmSliceInfo.Visible = False Then 'This applies after form
has been closed
L3GlobalModule.frmSliceInfo = Nothing '(frmSliceInfo does not test
as nothing after being closed)
L3GlobalModule.frmSliceInfo = New FormSliceInfo
L3GlobalModule.frmSliceInfo.Show()
ElseIf IsNothing(L3GlobalModule.frmSliceInfo) Then 'This applies first
time form is opened
L3GlobalModule.frmSliceInfo = New FormSliceInfo
L3GlobalModule.frmSliceInfo.Show()
Else 'This applies if form is visible and user clicks the button on
Form1 again
L3GlobalModule.frmSliceInfo.BringToFront()
L3GlobalModule.frmSliceInfo.WindowState = FormWindowState.Maximized
End If
' ***********This is your code
L3GlobalModule.frmSliceInfo.MdiParent = Me
L3GlobalModule.frmSliceInfo.Location = Me.Location
L3GlobalModule.frmSliceInfo.Size = Me.Size
L3GlobalModule.frmSliceInfo.Dock = DockStyle.Fill

frmMain.MdiParent = Me
frmMain.Location = Me.Location
frmMain.Size = Me.Size
frmMain.Dock = DockStyle.Fill
'************************
......
Now in FormMain, I have a button called AdvancedPrice:

Private Sub btnAdvancedPrice_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdvancedPrice.Click
' ***********This is your code
If Not IsNothing(L3GlobalModule.frmSliceInfo) AndAlso
L3GlobalModule.frmSliceInfo.Visible = False Then 'This applies after form
has been closed
L3GlobalModule.frmSliceInfo = Nothing '(frmSliceInfo does not test
as nothing after being closed)
L3GlobalModule.frmSliceInfo = New FormSliceInfo
L3GlobalModule.frmSliceInfo.Show()
ElseIf IsNothing(L3GlobalModule.frmSliceInfo) Then 'This applies first
time form is opened
L3GlobalModule.frmSliceInfo = New FormSliceInfo
L3GlobalModule.frmSliceInfo.Show()
Else 'This applies if form is visible and user clicks the button on
Form1 again
L3GlobalModule.frmSliceInfo.BringToFront()
L3GlobalModule.frmSliceInfo.WindowState = FormWindowState.Maximized
' ***********This is your code

In FormSliceInfo, I am having the user pick a text file, I then read it and
fill a multiline text box with it. As soon as I read the file it goes back
to FormMain. I stepped through it and saw that it was doing so as soon as
the textbox was filled.

What is going on? Please help, I am flummoxed!
Thanx,
--
Anil Gupte
www.keeninc.net
www.icinema.com

"Charlie" <c2****@yahoo.comwrote in message
news:92**********************************@microsof t.com...
To add a module right click on the project name in the Solution Explorer,
then click Add Module. A module is lot like a class except it cannot be
instantiated. The object variables that you create do not take up much
memory. They merely act as pointers to your object.

In Module1 you should declare, but not instantiate your form object
variables

Public F2 as Form2

I am still in the stone age working in Framework 1.0, so the following
code
may be antiquated or even obsolete for 2.0 users...

but I tested this code and it works for me...

It would go in a form from where you want to open Form2, and where you
have
a button called Button1.

'************

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If Not IsNothing(Module1.F2) AndAlso Module1.F2.Visible = False
Then
'This applies after form has been closed
Module1.F2 = Nothing '(F2 does not test as nothing after being
closed.)
Module1.F2 = New Form2()
Module1.F2.Show()
ElseIf IsNothing(Module1.F2) Then 'This applies first time form is
opened
Module1.F2 = New Form2()
Module1.F2.Show()
Else 'This applies if form is visible and user clicks the button on
Form1 again
Module1.F2.BringToFront()
End If

End Sub

'*****************

You don't have to put Form2 in a module. You could declare an object
variable as Form2 wherever you wanted to use it. The module in the code
is
just acting as a handy repository.

You could name the instance of Form2 in Module1 as Form2 instead of F2, if
you like.

You could carry this over to a menu item (instead of the button) on your
MDI, and it would be accessible to any open form in the MDI form.

Sep 7 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.