Solved

Bind windows form [X] with exit

Posted on 2009-04-02
7
433 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:csharp_learner
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks Cebik i got it working and i assigned the function to the FormClosing event.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now