|
-
Nov 30th, 2003, 01:31 AM
#1
Thread Starter
Addicted Member
Cancel Close Problem
Okay my problem is this I want stop the user from closing the child form of my problem if there is only one open, but if I set the cancel event to true in the onclosing event it will not allow me to close the program. My first thought was the set a boolean value to check to see if the parent for was being closed but it did not work either. So basically im stumped at the moment.
-
Nov 30th, 2003, 11:48 PM
#2
Hyperactive Member
Try a search using: single program instance mutex synchronization object. Read over a
few of the C# hits you get for this and then go steal and seriously study the code by
Michael Potter at: http://www.codeproject.com/csharp/cssingprocess.asp
One of the reasons you may want to limit the number of your parent form instances will
be so you can use runtime InteropServices to communicate between your multiples of forms.
I'm not saying you must limit your parent form; in fact, it could be a kewl study to do
it without any limit; but that's another story...
Once (assuming) you grasp how this is done with the above technique, it will hopefully
not be too big a leap to see how you can move from checking for a single instance in
Main() with RaiseOtherProcess to doing it when each of your forms are closing with:
Code:
public class frmMyBigForm : System.Windows.Forms.Form
{
.
.
.
private void frmMyBigForm_Closing( object sender, System.ComponentModel.CancelEventArgs e )
{
try
{
// if(MessageBox.Show("Do you stop this?", "My Brian Hurts", MessageBoxButtons.YesNo) == DialogResult.Yes)
// e.Cancel = true; // Cancel form closing event
using(SingleProgramInstance spi = new SingleProgramInstance("WackOnTheHeadMakesLightsInTheSky"))
{
spi.RaiseAlternateOzzieProcess();
}
}
catch ( Exception error )
{
Console.WriteLine( "frmMyBigForm_Closing Error: " + error.Message );
}
}
FWIW, this should give you a serious lead into how to answer your form on top question too. HTH.
-
Dec 2nd, 2003, 12:35 PM
#3
Thread Starter
Addicted Member
geez that looks like a bit much for something so small ;\
-
Dec 2nd, 2003, 04:57 PM
#4
in your main mdiform ( ie: the Parent ) ...
VB Code:
[Color=Blue]public[/color] [Color=Blue]bool[/color] ClosingForm=[Color=Blue]false[/color];
[Color=Blue]protected[/color] [Color=Blue]override[/color] [Color=Blue]void[/color] WndProc([Color=Blue]ref[/color] Message m)
{
[Color=Blue]switch[/color](m.Msg)
{
[Color=Blue]case[/color] 161 :[Color=green] /// click on control box.[/color]
[Color=Blue]if[/color](m.WParam.ToInt32()==20)[Color=green]/// click on close X[/color]
{
ClosingForm=[Color=Blue]true[/color];
}
[Color=Blue]break[/color];
}
[Color=Blue]base[/color].WndProc([Color=Blue]ref[/color] m);
}
in your mdiChild's closing event ....
VB Code:
[Color=Blue]private[/color] [Color=Blue]void[/color] Form2_Closing([Color=Blue]object[/color] sender, System.ComponentModel.CancelEventArgs e)
{
Form1 fMain=(Form1)[Color=Blue]this[/color].MdiParent;
[Color=Blue]if[/color]([Color=Blue]this[/color].MdiParent.MdiChildren.Length==1)
{
[Color=Blue]switch[/color](fMain.ClosingForm)
{
[Color=Blue]case[/color] [Color=Blue]false[/color] :
e.Cancel=[Color=Blue]true[/color];
[Color=Blue]break[/color];
[Color=Blue]case[/color] [Color=Blue]true[/color] :
e.Cancel=[Color=Blue]false[/color];
[Color=Blue]break[/color];
}
}
}
hope it helps
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Dec 4th, 2003, 10:56 AM
#5
Thread Starter
Addicted Member
hey stranger, lol.
Thanks, one question tho does this work if the user clicks the icon in the left of the application and clicks close??? Sorry I cant test this atm Im at work
-
Dec 4th, 2003, 11:21 AM
#6
hi dude , that catches the windows messages for clicking on the toolbox , then on the close button X only , but if you catch the messages ( Console.WriteLine ( m.Msg.ToString()) ; ) etc... then you should be able to get any of the extra numerics you want for the events like click on icon area.
btw, hows life treating you without msn to terrorize?
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Dec 5th, 2003, 12:51 PM
#7
Thread Starter
Addicted Member
I never left I'm still there and kicking, I've been on msn for years and will be for many more.
* ps. Check ur PMs on the board I have another question
Last edited by Tewl; Dec 5th, 2003 at 12:59 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|