locked
Passing Form type as parameter RRS feed

  • Question

  • Hi every body
    i need to write sub that loads single instance of  form that take form type as parameter
    this the code that i need to write it
    1- i declared the form variable

    Code Snippet

    StudentForm frm = null;

    2- this the code loads the form

    Code Snippet

     if (frm == null || frm.IsDisposed)
       {
         frm = new StudentForm();
       }
      frm.MdiParent = this;
      frm.Show();
      frm.Activate();


    i need sup that takes 2 parameters like that

    Code Snippet

    Private void LoadForm (StudentForm , frm )


    Friday, April 13, 2007 8:22 AM

Answers

  • Hi,

     

    You could take a look at the Type class, and also the Activator class.

     

    you could have you method like this:

     

    Code Snippet

    private void LoadForm(Type tp, out Form frm)

    {

        //...

        frm = (Form)Activator.CreateInstance(tp);

        //...

    }

     

    Notice that this method recives a Type, and a reference to a Form object (I'm assuming that you derive your forms from System.Windows.Forms.Form).

     

    You would call it like this:

     

    Code Snippet

    Form frm = null;

    LoadForm(Type.GetType("Fully.Qualified.Name.Of.StudentForm"), out frm);

     

    Hope this helps.

     

    Luc

    Friday, April 13, 2007 10:25 AM

All replies

  • Hi,

     

    You could take a look at the Type class, and also the Activator class.

     

    you could have you method like this:

     

    Code Snippet

    private void LoadForm(Type tp, out Form frm)

    {

        //...

        frm = (Form)Activator.CreateInstance(tp);

        //...

    }

     

    Notice that this method recives a Type, and a reference to a Form object (I'm assuming that you derive your forms from System.Windows.Forms.Form).

     

    You would call it like this:

     

    Code Snippet

    Form frm = null;

    LoadForm(Type.GetType("Fully.Qualified.Name.Of.StudentForm"), out frm);

     

    Hope this helps.

     

    Luc

    Friday, April 13, 2007 10:25 AM
  • Hi:

    I think you can try store it as string then using reflection build controls you want

    follow is a simple demo

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    using System.Reflection;

     

    namespace Test

    {

        public partial class Form3 : Form

        {

            public Form3()

            {

                InitializeComponent();

            }

     

            private void Form3_Load(object sender, EventArgs e)

            {

                for (int i = 0; i < 2; i++)

                {

                    string a = "System.Windows.Forms.Button";

                    System.Reflection.Assembly *** = Assembly.GetAssembly(typeof(Form));

                    Type t = ***.GetType(a);

                    Object control = Activator.CreateInstance(t);

                    Control bt = (Control)control;

                    this.Controls.Add(bt);

                    bt.Location = new System.Drawing.Point(42, 20 * i);

                    bt.Name = "textBox" + i.ToString();

                    bt.Size = new System.Drawing.Size(80, 20);

                    bt.Text = i.ToString();

                    bt.TabIndex = i;

                    this.Controls.Add(bt);

                }

            }

        }

    }

     

    Friday, April 13, 2007 10:36 AM
  • Please MR Luc Morin
    Can u explain more how to use your code ?
    Friday, April 13, 2007 1:47 PM