Solved

Bind windows form [X] with exit

Posted on 2009-04-02
7
436 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

815 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

12 Experts available now in Live!

Get 1:1 Help Now