Reputation: 857
I'm looking for an efficient way to clear my winform after data has been submitted. Currently I use a static method which accepts a control as a parameter and then loops through the control(s), determines the type of control and then clears the contents of the control. It works quite well but the problem I'm having is with the control events. When I clear the control, the control event fires and since there is a data type conversion present in the event (int.Parse for example) it causes an error because once the control is cleared, the control event is fired and an empty string is passed to int.Parse. Aside from using a bunch of if statements or int.TryParse in the event from the control to prevent the data conversion from happening, is there a better way to clear a control and not trigger it's event in the process?
Thanks
Here is an example of my control event and the clear method:
private void Quantity_TextChanged(object sender, EventArgs e)
{
presenter.Quantity = int.Parse(Quantity.Text);
}
public static void ClearForm(Control.ControlCollection controls)
{
foreach (Control ctrl in controls)
{
if (ctrl.GetType() == typeof(TextBox))
{
((TextBox)ctrl).Text = "";
}
if (ctrl.GetType() == typeof(ComboBox))
{
((ComboBox)ctrl).SelectedIndex = 0;
}
if (ctrl.GetType() == typeof(DateTimePicker))
{
DateTimePicker datePicker = ctrl as DateTimePicker;
//datePicker.Format = DateTimePickerFormat.Custom;
datePicker.CustomFormat = "dd-MMM-yyyy";
datePicker.Value = DateTime.Today;
}
if (ctrl.HasChildren)
{
ClearForm(ctrl.Controls);
}
}
}
Upvotes: 0
Views: 82
Reputation: 34
While it is tempting to use the solution you are describing, you really need to protect your inputs, or you will be throwing exceptions when the user does unexpected things. I would go back to your event handlers and either guard against "", or use tryparse, as you stated.
The other option, alluded to in the first answer is to have a couple methods, one unhooks all you events, the other hooks them back up. You bracket your call to Clear with those calls
UnHook();
Clear();
ReHook();
This will handle what you want to do, but leaves you having to keep your event code up to date by hand in two places.
I think the best solution is to just gate the input errors in your events.
Upvotes: 1
Reputation: 26209
Solution1 : You just need to Unwire
the EventHandlers
Subscribed to the control before clearing the contents
and Wire the EventHandlers
back after clearing it.
Solution 2:
You can just return from function before parsing the integer if it is EMPTY
asbelow:
private void Quantity_TextChanged(object sender, EventArgs e)
{
if(String.IsNullOrWhiteSpace(Quantity.Text))
return;
presenter.Quantity = int.Parse(Quantity.Text);
}
Solution 3: To avoid any Exceptions for invalid or EMPTY
values you can use int.TryParse()
method asbelow:
From MSDN : int.TryParse()
Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded.
private void Quantity_TextChanged(object sender, EventArgs e)
{
int quantity;
if(int.TryParse(Quantity.Text,out quantity))
presenter.Quantity = quantity.ToString();
}
Upvotes: 5