form within in form

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Saso Zagoranski

    #1

    form within in form

    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


  • Shruti Gupta [MSFT]

    #2
    RE: form within in form


    --------------------[color=blue]
    >From: "Saso Zagoranski" <saso.zagoransk i@guest.arnes.s i>
    >Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
    >Subject: form within in form
    >Date: Wed, 3 Sep 2003 22:23:26 +0200
    >Organization : ARNES
    >Lines: 12
    >Message-ID: <bj5ijt$f64$1@p lanja.arnes.si>
    >NNTP-Posting-Host: clj3-56.dial-up.arnes.si
    >X-Trace: planja.arnes.si 1062620606 15556 194.249.53.56 (3 Sep 2003[/color]
    20:23:26 GMT)[color=blue]
    >X-Complaints-To: abuse@arnes.si
    >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:[/color]
    cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!new sfeed00.sul.t-online.de!newsf ee
    d01.sul.t-online.de!t-online.de!news. belwue.de!irazu .switch.ch!swit ch.ch!kan
    ja.arnes.si!pla nja.arnes.si!no t-for-mail[color=blue]
    >Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1820 48
    >X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
    >
    >Hi!
    >
    >I have a question regarding Windows Forms...
    >Let's say I have two forms... MainForm and FormTwo... is it possible, when[/color]
    I[color=blue]
    >open
    >FormTwo from MainForm, to display FormTwo inside MainForm instead of in[/color]
    it's[color=blue]
    >own window?
    >
    >Thanks,
    >Saso
    >
    >
    >[/color]

    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/de...us/vbcon/html/
    vbconmdiapplica tions.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.

    Comment

    • Kelly Norton

      #3
      Re: form within in form

      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.IsMdiConta iner = true;
      (new Child(this,"Who ?")).Show();
      (new Child(this,"Wha t?")).Show();
      (new Child(this,"Whe n?")).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());
      }
      }
      }

      Comment

      • Saso Zagoranski

        #4
        Re: form within in form

        Hi!

        I tried the example you gave me... I have a Form (MainForm) on which I have
        a button...
        I edited the MainForm:
        this.IsMdiConta iner = 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,"S ome text");
        form2.Show();

        Nothing happens :(
        What am I doing wrong?


        "Kelly Norton" <personne@kelle gous.com> wrote in message
        news:%2344YAAmc DHA.1552@TK2MSF TNGP11.phx.gbl. ..[color=blue]
        > 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.IsMdiConta iner = true;
        > (new Child(this,"Who ?")).Show();
        > (new Child(this,"Wha t?")).Show();
        > (new Child(this,"Whe n?")).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());
        > }
        > }
        > }
        >[/color]


        Comment

        • Kelly Norton

          #5
          Re: form within in form



          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,"S ome 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(th is.OnUIEvent);
          button.Location = new Point(0,0);
          this.ClientSize = new Size(400,300);
          this.Text = "Mdi Example";
          this.IsMdiConta iner = true;
          this.Controls.A dd(button);
          }

          public void OnUIEvent(objec t sender, EventArgs e) {
          if (sender.Equals( button)) {
          Form f = new Child(this,Guid .NewGuid().ToSt ring());
          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 Zagoranski wrote:[color=blue]
          > Hi!
          >
          > I tried the example you gave me... I have a Form (MainForm) on which I have
          > a button...
          > I edited the MainForm:
          > this.IsMdiConta iner = 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,"S ome text");
          > form2.Show();
          >
          > Nothing happens :(
          > What am I doing wrong?
          >
          >[/color]

          Comment

          • Saso Zagoranski

            #6
            Re: form within in form

            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" <personne@kelle gous.com> wrote in message
            news:%236WsSOyc DHA.1280@tk2msf tngp13.phx.gbl. ..[color=blue]
            >
            >
            > 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,"S ome 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(th is.OnUIEvent);
            > button.Location = new Point(0,0);
            > this.ClientSize = new Size(400,300);
            > this.Text = "Mdi Example";
            > this.IsMdiConta iner = true;
            > this.Controls.A dd(button);
            > }
            >
            > public void OnUIEvent(objec t sender, EventArgs e) {
            > if (sender.Equals( button)) {
            > Form f = new Child(this,Guid .NewGuid().ToSt ring());
            > 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 Zagoranski wrote:[color=green]
            > > Hi!
            > >
            > > I tried the example you gave me... I have a Form (MainForm) on which I[/color][/color]
            have[color=blue][color=green]
            > > a button...
            > > I edited the MainForm:
            > > this.IsMdiConta iner = 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,"S ome text");
            > > form2.Show();
            > >
            > > Nothing happens :(
            > > What am I doing wrong?
            > >
            > >[/color]
            >[/color]


            Comment

            • Saso Zagoranski

              #7
              Re: form within in form

              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...

              "Kelly Norton" <personne@kelle gous.com> wrote in message
              news:OUiPvG%23c DHA.2436@TK2MSF TNGP09.phx.gbl. ..[color=blue]
              > The easiest solution is to override the OnClosed event in the FormTwo
              > class and call back to the parent when a child closes. One important
              > thing to remember, though, is that the children will all close when the
              > parent closes (firing that event) so be sure to handle that properly.
              >
              > just threw that into the example too.
              >
              > /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(th is.OnUIEvent);
              > button.Location = new Point(0,0);
              > this.ClientSize = new Size(400,300);
              > this.Text = "Mdi Example";
              > this.IsMdiConta iner = true;
              > this.Controls.A dd(button);
              > }
              >
              > public void OnUIEvent(objec t sender, EventArgs e) {
              > if (sender.Equals( button)) {
              > Form f = new Child(this,Guid .NewGuid().ToSt ring());
              > f.Show();
              > }
              > }
              >
              > public void OnChildClosed(C hild child) {
              > MessageBox.Show (String.Format( "{0} is closing",child. Text));
              > }
              > }
              >
              > 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());
              > }
              >
              > protected override void OnClosed(EventA rgs e) {
              > Parent p = (Parent)this.Md iParent;
              > p.OnChildClosed (this);
              > }
              > }
              > }
              >[/color]


              Comment

              Working...