Solved

Bind windows form [X] with exit

Posted on 2009-04-02
7
438 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
Independent Software Vendors: 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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 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.

749 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