Solved

Bind windows form [X] with exit

Posted on 2009-04-02
7
439 Views
Last Modified: 2013-12-14
Hi I have a window form exit function which checks for text change before exiting the application.But I found out the windows [X] button on the top right ignores this and closes the apllication.
I was wondering if i could bind the both together so no matter how the user wants to exit the application it'll prform the text change check.
0
Comment
Question by:csharp_learner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 6

Expert Comment

by:Cebik
ID: 24057282
Hi

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    DialogResult res = MessageBox.Show("Are you shure?", "?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
    if (res != DialogResult.Yes)
        e.Cancel = true;
}

Open in new window

0
 
LVL 6

Expert Comment

by:Cebik
ID: 24057292
It will help you..
Change 3 line into your check function and use e.Cancel = true if you don't won't close application.
0
 
LVL 6

Expert Comment

by:Cebik
ID: 24057356
by the way If you needed...
If you wan't check/refresh databindings object before close..


form1.BindingContext[yourClass].EndCurrentEdit();

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:csharp_learner
ID: 24057366
sorry but can't seem to get it working, it still closes when i click on [X]
This is my normal close function how can i link both together?
Thanks for the help.
        private void MainExit_Click(object sender, EventArgs e)
        {
            if (Box1.Text != Box1Value | Box2.Text != Box2Value)
            {
                MessageBox.Show("cannot close");
                return; }
            this.Close();
        }

Open in new window

0
 
LVL 6

Accepted Solution

by:
Cebik earned 250 total points
ID: 24057524
Try this..

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (Box1.Text != Box1Value | Box2.Text != Box2Value)
    {
          MessageBox.Show("cannot close");
          e.Cancel = true;
    }
}
 
private void MainExit_Click(object sender, EventArgs e)
{
       this.Close();
}

Open in new window

0
 
LVL 6

Expert Comment

by:Cebik
ID: 24057535
Offcourse add function Form1_FormClosing into form closing event in the Form1Designer..
or programaticly


public Form1()
{
    InitializeComponent();
    this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
}

Open in new window

0
 

Author Comment

by:csharp_learner
ID: 24057762
Thanks Cebik i got it working and i assigned the function to the FormClosing event.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question