Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating windows application in which i have one form i.e. employee details. If user enter data in form and click on Closed(X) button than message box display "You want to saved data or not ".and if user does not enter details and clicked on Closed(X) button than message box display"Are you sure want to exit".
Posted

Hi,

see this code as an example:
C#
public partial class Form1 : Form
{
   public Form1()
   {
      InitializeComponent();
      this.FormClosing +=new FormClosingEventHandler(Form1_FormClosing);
   }

   private void Form1_FormClosing(object sender, FormClosingEventArgs e)
   {
      if (e.CloseReason != CloseReason.UserClosing)
         return;

      if (String.IsNullOrEmpty(textBox1.Text))
      {
         if (MessageBox.Show("Are you sure you want to close the form?", "Warning", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
            e.Cancel = true;
      }
      else
      {
         if (MessageBox.Show("You want to save data or not?", "Warning", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
         {
            //Save data
         }
      }
   }
}
 
Share this answer
 
v2
Comments
Manohar Khillare 15-Aug-12 4:59am    
this code is not work when i click on close button
JF2015 15-Aug-12 5:01am    
Of course you need to subscribe to the closing event before using that code. I modified the answer to include that. See if that works now.
Manohar Khillare 15-Aug-12 5:07am    
thanks it works. i want to ask another question that if user want to save data he clicked on yes button of message box. after that user click on save button to saved data than form will closed automatically. how can i do that?
JF2015 15-Aug-12 5:12am    
Glad to hear that it works. Please accept and upvote the answer to mark it as solved. To close the form automatically after saving you could use the modified code as:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.FormClosing +=new FormClosingEventHandler(Form1_FormClosing);
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.UserClosing)
return;

if (bForceClose)
return;

if (String.IsNullOrEmpty(textBox1.Text))
{
if (MessageBox.Show("Are you sure you want to close the form?", "Warning", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
e.Cancel = true;
}
else
{
if (MessageBox.Show("You want to save data or not?", "Warning", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
{
e.Cancel = true;
}
}
}

bool bForceClose = false;

private void btnSave_Click(object sender, EventArgs e)
{
//Save data...

//Force automatically closing the app
bForceClose = true;
this.Close();
}
}
Manohar Khillare 15-Aug-12 5:27am    
but the code is not working. if i clicked on yes button than form get closed even i dont clik on save button.
How to save the whole application when user clicks the "X"button...???
Database should be updated...!!! "Commit" ill Work???
 
Share this answer
 
v2

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