Wednesday, 22 June 2016

c# Generics for single form instances

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
    class Class1<T> where T : Form, new()
    {
        private static T mInst;

        public Class1()
        {
            // Here i am just stuffing our single form instance code
            //  into the class constructor
            if (mInst == null)
            {
                mInst = new T();
                mInst.Show();
            }
            else
            {
                mInst.WindowState = FormWindowState.Normal;
                mInst.Focus();
            }
        }

        // You need this method for when a form closes
        public void letGo()
        {
            mInst = null;
        }
    }
}

http://www.techpowerup.com/forums/threads/sample-c-code-fun-with-generics-for-single-form-instances.34742/

--------------------------------------------------------------------------------------

private class open_form<T> where T: Form, new()
        {
            private static T _form = null;

            public open_form(Form main_form)
            {
                if(_form == null || _form.IsDisposed)
                {
                    _form = new T();

                    _form.MdiParent = main_form;
                    _form.Show();

                    _form.Location = new Point(0, 0);
                }
            }
        }


--------------------------------------------------------------------
open_form<Well_Production> new_form = new open_form<Well_Production>(this);

No comments:

Post a Comment