Click here to Skip to main content
15,905,614 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI Guys.

i used this code in my program: Application.Exit();

but when i running program this error appears:
InvalidOperation Exeption was unhandled.
collection was modified;enumeration operation may not execute.

Can I ask if someone have an idea to how can i fix this error.
C#
public partial class frmexit : Form
    {
        public frmexit()
        {
            InitializeComponent();
        }

        private void btnyes_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnno_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
Posted
Updated 1-Dec-12 21:01pm
v2
Comments
[no name] 2-Dec-12 2:46am    
Where you've written this ?
siasalar 2-Dec-12 2:51am    
i made a form to question for exit( are you sure to exit??)
for buttonNo i wirte: close.this();
and for buttonYes i wirte: Application.Exit();
[no name] 2-Dec-12 2:53am    
Post your code as well..
siasalar 2-Dec-12 2:59am    
public partial class frmexit : Form
{
public frmexit()
{
InitializeComponent();
}

private void btnyes_Click(object sender, EventArgs e)
{

Application.Exit();

}

private void btnno_Click(object sender, EventArgs e)
{
this.Close();
}


}
Richard MacCutchan 2-Dec-12 7:31am    
You have still not explained why you think this code is causing the error that you see.

with this code the problem were fixed:
Application.ExitThread();
 
Share this answer
 
You're code is correct.. Go by this:
C#
private void button1_Click(object sender, EventArgs e)
        {
            var msg = MessageBox.Show("Exit", "", MessageBoxButtons.YesNo).ToString();

            if (msg == "Yes")
                Application.Exit();
            else
                return;
        }
 
Share this answer
 
v2
Comments
siasalar 2-Dec-12 4:02am    
i dont want use messagebox.show i want to make a new form to do this.
hi dear,

DialogResult a;
            a = MessageBox.Show("Are you sure to exit.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
            if (a == DialogResult.Yes)
            {
                Application.Exit();
            }
            else
            {
                this.Close();
            }
 
Share this answer
 
Comments
siasalar 2-Dec-12 4:11am    
Hi dear tnx for your responsibility.
i dont want use messagebox.show i want to make a new form to do this.
Hi Dear,

C#
DialogResult a;
a = MessageBox.Show("Are you sure to exit.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
if (a == DialogResult.Yes)
{
   Form1 frm = new Form1 ();
   frm.Show();
}
else
{
   // Continue
}
 
Share this answer
 
Comments
siasalar 2-Dec-12 6:27am    
:)look at to my question and comments.
azizulhoque.bd 3-Dec-12 0:27am    
please detail explain with code. I do not understand, what do you want ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900