Cancel button not working? - c#

I am creating C#.net(4.0) application.This application is a Console Application and I am creating a Window Form in this application with two button start and cancel.
From the main Function I am calling the form :
Application.Run(new Form());
Now the form contains two buttons :
Start Button-On start button click I am doing some task.
Task1();
Task2();
Cancel Button-On Cancel button click I want to terminate application.
Application.Exit() //is called on Cancel Button click.
The problem is as I start the application and click cancel button application terminates, but when I press start button and then press cancel button the application doesn't terminate..

Related

How do I click the cross button (the cancel button) on the sub form and go to the main form?

I know how to close a sub form and go to main form by clicking "close" button under file menu bar, but I don't know how to cancel a subform and go to main form by clicking the cross button --"cancel" which is located on the upper right corner.
and then back to the main form
Update:
I have tried go to form property, under Event,there is an item called, FormClosing, write a click event funcation name,i.e cancel_Click, and then press enter. Then I can write code under cancel_click, but it rises another problem. Since I want use same code here as the one under "close" on the menu (both can redirect back to the main form), when I close on menu bar, it appears two main form. How can I fix this?
use Form.Closing event and then do what ever you want to do. link
Set the FormClosing event for the sub-form to go to the main form. Set the FormClosing event for the main form to exit the program.

WPF form controls(Next button, cancel button) gets disabled in background

I've a WPF page that contains next and cancel navigation to subsequent pages. Next button gets enabled based on certain validations. On click of another button in the page, another .exe gets opened and the 1st page goes to background. After finishing the tasks in opened exe, if the 1st page is not clicked, Next button and close button stay disabled. Once I click on the form, they get enabled.
I checked that if the 1st form is in background, CanExecuteChanged function gets called only when the form is clicked manually.
How to make the 1st form active again after the 2nd exe is closed?

Exit application by clicking button on mainform when already pop up form is running

Form2 is user confirmation window,i want to close the application by stop button on mainform,but currently form2 is active and there is no dependency between form2 and mainform.How to close application ?
You must be opening Form2 using ShowDialog() method. Instead of that use Show() method and on MainForm button's click event call Application.Exit() method
You can use...
// for WinForms application
if (System.Windows.Forms.Application.MessageLoop)
{
System.Windows.Forms.Application.Exit();
}
// for Console application
System.Environment.Exit(1);
So your problem is the parent window will not have focus/input until the modal window (the confirmation dialog) has finished.
So, to avoid this, just make the confirmation dialog using a custom window (instead of a common dialog / msgbox) and show it. This way if the user wants to click your main form they can (the confirmation will be on background, covered by the main window because of this).
it looks like you're showing the dialog as ShowDialog() or showing a built in MessageBox which is also a ShowDialog that needs to be closed in order for focus to return back to main window. So what you should do is create a custom Dialog and use dialog.Show() method. This will allow you to interact with your main window while the dialog is open and you can click on a button to close your application regardless of the dialog being open or not. Following code should do the closing work for you
Application.Current.ShutDown();

Stop button click event on other button click

how to access two button click method if first button doing work when we click second button?
I tried to access other button click event when first button click event is performing operation and I want to stop this execution and perform another operation
You are going to need to do some reading about asynchronous programming and then implement Cooperative Task Cancellation.
When you press the second button:
check that a task is in progress
if an operation is in progress, cancel the ongoing task and wait for it to 'complete' (cancel successfully)
run your new operation

Reset Focus in Windows 8 Apps

So I have a button that initiates a popup. In the popup I have it to detect when the enter key is pressed when the textbox is in focus and to submit the data and close the popup. However, when I close the popup the button that was pressed to initiate the popup regains focus and receives the key down event and thus opens the prompt again.
So I ask: Is there a way to reset focus from all elements?
You could try this for right after the popup closes.
FirstButton.Focus(Windows.UI.Xaml.FocusState.Unfocused);
Not sure how your code is set up but maybe do something like:
(assuming pop up is within the same window as the original button)
//in the button two clicked function
if (YourPopUp.Visibility == Visibility.Visible)
{
//do data sending
//collapse pop up
FirstButton.Focus(Windows.UI.Xaml.FocusState.Unfocused);
}

Categories

Resources