Results 1 to 7 of 7

Thread: Cancel Close Problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188

    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.

  2. #2
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188
    geez that looks like a bit much for something so small ;\

  4. #4
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    in your main mdiform ( ie: the Parent ) ...
    VB Code:
    1. [Color=Blue]public[/color] [Color=Blue]bool[/color] ClosingForm=[Color=Blue]false[/color];
    2.        [Color=Blue]protected[/color] [Color=Blue]override[/color] [Color=Blue]void[/color] WndProc([Color=Blue]ref[/color] Message m)
    3.        {
    4.        [Color=Blue]switch[/color](m.Msg)
    5.        {
    6.            [Color=Blue]case[/color] 161 :[Color=green] /// click on control box.[/color]
    7.        [Color=Blue]if[/color](m.WParam.ToInt32()==20)[Color=green]/// click on close X[/color]
    8.         {
    9.              ClosingForm=[Color=Blue]true[/color];
    10.         }
    11.         [Color=Blue]break[/color];
    12.         }
    13.         [Color=Blue]base[/color].WndProc([Color=Blue]ref[/color] m);
    14.         }
    in your mdiChild's closing event ....
    VB Code:
    1. [Color=Blue]private[/color] [Color=Blue]void[/color] Form2_Closing([Color=Blue]object[/color] sender, System.ComponentModel.CancelEventArgs e)
    2.         {
    3.              Form1 fMain=(Form1)[Color=Blue]this[/color].MdiParent;
    4.             [Color=Blue]if[/color]([Color=Blue]this[/color].MdiParent.MdiChildren.Length==1)
    5.             {
    6.                 [Color=Blue]switch[/color](fMain.ClosingForm)
    7.                 {
    8.                     [Color=Blue]case[/color] [Color=Blue]false[/color] :
    9.                        e.Cancel=[Color=Blue]true[/color];
    10.                         [Color=Blue]break[/color];
    11.                     [Color=Blue]case[/color] [Color=Blue]true[/color] :
    12.                         e.Cancel=[Color=Blue]false[/color];
    13.                         [Color=Blue]break[/color];
    14.                 }
    15.             }
    16.         }
    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]

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188
    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

  6. #6
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    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]

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188
    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
  •  



Click Here to Expand Forum to Full Width