How to hide a form on initialization

Have a problem.
C#:
private void UserShutdown(object sender, SessionEndingEventArgs e)
{
MessageBox.Show("exit");
	Application.Exit();
}
//-------------------------------
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
	e.Cancel=true;
	this.Visible=false;
MessageBox.Show("closed");
}
Here is what happens in the background (I beleive). The user attempts to restart the computer. First the Form1_Closing ev is called. It cancels the close event, making windows stop shutting down. Because of that, the UserShutdown ev is not called at all.

How can I tell from the Closing ev that the user is trying to shut down? Is there a way I can wait on that event? Or maybe there is another way? What do you think?
 
Hmm, thats weird. If the Closing event is fired before the Shutdown event, I dont know what to tell you.

Come to think of it, try checking Environment.HasShutdownStarted in your Closing event, perhaps thatll do it.
 
The command HasShutdownStarted must be VB because it is not there. Nevermind, it is not static. I think that is why it was not in the list. I will try it.
 
Last edited by a moderator:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Environment E=new Environment();
MessageBox.Show( "closed "+E.HasShutdownStarted );
e.Cancel=true;
this.Visible=false;
}

I get this error:
D:\My Documents\C Sharp projects\WallPaperRotator\Form1.cs(873): System.Environment.Environment() is inaccessible due to its protection level
 
Last edited by a moderator:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("closed "+System.Environment.HasShutdownStarted);
e.Cancel=true;
this.Visible=false;
}

D:\My Documents\C Sharp projects\WallPaperRotator\Form1.cs(873): An object reference is required for the nonstatic field, method, or property System.Environment.HasShutdownStarted
 
Weird, I guess they made it static in .NET 1.1. Since that class cannot be instantiated in .NET 1.0 I dont know what to suggest.
 
Ok, here is the scoop: the stack trace method does not work every time on my computer. Sometimes when I test it, the same message is produced (userclose) I think it is. The same message for the user trying to close the for as the computer shutting down.

I just installed 1.1 but not visual studio wont load. I uninsalled the old framework and that is probably why. Did I need to keep that? The Environment.HasShutdownStarted method would not compile with the old and new framework in place. Now I cant even test to see if it works now with just 1.1.

Vs says it cannot load the compiler now. How can I fix this? It says to reinstall Vs but I think it will put the old framework on there.
 
You should have both frameworks installed. Large amounts of Visual Studio 2002 are written with version 1.0 of the framework and it needs it installed.
 
How do I tell it to use the new framework so that Environment.HasShutdownStarted works. I tried downloading and installing SDK 1.1 but that did not seem to help.
 
You cant. If you want a development environment to compile code for .NET 1.1 you need Visual Studio .NET 2003.
 
This is my last option (I guess) before I buy VS2003 for 30 (if it is 30)

Can I make this work in some way?

System.Environment E=new System.Environment();
if(E.HasShutdownStarted)

I always get the error:
D:\My Documents\C Sharp projects\WallPaperRotator\Form1.cs(1022): System.Environment.Environment() is inaccessible due to its protection level

Why would it be there if I cant use it?
 
I honestly have no idea. I can only imagine its a bug, thats why its fixed in version 1.1. You might be able to instantiate the class under 1.0 via reflection.
 
I had to change the code some to get it to work correctly. Does this look right?
C#:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
	try{
		StackTrace Trace = new StackTrace(7);
		StackFrame Frame = Trace.GetFrame(7);
		string s= Frame.GetMethod().Name;
			if(s=="WndProc")
                                	{
				Application.Exit();
			}
			else
			{
				e.Cancel=true;
				this.Visible=false;
			}
	}
	catch{}
}
No need to respond if it looks ok.
 
Last edited by a moderator:
Back
Top