Click here to Skip to main content
15,891,828 members
Home / Discussions / C#
   

C#

 
QuestionMenuStrip -> Open new child form Pin
benjamin yap13-May-08 4:31
benjamin yap13-May-08 4:31 
AnswerRe: MenuStrip -> Open new child form Pin
Gareth H13-May-08 4:38
Gareth H13-May-08 4:38 
GeneralRe: MenuStrip -> Open new child form Pin
benjamin yap13-May-08 4:48
benjamin yap13-May-08 4:48 
GeneralRe: MenuStrip -> Open new child form Pin
Gareth H13-May-08 4:52
Gareth H13-May-08 4:52 
GeneralRe: MenuStrip -> Open new child form Pin
benjamin yap13-May-08 5:00
benjamin yap13-May-08 5:00 
AnswerRe: MenuStrip -> Open new child form Pin
darkelv13-May-08 5:13
darkelv13-May-08 5:13 
GeneralRe: MenuStrip -> Open new child form Pin
benjamin yap13-May-08 5:24
benjamin yap13-May-08 5:24 
GeneralRe: MenuStrip -> Open new child form Pin
darkelv13-May-08 17:33
darkelv13-May-08 17:33 
It's a copy and paste code so the code may not compile. This is just to show you how to do it.

public class FormManager
    {
        // Main Form
        private MainForm mainForm;

        public FormManager(MainForm mainForm)
        {
            this.mainForm = mainForm;
        }

        public virtual System.Windows.Forms.Form GetForm(Type type, object[] args)
        {
            return GetMDIForm(type, args);
        }

        private ChildForm GetChildMDIForm(Type type)
        {
            // Try to get form of type 'type' from MdiChildren in mainForm
            for (int i = 0; i < mainForm.MdiChildren.Length; i++)
            {
                if (mainForm.MdiChildren[i].GetType().Equals(type))
                {
                    return (ChildForm)mainForm.MdiChildren[i];
                }
            }
            return null;
        }
        public virtual ChildForm GetMDIForm(Type type, object[] args)
        {
            ChildForm form = null;
            bool allowMultiple = false;

            // Attribute to decide whether multiple instance of form is allowed.
            object[] attributes = type.GetCustomAttributes(typeof(ChildFormAttribute), false);
            if (attributes.Length > 0)
            {
                allowMultiple = ((ChildFormAttribute)attributes[0]).AllowMulti;
            }

            if (!allowMultiple) // Not allow to create multiple
            {
                //Try to get from MDI Content
                form = GetChildMDIForm(type);
            }

            if (form == null)
            {
                form = CreateChildMDIForm(type, args);
                if (form != null)
                {
                    form.MdiParent = mainForm;
                    form.WindowState = FormWindowState.Maximized;
                    form.Show()
                }
                else
                {
                    throw new Exception(string.Format("Unable to create form of type '{0}'", type.ToString()));
                }
            }
            else
            {
                form.BringToFront();
            }

            return form;
        }
        private ChildForm CreateChildMDIForm(Type type, object[] args)
        {
            // Try to create a new form with args
            ConstructorInfo ci = type.GetConstructor(System.Type.EmptyTypes);
            ChildForm form = (ChildForm)ci.Invoke(args);
            return form;
        }

        public void ShowRole()
        {
            // Singleton class for opening new form or bring existing form to front
            Wine.Global.FormManager.Instance.GetForm(typeof(Wine.UI.Role), null);
        }

    }

    [ChildFormAttribute(false)] //<---- Use Attribute to indicate this form can not be multiple
    public partial class Role : ChildForm
    {
        public Role()
            : base()
        {
            InitializeComponent();
        }

    }

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public class ChildFormAttribute : System.Attribute
    {
        private bool allowMulti;
        public bool AllowMulti
        {
            get
            {
                return allowMulti;
            }
        }

        public ChildFormAttribute(bool allowMulti)
        {
            this.allowMulti = allowMulti;
        }
    }

QuestionResizing form nad positioning controls Pin
benjamin yap13-May-08 4:16
benjamin yap13-May-08 4:16 
QuestionXMLSerializer Pin
George_George13-May-08 4:12
George_George13-May-08 4:12 
AnswerRe: XMLSerializer Pin
led mike13-May-08 4:29
led mike13-May-08 4:29 
GeneralRe: XMLSerializer Pin
George_George13-May-08 4:49
George_George13-May-08 4:49 
AnswerRe: XMLSerializer Pin
Nissim Salomon13-May-08 4:40
Nissim Salomon13-May-08 4:40 
GeneralRe: XMLSerializer Pin
George_George13-May-08 4:48
George_George13-May-08 4:48 
QuestionBeginner's C# Book Pin
Ken Mazaika13-May-08 4:11
Ken Mazaika13-May-08 4:11 
AnswerRe: Beginner's C# Book Pin
CPallini13-May-08 4:21
mveCPallini13-May-08 4:21 
GeneralRe: Beginner's C# Book [modified] Pin
carbon_golem13-May-08 5:14
carbon_golem13-May-08 5:14 
GeneralRe: Beginner's C# Book Pin
CPallini13-May-08 5:20
mveCPallini13-May-08 5:20 
GeneralRe: Beginner's C# Book Pin
Hamid_RT13-May-08 5:28
Hamid_RT13-May-08 5:28 
GeneralRe: Beginner's C# Book Pin
CPallini13-May-08 5:34
mveCPallini13-May-08 5:34 
GeneralRe: Beginner's C# Book Pin
Hamid_RT13-May-08 5:47
Hamid_RT13-May-08 5:47 
GeneralRe: Beginner's C# Book Pin
CPallini13-May-08 5:51
mveCPallini13-May-08 5:51 
GeneralRe: Beginner's C# Book Pin
Hamid_RT13-May-08 5:55
Hamid_RT13-May-08 5:55 
GeneralRe: Beginner's C# Book Pin
Hamid_RT13-May-08 5:27
Hamid_RT13-May-08 5:27 
GeneralRe: Beginner's C# Book Pin
ToreUp13-May-08 6:21
ToreUp13-May-08 6:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.