Windows Develop Bookmark and Share   
 index > Windows Forms General > how to use canel button in win application
 

how to use canel button in win application

hello, sorry I am just very new to win application so i have few questions.

I have 3 buttons in a win form.
1) to do work, 2button to Cancel 3button is to close the win application window.

so how would I code to Cancel and close the window. keeping in mind the user may click on the close button while the application still running, so the application doesn't generate an error.

thanks.

boobyy  Friday, January 11, 2008 6:27 AM
well this is how it is. I am touching the windows application first time so sorry to all.
this is how it is.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//the process thaking long time to get answer 3 or 4 minutes and during that time
//user can't do anything. just wait for the process. The Terminate.Exit() is working it closing the application
//but if the process is in act, then the Windows Not Responding error occur .

label1.Text = DateTime.Now.ToString();
double sum = 0;
Class1 TestClass = new Class1();
sum= TestClass.GetResults();
lblResult.Text = sum.ToString("N");



}

private void close_Click(object sender, EventArgs e)
{
//when click here , then the process should stop , but window form remains there.

}

private void quite_Click(object sender, EventArgs e)
{
// when click the windows form should be abanded from the screen
}
}
}
boobyy  Friday, January 11, 2008 8:51 PM
Hi,

Closing an application is as easy as calling Application.Exit(); When you want to be able to cancel your application doing something, we'll have to now what it is you are trying to cancel. Could you elaborate a bit more on that?

The 2 scenarios you talk about are easily constructed:

1. Cancel the working of the application (for the cancel button)

2. Cancel the working of the application and close the application (for the close button)

As you can see, the first part of those two are exactly the same. So you should only write it once Wink

Hope this helps.

Rick van den Bosch  Friday, January 11, 2008 7:46 AM
Thanks for your answer. : )

sorry for not difining my question clearly.

Lets say, I click a button "GetData" , it is a huge data .
once I click GetData and now I am waiting for it to reply, i thought well cancel it. So when I click the cancel Button, the process ends, but I am still albe to do other task.

2nd thing is I am done with my work and now I am going to close the window. Like once u r done with your MS.Word and then close it it. So the third button "Close " would do that.

I hope it make sence.
boobyy  Friday, January 11, 2008 2:38 PM

I tried to use the Application.Exit();
It is working fine when I want to close the application (form disappea , this is one of the thing that I want.)
however, only when the process ended.

If i click the Exit button, while the process still in progress ( 3 minutes), application freezes there.

my 2nd question still remains there , how to end the process by clicking a button without close the application. But only the processs.
boobyy  Friday, January 11, 2008 5:20 PM

By GUI Standard:

Yes, No, OK, or Cancel buttonshould close the window/form, so a Close button is not needed.

In your scenario if the first 2 buttons are Start and Stop, then a Close button makes sense.

JRQ  Friday, January 11, 2008 5:46 PM

The best way to do Cancel the operation is by doing the operation in the BackgroundWorker.

Look for it and you you will get your answers.

Fábio Franco  Friday, January 11, 2008 7:01 PM
Second that, for this second question, he should 'cancel' the getdata then close the form.

Fábio wrote:

The best way to do Cancel the operation is by doing the operation in the BackgroundWorker.

Look for it and you you will get your answers.

H. _冬_ Tony  Friday, January 11, 2008 7:07 PM
well this is how it is. I am touching the windows application first time so sorry to all.
this is how it is.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//the process thaking long time to get answer 3 or 4 minutes and during that time
//user can't do anything. just wait for the process. The Terminate.Exit() is working it closing the application
//but if the process is in act, then the Windows Not Responding error occur .

label1.Text = DateTime.Now.ToString();
double sum = 0;
Class1 TestClass = new Class1();
sum= TestClass.GetResults();
lblResult.Text = sum.ToString("N");



}

private void close_Click(object sender, EventArgs e)
{
//when click here , then the process should stop , but window form remains there.

}

private void quite_Click(object sender, EventArgs e)
{
// when click the windows form should be abanded from the screen
}
}
}
boobyy  Friday, January 11, 2008 8:51 PM

Although words are just words, it is really a very good coding practice if you stick with their meaning. And if a certain word has an implied behavior like OK or Cancel (accept or dismiss changes then closes the window), it is a good idea to follow the standard.

I suggest:

button1 -> Start

close -> Stop

quite -> Close

JRQ  Friday, January 11, 2008 9:33 PM

You can use google to search for other answers

Custom Search

More Threads

• ListBox sorting
• ComboBox Raise ShowItemsList Event
• Split Spool File
• opning Word document in WebBrowser object
• .NET ASCII printer support
• Dockable window with a Pin... How?
• How to restrict a mdi child locations or any forms location and size?
• Logic of the Checked ListBox's cheking?
• WebBrowser control defect
• More than one dynamic checkbox