Validating Data In A WinForm - c#

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();
}
}

Related

Detecting which button was clicked when closing a winform?

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

In a textbox validating event, how to ignore clicks to a certain button?

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.

How do I make a text box in a form not visible until after I click compute in C#?

So in the beginning, the user has to input data but in the form. It has textboxes and labels where the answers to the calculations go. I have it so that when it clicks, it computes certain labels and textboxes show up and when the user clicks reset they disappear using:
txtTaxesPaid.Visible = true;
txtTaxesPaid.Visible = false;
My problem is that in the beginning the ones that are not visible. When I click reset they show up. How do I make them not visible in the very beginning?
You can set Textboxes Visible property from Properties window in the designer.Or you can do it in Form Load event programatically:
private void Form1_Load(object sender, EventArgs e)
{
txtTaxesPaid.Visible = false;
}
If you don't see Form Load method just double click your Form in the designer and you will see, or find Load event from Properties > Events window.
Option 1: (shortest and most straight forward)
In VS designer of your form - locate those controls and set the visible property to false.
Option 2:
In your form_load event you can set the initial state of your controls.
Option 3:
You can force to invoke the method of the button which already contain the reset commands from your form_load event.
Option 4: (I think is the preferable choice)
- Create a method for reset..
- call the method from form_load or from your "reset" button or anywhere else you want.
private void ResetControls()
{
txtMyControl.Visible=false;
//here comes more logic for what to do upon reset.
}
private void form_load(...
{
ResetControls();
}
private void btnReset_Click(...
{
ResetControls();
}

How to keep the focus back to form when clicking on OK button in windows forms?

I have a windows form with multiple controls and OK and Cancel buttons. In the Click event handler of OK button, it checks to see if there are errors on the form. I need the form to stay open if there are errors. But the code below is not working. If there are errors, it is closing the form and returning to the caller. And I have the below two lines to invoke and show the form.
PtForm paymentForm = new PtForm();
ptForm.ShowDialog();
private void btnOk_Click(object sender, EventArgs e)
{
this.ValidateChildren(ValidationConstraints.Visible);
string ErrorString = GetValidationErrors();
MessageBox.Show(ErrorString, "Errors");
if (!string.IsNullOrEmpty(ErrorString))
{
return;
}
//Processing
}
Thanks for any help.
There's nothing in this code that will close the form. Therefore, the culprit must be outside this code.
Did you set your OK button's DialogResult property to DialogResult.OK? That would explain why the form is closing. AFAIK, if you set DialogResult on the button, that's what happens -- there's no way to veto it in code.
So in that case, you would need to go back to the designer and set the button's DialogResult back to None. Then at the end of your btnOk_Click method, once you've validated all the input and decided that it's safe to close the dialog, add a line that sets your Form's DialogResult property to OK.
Remove DialogResult property of a button-i.e. set it to None.

How to stop textbox leave event firing on form 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

Categories