Results 1 to 3 of 3

Thread: [RESOLVED] [2005] MDI and Childs problems

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    89

    Resolved [RESOLVED] [2005] MDI and Childs problems

    Hi,

    My English is not native, sorry.

    I have MDI form and Child form.
    I added "Form_Closing" event to the MDI code file. the following is the code:

    c# Code:
    1. private void frmMainMdi_FormClosing(object sender, FormClosingEventArgs e)
    2.         {
    3.             // Hold the MessageBox button pressed
    4.             DialogResult diaResult;
    5.             // Show Message to the User
    6.             diaResult = MessageBox.Show("Are you sure you want to cancel the setup?",
    7.                                         "Exit Setup",
    8.                                         MessageBoxButtons.YesNo,
    9.                                         MessageBoxIcon.Warning,
    10.                                         MessageBoxDefaultButton.Button2);
    11.            // Checking which button as been pressed
    12.            if (diaResult == DialogResult.No)
    13.            {
    14.                // CANCEL pressed, set focus to the program
    15.                e.Cancel = true;
    16.            }
    17.            else
    18.            {
    19.                // YES pressed, exit the program
    20.                this.Dispose();
    21.                Application.Exit();
    22.            }
    23.         }

    Now, in the child form i have CANCEL button that suppose to teminate the application if the user press YES.

    I added to the click event the following code:

    c# Code:
    1. private void btnCancel_Click(object sender, EventArgs e)
    2.         {
    3.           Application.Exit();
    4.         }

    And now I am having the problem,

    I am getting an error when I try to cancel the program and the error points to the cancel button, application.exit();
    the following is the VS2005 ERROR:

    System.InvalidOperationException was unhandled
    Message="Collection was modified; enumeration operation may not execute."
    Source="mscorlib"



    How can I terminate the program from runnning when pressing the cancel button?

    Thanks
    VBDC
    Last edited by vbdc; May 8th, 2007 at 07:14 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] MDI and Childs problems

    You need to get rid of this for a start:
    C# Code:
    1. else
    2. {
    3.     // YES pressed, exit the program
    4.     this.Dispose();
    5.     Application.Exit();
    6. }
    If the FormClosing event is raised it's because your form is closing. If it's closing that means that it is going to be disposed and the application will exit. You should not be disposing the form and exiting the application at that point.

    What you're doing is like you checking if the door is closing and, if it is, closing the door. That's not logical. I don't know whether that will fix your issue or not but it sure won't hurt. Fix that and then try again. If the issue persists then post back.

    Also, when asking for user confirmation of an action they requested you should be using OKCancel, which is the Windows convention, not YesNo.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    89

    Re: [2005] MDI and Childs problems

    jmcilhinney,

    Thanks, I did what you said and It works great.

    Thank you again.

    VBDC

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