Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Bind windows form [X] with exit

Posted on 2009-04-02
7
Medium Priority
?
441 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
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!

 

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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 column That will then direct you to their download page. From that p…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

721 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