Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to pass a form name to a sub program

How to pass a form name to a sub program

Scheduled Pinned Locked Moved C#
csharpquestioncomhelptutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mwith
    wrote on last edited by
    #1

    Hi All, I am new to C# and OOP. I refered a article about Single instance child for in MDIhttp://www.codeproject.com/csharp/single_instance.asp?df=100&forumid=51798&select=1394073#xx1394073xx[^]) I tried to write a sub program to stop that code duplication as follows private void frmLoad(Form frmNew) { Form[] charr = this.MdiChildren; if (charr.Length == 0) // no child form is opened { frmNew sus = new frmNew(); sus.MdiParent = this; sus.StartPosition = FormStartPosition.Manual; sus.Location = new Point(0, 0); sus.Show(); } else // child forms are opened { int count = 0; foreach (Form chform in charr) { if (chform.Name == "frmNew")// one instance of the form is opened { chform.Activate(); count = 0; break; // exit loop } else count++; } if (count > 0) { frmNew sus = new frmNew(); sus.MdiParent = this; sus.StartPosition = FormStartPosition.Manual; sus.Location = new Point(0, 0); sus.Show(); } } } I want to pass a form name as a argument. what is wrong with this? Please can anybody help me?:(( mWith

    L 1 Reply Last reply
    0
    • M mwith

      Hi All, I am new to C# and OOP. I refered a article about Single instance child for in MDIhttp://www.codeproject.com/csharp/single_instance.asp?df=100&forumid=51798&select=1394073#xx1394073xx[^]) I tried to write a sub program to stop that code duplication as follows private void frmLoad(Form frmNew) { Form[] charr = this.MdiChildren; if (charr.Length == 0) // no child form is opened { frmNew sus = new frmNew(); sus.MdiParent = this; sus.StartPosition = FormStartPosition.Manual; sus.Location = new Point(0, 0); sus.Show(); } else // child forms are opened { int count = 0; foreach (Form chform in charr) { if (chform.Name == "frmNew")// one instance of the form is opened { chform.Activate(); count = 0; break; // exit loop } else count++; } if (count > 0) { frmNew sus = new frmNew(); sus.MdiParent = this; sus.StartPosition = FormStartPosition.Manual; sus.Location = new Point(0, 0); sus.Show(); } } } I want to pass a form name as a argument. what is wrong with this? Please can anybody help me?:(( mWith

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Just create a member variable for frmNew:

      private Form2 frmNew = null;
      private void CreateFrmNew()
      {
      if (frmNew == null)
      {
      frmNew = new Form2();
      frmNew.MdiParent = this;
      frmNew.StartPosition = FormStartPosition.Manual;
      frmNew.Location = new Point(0, 0);
      }

      frmNew.Show();
      }

      Call CreateFrmNew, it will create frmNew if not exists, otherwise frmNew will be shown.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups