1

everyone. I've run into such a problem. In my app, I show second form from my main form through ShowDialog method. In this form I have some textBox(s) to connect to the DB and a Connect button. If user clicks X, the app exit. But if user click "connect" - I connect to DB and also close my second form. To catch closing event I use FormClosing method where the app asks whether I'm about to close the app and if yes it exit. The problem is that when I click the button, FormClosing event fires and asks me if I want to exit. How to avoid it? I try to use sender, but it doesn't work.

Here is my code:

private void Connect_Click(object sender, EventArgs e)
{
    try
    {
         orcl.connect(userID.Text, Password.Text, comboTNS.Text);

         if (orcl.ifHasRows("select dbclass from setupdbversion where dbclass='SECURITY' and rownum=1"))
         {//my stuff
             this.Close();
         }

     }
     catch (Exception ex)
     {
          MessageBox.Show(ex.Message.ToString());
     };
}


private void SecConnForm_FormClosing_1(object sender, FormClosingEventArgs e)
{   
     MessageBox.Show(sender.ToString());
     if (e.CloseReason == CloseReason.UserClosing)
     {
          MessageBox.Show(sender.ToString());
          if (string.Equals((sender as Form).Name, @"SecConnForm")) //it doesn't work as in any cases the sender is my form, not a button (when i click on button of course)
          {
               if (MessageBox.Show(this, "Really exit?", "Closing...",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
                       == DialogResult.Cancel) 
                   e.Cancel = true;
               else
                   Application.Exit();

          }
          else
          {
               //other stuff goes..
          }
     }
}
1
  • "when i click the button", please elaborate which button you are talking about?
    – jparthj
    Mar 15, 2013 at 13:51

2 Answers 2

2

Form closing event will fire every time form is closed whether that is done by code or by user click.

What you need is something similar to this.

private boolean bFormCloseFlag = false;

private void Connect_Click(object sender, EventArgs e)
{
    try
    {
         orcl.connect(userID.Text, Password.Text, comboTNS.Text);

         if (orcl.ifHasRows("select dbclass from setupdbversion where dbclass='SECURITY' and rownum=1"))
         {//my stuff
             bFormCloseFlag = true;
             this.Close();
         }

     }
     catch (Exception ex)
     {
          MessageBox.Show(ex.Message.ToString());
     };
}


private void SecConnForm_FormClosing_1(object sender, FormClosingEventArgs e)
{   
     if (bFormCloseFlag = false)
     {
        MessageBox.Show(sender.ToString());
        if (e.CloseReason == CloseReason.UserClosing)
        {
          MessageBox.Show(sender.ToString());
          if (string.Equals((sender as Form).Name, @"SecConnForm")) //it doesn't work as in any cases the      sender is my form, not a button (when i click on button of course)
          {
               if (MessageBox.Show(this, "Really exit?", "Closing...",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
                       == DialogResult.Cancel) 
                   e.Cancel = true;
               else
                   Application.Exit();

          }
          else
          {
               //other stuff goes..
          }
       }
     }
}

This flag will simply check whether form is closed by 'X' button click or it is closed by your code.

0
0

The this.Close() triggers the closing type of "UserClosing" Possibly just hide the dialog instead of this.close()?

1
  • you could add comment to convey your message anyway :-)
    – jparthj
    Mar 15, 2013 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.