I have this event handler in my winform:
private void SaveToDXF_FormClosing(object sender, FormClosingEventArgs e)
{
// Make sure the user has selected at-least one layer
if (listBoxLayers.SelectedItems.Count == 0)
{
_AcAp.Application.ShowAlertDialog("Please select one or more layers.");
e.Cancel = true;
}
}
Originally I was using the OK button click handler but I quickly found out that there did not seem to be a way to cancel out of actually closing the form. Then I read on SO a suggestion to use FormClosing. This works fine but ...
If the user closing the form by pressing the Cancel button this event still fires. That makes sense. But I only want to perform this validation check and cancel closing the form if they click the OK button.
How do we do this?
Sounds like you can check the this.DialogResult in the closing handler: it will be different depending which button was clicked (whatever you set as the DialogResult in the properties grid).
e.g. "don't do the check if they are cancelling" could be like
if (this.DialogResult != DialogResult.Cancel && listBoxLayers.SelectedItems.Count == 0)
If you have more checks to do it might be simple to just "if cancelling then return" as the event handler first line
Related
This may seem like a strange request, but I want to cancel the normal behavior of a dropdown control regarding opening or closing the list. I have several dropdowns on a form and it seemed tedious to have to hit the arrow exactly to open the list. I added code in the click event handler to open or close the list depending on its current state:
private void customerCodeComboBox_Click(object sender, EventArgs e)
{
if (customerCodeComboBox.DroppedDown == false)
customerCodeComboBox.DroppedDown = true;
else
customerCodeComboBox.DroppedDown = false;
}
This works just fine unless you actually click on the dropdown arrow. Then it opens and closes immediately since it fires the normal open and then my code closes it! :(
Is there a way to cancel the normal behavior? e.Cancel was not available in the EventArgs. Is there a better way to open the list when a mouse clicks anywhere in the field?
I have a modal form (login) which I want that on closing it using "X" button, force application to exit.
I tried this:
private void Login_Deactivate(object sender, EventArgs e)
{
Application.Exit();
}
this cause my application to exit on pressing login button (closing). but I want exiting only on "X" button of window.
How can I do it?
Get rid of the Deactivate event, you don't need it.
The issue here is that you cannot tell that just the "X" button was pressed. What you really do is track all the other conditions that cause form close, and react when it's not one of those.
You should leverage the DialogResult enum, and allow the calling application to react, instead of trying to do it all in the Login form.
In your Login form constructor, set the property to None:
public Login()
{
DialogResult = DialogResult.None;
}
Then, in your button handler for "OK", set the property: DialogResult = DialogResult.OK.
If you have a specific Cancel event, you can set DialogResult = DialogResult.Cancel.
Ultimately, you could have a FormClosing event in the Login form, in which you can check this value and react. But more importantly, your calling application can check the result from the login form and react there as well.
I think you should use this.Parent.Application.Exit(), it kills whole application.
Instead of Deactivate event, subscribe to the "FormClosing" event of the "Login" form. Write the same code which you have written.
Keep the Deactivate event only if it is required to close the Application when you click outside login form.
The problem is this:
I have a form that accepts a TextBox text and 2 buttons, one to save the text into the database and the other one to cancel and close the form. The form also has an ErrorProvider that's used in the TextBox.Validating event like this:
private void txtNombre_Validating(object sender, CancelEventArgs e)
{
string errorMsg;
if (!ValidateText(txtNombre.Text, out errorMsg))
{
// Cancel the event and select the text to be corrected by the user.
e.Cancel = true;
txtNombre.Select(0, txtNombre.Text.Length);
// Set the ErrorProvider error with the text to display.
this.errorProvider1.SetError(txtNombre, errorMsg);
}
}
It does what is expected, to not let the user to do anything in the form until the required field is filled, except for 1 thing: to let the user close the form via the Cancel button.
I tried comparing the object sender to the button cmdCancel but it's not possible because the sender is always the TextBox.
Any advice as to how to ignore the Validating event when the user clicks on cmdCancel?
Look at the property Control.CausesValidation. Set it to false for your cancel button.
Set CausesValidation to false on the Cancel button.
I have created a dialog box in my WinForms application. This has many text boxes and ok/cancel buttons. When the user clicks ok, I only want the dialog box to close if all entries are valid. I can see how to do this with the "Validating" events for each control separately. That is fine. But these only seem to fire when a control loses focus. However, empty text boxes in my dialog are also invalid input which means the user may never have focused on that control. I would prefer to just validate all controls on clicking OK.
I can't work out how to do this though. Overriding the onclick of the OK button doesn't seem to have an option for stopping the window from closing. The Form IsClosing event does by setting Cancel = true. But this doesn't seem to be able to distinguish between whether the OK or Cancel button is clicked. Obviously if the cancel button is clicked I don't care about validation and want to allow the form to close regardless.
What is the best approach for doing this?]
Update:
I already had CausesValidation set to true on both my form and ok button but my validation event does not get fired when I click the ok button. I mention this as it was suggested as a solution below.
Please select the form > Set the property CausesValidation to true
Select OK button and again set property CausesValidation to true
and then it will take care of all the validations.
Important points:
1) You must mention e.Cancel=true in all the validating eventhandlers
2) If your buttons are in panels then you must set panels (or any parent control's) CausesValidation property to true
Edit:
3) Validate fires just before loss of focus. While pressing Enter will
cause the default button to Click, it doesn't move the focus to that
button, hence no validation event will be fired if you have set forms AcceptButton Property to OK button
First make sure to cancel the validation when any of the textboxes have validation errors. For example:
private void nameTextBox_Validating(object sender, CancelEventArgs e) {
if (nameTextBox.Text.Length == 0)
{
e.Cancel = true;
return;
}
}
Now add the following code to the beginning of the ok button action:
if (!ValidateChildren())
return;
This will trigger the validation event for all controls on the form,
You can also use this simple code. just introducing a simple Boolean variable named hasError can do the job.
public partial class Form1 : Form
{
private bool hasError;
public Form1()
{
InitializeComponent();
}
private void OkBtn_Click(object sender, EventArgs e)
{
errorProvider1.Clear(); hasError=false;
if (ValidateTxt.Text.Length == 0)
{
errorProvider1.SetError(ValidateTxt, "must have a value");
hasError=true;
}
if (!hasError)
{
//Do what you want to do and close your application
Close();
}
}
private void CancelBtn_Click(object sender, EventArgs e)
{
Close();
}
}
using c# winforms vs2008
I've got a textbox on a form with a method being called from the textBox1_Leave event. The method takes the contents of the textbox in question and populates other textboxes based on the contents.
My problem is that is the user has focus on the text box then clicks the button to close the form (calling this.close) then the form does not close because the textbox leave event gets fired.
If the textbox does not have focus on form close then the form closes fine.
If however a user closes the form by clicking the little X close icon in the top corner the it closes fine all the time with out the textbox leave event being fired.
How can I duplicate the X close functionality so that I can always close the form without the textbox leave event being fired?
The simplest solution is going to be to check which control is actually focused before doing your post-processing - but you can't do it in the Leave handler, because the focus will still be on the text box at that point.
Instead, you need to move your logic to the LostFocus event, which is not in the designer. You'll have to wire it up at runtime:
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
}
private void textBox1_LostFocus(object sender, EventArgs e)
{
if (closeButton.Focused)
return;
// Update the other text boxes here
}
}
The LostFocus event happens to fire after the new control receives focus.
Clarification - you might find that it works by putting this logic in the Leave event - if the focus is changed by the mouse. If the keyboard is used instead, you'll get the wrong behaviour. LostFocus is reliable in both cases - the focused control will always be the "new" control. This is documented on MSDN: Order of Events in Windows Forms.
Incidentally, the reason why you're not having this problem with the "red X" is that the X is not actually a control that can receive focus, it's part of the window. When the user clicks that, it's not causing the text box to lose focus, and therefore isn't causing the Leave event to fire.
Another approach:
Use the textbox's validating event instead of it's leave event, then change the button's CausesValidation property to false. You will also have to set the textbox to not cause validation in the button's click event so the validating event will not fire when the form is closing (thanks to #Powerlord for pointing this out).
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.CausesValidation = false;
this.Close();
}
You could also handle the FormClosing event and make sure the e.Cancel argument does not get set to true by the validating events on the other controls on the form. I think they will be fired off before the FormClosing event.
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = false;
return;
}
}
you can check to see which control has just got focus.
private void textBox1_Leave(object sender, EventArgs e)
{
if (btnClose.Focused)
return;
// go from here
}
Just check if the form owning the textbox is disposing? If it's getting closed, it's disposing. If it's disposing you could simply end the pesky 'leave' event without doing anything. I didn't check it and forgive me, I'm choked on a project of my own so and I was searching myself, so I don't think I'll have time for that.
private void GuiltyTextBox_Leave(object sender, EventArgs e) {
Form formOwningTheTextBox=(Form)((Control)sender).TopLevelControl;
if (formOwningTheTextBox.Disposing || formOwningTheTextBox.IsDisposed) return;
.......
}
I just believe this is going to work with minimum effort and wanted to send a quick answer before I resume searching my own answer.
Write Following line of code in text box leave event on top
if me.closing then
return
end if