Reputation: 159
I know there's many answers closely to this one but I really want an option from someone experienced.
So, after fill in form, user have option to Save data and remain in form, save data and close form or save data and add new record.
For saving, saving and close its pretty simple, but for saving and add new record, I have some problems.
So far, I'm resetting all controls to its original state after save data.
Comboboxes to SelectedIndex = -1;
Textboxes to string.Empty;
Radioboxes to checked = false;
Checkboxes to checked = false;
DatetimeEdit to Values = null;
And this works for resetting controls in a small form.
Is there any other, faster and better way to achieve this goal?
Maybe closing and reopening Form?
All my controls, comboboxes fill and other needs are made in constructor. I do not load anything in Load Event.
Upvotes: 0
Views: 182
Reputation: 5986
In order to do it efficiently and reusebale its possible to do something like this, please see comments:
private void RollBackForm()
{
// put here all the containers the contain the controls, panel,groupbox the form itself etc...
Control[] Containers = { panel1, groupBox1, this };
// iterate trough all containers
foreach (Control container in Containers)
{
// check control type, cast it and set to default
foreach (Control childControl in container.Controls)
{
if (childControl is ComboBox)
{
((ComboBox)childControl).SelectedIndex = -1;
}
else if (childControl is TextBox)
{
((TextBox)childControl).Text = string.Empty;
}
else if (childControl is RadioButton)
{
((RadioButton)childControl).Checked = false;
}
else if (childControl is CheckBox)
{
((CheckBox)childControl).Checked = false;
}
}
}
}
Upvotes: 1