Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Bind windows form [X] with exit

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses

971 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