Click here to Skip to main content
15,881,172 members
Articles / Desktop Programming / Windows Forms
Alternative
Tip/Trick

Single Instance Form in a MDI application

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
1 Oct 2011CPOL 8K   3   1
private void OpenForm() where T : Form, new(){ T frm = (T)new List(this.MdiChildren).Find(f => f is T) ?? new T() { MdiParent = this }; frm.Show(); frm.Focus();}and usageOpenForm();
C#
private void OpenForm<T>() where T : Form, new()
{
    T frm = (T)new List<Form>(this.MdiChildren).Find(f => f is T) ?? new T() { MdiParent = this };

    frm.Show();
    frm.Focus();
}

and usage
C#
OpenForm<NewForm>();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThis solution is neat. Maybe it is even more useful to retur... Pin
wmjordan1-Oct-11 19:13
professionalwmjordan1-Oct-11 19:13 

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.