Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, September 2, 2008 12:33 AM
Hi guys,
well i have a application which changes the resoloution on the computer (works fine)
i have the code to change it back to the original, say on a button click
however i dont know the code for running it when the application is exited as i need to change it back when the application exits.
I know people hate there resoloution being changed but this is for a particular purpose not the general public so it is fine.
Thanks,
Matt
All replies (10)
Tuesday, September 2, 2008 12:48 AM
In your Form_Closing event you can check the e.CloseReason enumeration. If your Form is closing for a specific reason (i.e. you are calling Application.Exit) then you can act accordingly.
http://msdn.microsoft.com/en-us/library/system.windows.forms.closereason.aspx
Tuesday, September 2, 2008 1:18 AM
ah Closing, cheers, i tried Close, Unload, Exit, everything i could think of except Closing :|
Cheers,
Matt
Tuesday, September 2, 2008 1:29 AM
hmm i seem not to be calling the form closing event right, forms name is main
can you please give me an example of say showing a msgbox when the application exits?
Tuesday, September 2, 2008 1:51 AM
private void Form1_FormClosing(object sender, FormClosingEventArgs e) |
{ |
switch (e.CloseReason) |
{ |
case CloseReason.ApplicationExitCall: |
// The Exit method of the Application class was called. |
break; |
case CloseReason.FormOwnerClosing: |
// The owner form is closing. |
break; |
case CloseReason.MdiFormClosing: |
// The parent form is closing. |
break; |
case CloseReason.None: |
// Unknown closing reason. |
break; |
case CloseReason.TaskManagerClosing: |
// The application is being closed from the TaskManager. |
break; |
case CloseReason.UserClosing: |
// The user is closing the form through the UI. |
break; |
case CloseReason.WindowsShutDown: |
// Windows is closing the application because it is shutting down. |
break; |
} |
} |
If you need to handle the closings differently, you can now do so with the CloseReason.
Tuesday, September 2, 2008 3:01 AM
cheers,
i was trying do do this:
1 | private void Main_FormClosing(object sender, FormClosingEventArgs e) |
2 | { |
3 | switch (e.CloseReason) |
4 | { |
5 | case CloseReason.ApplicationExitCall: |
6 | MessageBox.Show("The Exit method of the Application class was called.", "Bye"); |
7 | break; |
8 | case CloseReason.FormOwnerClosing: |
9 | MessageBox.Show("The Owner form is closing.", "Bye"); |
10 | break; |
11 | case CloseReason.MdiFormClosing: |
12 | MessageBox.Show("The parent form is closing.", "Bye"); |
13 | break; |
14 | case CloseReason.None: |
15 | MessageBox.Show("Unknown closing reason", "Bye"); |
16 | break; |
17 | case CloseReason.TaskManagerClosing: |
18 | MessageBox.Show("The Application is being closed by taskmanager.", "Bye"); |
19 | break; |
20 | case CloseReason.UserClosing: |
21 | MessageBox.Show("Ui close.", "Bye"); |
22 | break; |
23 | case CloseReason.WindowsShutDown: |
24 | MessageBox.Show("Windows is shutting down.", "Bye"); |
25 | break; |
26 | } |
27 | } |
but the application is simply closing, how woudl i get this to work?
Tuesday, September 2, 2008 4:19 AM
You can trap Application.ApplicationExit event?!
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
}
void Application_ApplicationExit(object sender, EventArgs e)
{
throw new Exception("The method or operation is not implemented.");
}
Viral
Tuesday, September 2, 2008 4:31 AM
Just writing the code in Program.cs, after the Application.Run() call is another way. You should fret about exceptions a bit though.
Hans Passant.
Tuesday, September 2, 2008 7:05 AM
nobugz said:
Just writing the code in Program.cs, after the Application.Run() call is another way. You should fret about exceptions a bit though.
Hans Passant.
how would i reference variables in main.cs?
Tuesday, September 2, 2008 7:11 AM
Change it before you call Application.Run(), set it back after it returns. No reason the methods couldn't be static either, you're changing only one resolution.
Hans Passant.
Tuesday, September 2, 2008 8:10 AM
sorry im a bit confused here,
basically this script stires your current res, then changes it, when the application is finished it should change the resoloution back,
how woudl i go about that?
heres the line to change it back:
Resolution.CResolution ChangeRes = new Resolution.CResolution(FixHeight, FixWidth); |