form within in form

  • Thread starter Thread starter Saso Zagoranski
  • Start date Start date
S

Saso Zagoranski

Hi!

I have a question regarding Windows Forms...
Let's say I have two forms... MainForm and FormTwo... is it possible, when I
open
FormTwo from MainForm, to display FormTwo inside MainForm instead of in it's
own window?

Thanks,
Saso
 
--------------------
From: "Saso Zagoranski" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Subject: form within in form
Date: Wed, 3 Sep 2003 22:23:26 +0200
Organization: ARNES
Lines: 12
Message-ID: <[email protected]>
NNTP-Posting-Host: clj3-56.dial-up.arnes.si
X-Trace: planja.arnes.si 1062620606 15556 194.249.53.56 (3 Sep 2003 20:23:26 GMT)
X-Complaints-To: (e-mail address removed)
NNTP-Posting-Date: Wed, 3 Sep 2003 20:23:26 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!newsfee
d01.sul.t-online.de!t-online.de!news.belwue.de!irazu.switch.ch!switch.ch!kan
ja.arnes.si!planja.arnes.si!not-for-mail
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:182048
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Hi!

I have a question regarding Windows Forms...
Let's say I have two forms... MainForm and FormTwo... is it possible, when I
open
FormTwo from MainForm, to display FormTwo inside MainForm instead of in it's
own window?

Thanks,
Saso

Hi,
It sounds like you are taking about a multiple-document interface
application where MainForm would be the parent form and FormTwo would be
the child. Please refer to the link below for information and examples on
how to program this using Windows Forms. If this doesn't answer your
question please reply back to this thread.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/
vbconmdiapplications.asp

Thanks,
Shruti Gupta
--

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
I'm thinking you must be talking about MDI (multiple document
interface). Here's a really dumb example:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace MdiExample {
public class Parent : Form {
public Parent() : base() {
this.ClientSize = new Size(400,300);
this.Text = "Mdi Example";
this.IsMdiContainer = true;
(new Child(this,"Who?")).Show();
(new Child(this,"What?")).Show();
(new Child(this,"When?")).Show();
}
}

public class Child : Form {
public Child(Form parent, string title) : base() {
this.ClientSize = new Size(100,75);
this.Text = title;
this.MdiParent = parent;
this.BackColor = Color.Red;
}

public static void Main(string[] args) {
Application.Run(new Parent());
}
}
}
 
Hi!

I tried the example you gave me... I have a Form (MainForm) on which I have
a button...
I edited the MainForm:
this.IsMdiContainer = true;

I edited FormTwo:
public FormTwo(Form parent, string text) : base()
{
this.MdiParent = parent;
}

When I press the button I execute:

FormTwo form2 = new FormTwo(this,"Some text");
form2.Show();

Nothing happens :(
What am I doing wrong?
 
I tried to mimic the changes you described below. This example works
for me in both the 1.0 and 1.1 .NET framework. However, to use buttons
inside a MDI parent form would be (IMO) way outside of convention.
Typically the parent's UI is all menu and toolbar driven. You will
notice that the button I have on the MDI parent form just sort of floats
above all the children. Also, if you haven't already, you should make
sure that your Button's EventHandler is being fired by adding a quick
messagebox:

MessageBox.Show("Yessir, I am indeed working");
FormTwo form2 = new FormTwo(this,"Some text");
form2.Show();

That will at least tell you if your problem is in the spawning of child
forms or in the handling of the Button's Click delegate.

My slightly altered example is included below.
/kel
-----------------------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

namespace MdiExample {
public class Parent : Form {
private Button button = null;

public Parent() : base() {
button = new Button();
button.Text = "Bam!";
button.Click += new EventHandler(this.OnUIEvent);
button.Location = new Point(0,0);
this.ClientSize = new Size(400,300);
this.Text = "Mdi Example";
this.IsMdiContainer = true;
this.Controls.Add(button);
}

public void OnUIEvent(object sender, EventArgs e) {
if (sender.Equals(button)) {
Form f = new Child(this,Guid.NewGuid().ToString());
f.Show();
}
}
}

public class Child : Form {
public Child(Form parent, string title) : base() {
this.ClientSize = new Size(100,75);
this.Text = title;
this.MdiParent = parent;
this.BackColor = Color.Red;
}

public static void Main(string[] args) {
Application.Run(new Parent());
}
}
}
 
Hi Kelly!

I found out what was wrong and you are right... The MDI parent form should
have just toolbars and main menus...
I removed the button from the MDI parent and it works great...

Just one more question...
I used to open FormTwo with ShowModal() and that way I could immediately
know, when FormTwo was closed...
How can MainForm now know when is FormTwo closed?

Thanks a lot...
Saso

Kelly Norton said:
I tried to mimic the changes you described below. This example works
for me in both the 1.0 and 1.1 .NET framework. However, to use buttons
inside a MDI parent form would be (IMO) way outside of convention.
Typically the parent's UI is all menu and toolbar driven. You will
notice that the button I have on the MDI parent form just sort of floats
above all the children. Also, if you haven't already, you should make
sure that your Button's EventHandler is being fired by adding a quick
messagebox:

MessageBox.Show("Yessir, I am indeed working");
FormTwo form2 = new FormTwo(this,"Some text");
form2.Show();

That will at least tell you if your problem is in the spawning of child
forms or in the handling of the Button's Click delegate.

My slightly altered example is included below.
/kel
-----------------------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

namespace MdiExample {
public class Parent : Form {
private Button button = null;

public Parent() : base() {
button = new Button();
button.Text = "Bam!";
button.Click += new EventHandler(this.OnUIEvent);
button.Location = new Point(0,0);
this.ClientSize = new Size(400,300);
this.Text = "Mdi Example";
this.IsMdiContainer = true;
this.Controls.Add(button);
}

public void OnUIEvent(object sender, EventArgs e) {
if (sender.Equals(button)) {
Form f = new Child(this,Guid.NewGuid().ToString());
f.Show();
}
}
}

public class Child : Form {
public Child(Form parent, string title) : base() {
this.ClientSize = new Size(100,75);
this.Text = title;
this.MdiParent = parent;
this.BackColor = Color.Red;
}

public static void Main(string[] args) {
Application.Run(new Parent());
}
}
}

Saso said:
Hi!

I tried the example you gave me... I have a Form (MainForm) on which I have
a button...
I edited the MainForm:
this.IsMdiContainer = true;

I edited FormTwo:
public FormTwo(Form parent, string text) : base()
{
this.MdiParent = parent;
}

When I press the button I execute:

FormTwo form2 = new FormTwo(this,"Some text");
form2.Show();

Nothing happens :(
What am I doing wrong?
 
Hi again! :)

I tried out your example and it works... but there is a problem if MainForm
and FormTwo are in different assemblies...

I call FormTwo from the main form, so I have a reference to the assembly
including FormTwo in the main form... but I can't have a reference from the
form Two assembly because of the circular dependency problem...

That means that I can't access the OnChildClosed() method from MainForm...
 
Back
Top