How you can call mdi a child

If youre wondering how you should call a mdi child, then are here some examples that you can use. Read them all and understand the difference.
This will work ( VB.Net) but is not save to work with, you need a Instance of the form you want to use.

public class main
{
  private void mnu_1_Click(object sender, System.EventArgs e)
  {
    System.Windows.Forms.ToolStripMenuItem tsmi = 
                              (System.Windows.Forms.ToolStripMenuItem) sender
    switch (tsmi.Text)
    {
      case "A":
        formA.MdiParrent = this;
        formA.Show();
        break;

      case "B":
        formB.MdiParrent = this;
        formB.Show();
        break;
    }
  }
} // eof class

This is good to use but is using to much memory

public class main
{
  formA frmA = new formA();
  formB frmB= new formB();
  
  private void mnu_1_Click(object sender, System.EventArgs e)
  {
    System.Windows.Forms.ToolStripMenuItem tsmi = 
                             (System.Windows.Forms.ToolStripMenuItem) sender
    switch (tsmi.Text)
    {
      case "A":
        frmA.MdiParrent = this;
        frmA.Show();
        break;

      case "B":
        frmB.MdiParrent = this;
        frmB.Show();
        break;
    }
  }
} // eof class

This one is using less memory but it still can do with less memory.

public class main
{
  formA frmA;
  formB frmB;
  
  private void mnu_1_Click(object sender, System.EventArgs e)
  {
    System.Windows.Forms.ToolStripMenuItem tsmi = 
                             (System.Windows.Forms.ToolStripMenuItem) sender
    switch (tsmi.Text)
    {
      case "A":
        frmA = new formA();
        frmA.MdiParrent = this;
        frmA.Show();
        break;

      case "B":
        frmB = new formB();
        frmB.MdiParrent = this;
        frmB.Show();
        break;
    }
  }
} // eof class

I find this way much better an cleaner.

public class main
{
  private void mnu_1_Click(object sender, System.EventArgs e)
  {
    System.Windows.Forms.ToolStripMenuItem tsmi = 
                              (System.Windows.Forms.ToolStripMenuItem) sender
    System.Windows.Forms.Form frm = null;
    switch (tsmi.Text)
    {
      case "A":
        frm = formA;
        break;

      case "B":
        frm =formB;
        break;
    }
    frm.MdiParrent = this;
    frm.Show();
  }
} // eof class

These are just a few examples to show how it can be done, the reason i to put this post up was seeing new code that used a lot of unnecessary memory.


Warning: count(): Parameter must be an array or an object that implements Countable in /home/p20382/domains/2be-it.com/public_html/programming/wordpress/wp-includes/class-wp-comment-query.php on line 405

One thought on “How you can call mdi a child”

Leave a Reply