Problems switching between forms [C#]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
Simple enough - I had an application composed of a single form (frmMain) - now for the next version I needed to add a new form (frmFirst) to be displayed first (as sort of a control form).
Instead of displaying frmMain first I want to add a control form (frmFirst) to check and see if the user can proceed to launch the actual application.
I assume this can be done without moving the main() from frmMain to frmFirst (right?)

This is a step-by-step of what I am trying to accomplish:
- Application Starts
- frmFirst is displayed to the user
- if the user presses OK then frmMain is displayed to the user and frmFirst is closed/disposed/hidden (no more need)
- if the user presses CANCEL then the application terminates

So this is the approach I took,
- Start frmMain and immediatly hide it
- Create an instance of frmFirst and show it
- If OK is pressed in frmFrist then display frmMain and dispose of this (frmFirst)
- If CANCEL is pressed the application terminates

Find below sniplets of both frmMain and frmFirst which attempt to perform the above mentioned scheme:

-= frmMain =-
Code:
frmFirst fFirst = null;

static void Main() 
{
	Application.Run(new frmMain());
}

private void frmMain_Load(object sender, System.EventArgs e)
{
	this.hide()		// this doesn't seem to work?
	fFirst = new frmFirst(this);
	fFirst.Show();
}	// *** oddly enough this is where the ERROR occurs ???? at the end of the form_load???

-= frmFirst =-
Code:
frmMain fMain = null;

public frmFirst(frmMain Main)
{
	InitializeComponent();

	fMain = Main;
}

private void btnOK_Click(object sender, System.EventArgs e)
{
	if (Validate())
	{
		fMain.Show();
		this.Dispose(true);
	}
}

Oddly enough this is NOT working - I keep getting the following error (on the line of code with the *** above)
Error: Cannot access a disposed object named "frmFirst".
But why exactly? And how do I get around that so to get the desired results?

Also, it seems that the this.Hide() in the frmMain FormLoad does NOT work - when the application loads the form is NOT hidden, any clue why? Is that because you cannot .HIDE() in the form_load (seeing as the form has not yet been loaded)?
If so then how do you immediatly hide the form from the user?

Any help, ideas, or comments would be greatly appreciated - of course any alternative (and better more conventional) ideas would be great ;) (you know if I am going about this the wrong way).
Thanks,
 
I had a quick look, something like the following seems to work. Might be a programming 'no, no', but as far as I can tell it will achieve the effect your after.
C#:
		static void Main() 
		{
			Form1 splashForm = new Form1();
			splashForm.ShowDialog();

			if(splashForm.DialogResult == DialogResult.OK)
			{
				Application.Run(new Form2());
			}
		}
 
The Form.Load event is raised after the form is loaded and before the first time that it is shown. At this point it is not visible anyways, and there is a pending call to Show(), meaning that even if you try to hide it, after Form.Load is raised, it is going to be shown. You prevent this only by not calling Show(). If you are using the Application.Run() method, this will automatically call the Form.Show() method on the form passed in.

My only recommendation on Cags' code: the code does seem to work, but you might want to consider calling the parameterless overload for Application.Run to start the message loop before you actually introduce the UI. Cags' code will probably work, but I think that my suggestion would be the "kosher" way to do it. I'm not 100% sure, though, so you might want to research it.
 
Back
Top