Click here to Skip to main content
15,917,652 members
Home / Discussions / C#
   

C#

 
GeneralRe: another textbox question Pin
[Marc]15-Dec-05 8:19
[Marc]15-Dec-05 8:19 
GeneralRe: another textbox question Pin
melanieab15-Dec-05 7:46
melanieab15-Dec-05 7:46 
QuestionBrowser Activity Monitor Pin
TAREQ F ABUZUHRI14-Dec-05 5:33
TAREQ F ABUZUHRI14-Dec-05 5:33 
QuestionX and Y coordinates that follow mouse cursor? Pin
...---...14-Dec-05 5:23
...---...14-Dec-05 5:23 
AnswerRe: X and Y coordinates that follow mouse cursor? Pin
WillemM14-Dec-05 9:30
WillemM14-Dec-05 9:30 
QuestionSingle instance of a form Pin
PHDENG8114-Dec-05 4:28
PHDENG8114-Dec-05 4:28 
AnswerRe: Single instance of a form Pin
S. Senthil Kumar14-Dec-05 5:38
S. Senthil Kumar14-Dec-05 5:38 
AnswerRe: Single instance of a form Pin
Curtis Schlak.14-Dec-05 5:42
Curtis Schlak.14-Dec-05 5:42 
Here's your problem with the FormLoader. You check to see if the form type has been loaded and then try to BringToFront a new instance of the form. Here's a rework of your class that does what you want.
public class FormLoader
{
	private static HybridDictionary m_InitializedForms = new HybridDictionary();

	public static void LoadFormType(Type formType, Form parentForm)
	{
		lock( typeof( FormLoader ) )
		{
			if( eh == null )
			{
				eh = new EventHandler( FormClosed );
			}
		}

		if( IsAlreadyLoaded( formType ) )
		{
			Form f = GetForm( formType );
			f.Focus();
			f.BringToFront();
			return;
		}

		Form frm = (Form)Activator.CreateInstance(formType);
		FlagAsLoaded( formType, frm );
		frm.Closed += eh;
		frm.Show();
	}

	private static void FlagAsLoaded( Type formType, Form f )
	{
		m_InitializedForms[formType.Name] = f;
	}

	private static void FlagAsNotLoaded( Type formType )
	{
		m_InitializedForms.Remove( formType );
	}

	private static bool IsAlreadyLoaded( Type formType )
	{
		return ( ( m_InitializedForms[ formType.Name ] as Form ) != null );
	}

	private static Form GetForm( Type formType )
	{
		return ( m_InitializedForms[ formType.Name ] as Form );
	}

	private static void FormClosed( object sender, EventArgs e )
	{
		Form closingForm = sender as Form;
		if( closingForm != null )
		{
			closingForm.Closed -= eh;
			FlagAsNotLoaded( sender.GetType() );
		}
	}

	private static EventHandler eh;
}
Hope the helps.

"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
GeneralRe: Single instance of a form Pin
PHDENG8114-Dec-05 6:51
PHDENG8114-Dec-05 6:51 
GeneralRe: Single instance of a form Pin
Curtis Schlak.14-Dec-05 7:20
Curtis Schlak.14-Dec-05 7:20 
GeneralRe: Single instance of a form Pin
PHDENG8114-Dec-05 7:01
PHDENG8114-Dec-05 7:01 
GeneralRe: Single instance of a form Pin
Curtis Schlak.14-Dec-05 7:18
Curtis Schlak.14-Dec-05 7:18 
GeneralRe: Single instance of a form Pin
Curtis Schlak.14-Dec-05 7:25
Curtis Schlak.14-Dec-05 7:25 
GeneralRe: Single instance of a form Pin
PHDENG8114-Dec-05 8:16
PHDENG8114-Dec-05 8:16 
GeneralRe: Single instance of a form Pin
Curtis Schlak.14-Dec-05 10:40
Curtis Schlak.14-Dec-05 10:40 
GeneralRe: Single instance of a form Pin
PHDENG8119-Dec-05 2:27
PHDENG8119-Dec-05 2:27 
QuestionWrapper for BITS Pin
oddstar14-Dec-05 4:24
oddstar14-Dec-05 4:24 
AnswerRe: Wrapper for BITS Pin
WillemM14-Dec-05 9:27
WillemM14-Dec-05 9:27 
Questionwhich textbox is entered? Pin
melanieab14-Dec-05 4:04
melanieab14-Dec-05 4:04 
AnswerRe: which textbox is entered? Pin
J4amieC14-Dec-05 4:21
J4amieC14-Dec-05 4:21 
GeneralRe: which textbox is entered? Pin
melanieab14-Dec-05 5:03
melanieab14-Dec-05 5:03 
AnswerRe: which textbox is entered? Pin
anandss14-Dec-05 11:11
anandss14-Dec-05 11:11 
QuestionProblem in gridview(asp.net (C#)) Pin
montu337714-Dec-05 3:56
montu337714-Dec-05 3:56 
QuestionTopMost property c# win forms Pin
fady_sayegh14-Dec-05 3:37
fady_sayegh14-Dec-05 3:37 
AnswerRe: TopMost property c# win forms Pin
xtroll14-Dec-05 4:53
xtroll14-Dec-05 4:53 

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.