Reputation: 65
New C# problem...
I'm supposed to write 6 separate methods to clear 6 groups of checkboxes (all at once) when I click the clear button. I know how to code it with individual checkboxes, but the problem asks for me to create a method and then call all 6 of them upon clicking the clear button. Help?
I have nothing associated with the clearButton_Click event yet.
private void ClearOilLube
{
set {oilChangeCheckBox.Checked = false;
lubeJobCheckBox.Checked = false;}
}
private void ClearFlushes
{
set {radiatorFlushCheckBox.Checked = false;
transFlushCheckBox.Checked = false;}
}
private void ClearMisc
{
set {inspectionCheckBox.Checked = false;
replaceMufflerCheckBox.Checked = false;
tireRotationCheckBox.Checked = false;}
}
private void ClearOther
{
set {partsCostInputTextBox.Text = null;
laborInputTextBox.Text = null;}
}
private void ClearFees
{
set {servicesLaborDispLabel.Text = null;
partsDispLabel.Text = null;
partsTaxDispLabel.Text = null;
totalFeesDispLabel.Text = null;}
}
Upvotes: 4
Views: 1086
Reputation: 271575
Your methods (or is that properties?) are written quite badly. I will just assume that you want to write methods not properties.
Method headers take the following form: (simplified)
[access modifier] {return value type} {method name} ([parameter list])
So your methods should look like this:
private void ClearOilLube ()
{
oilChangeCheckBox.Checked = false;
lubeJobCheckBox.Checked = false;
}
private void ClearFlushes ()
{
radiatorFlushCheckBox.Checked = false;
transFlushCheckBox.Checked = false;
}
private void ClearMisc ()
{
inspectionCheckBox.Checked = false;
replaceMufflerCheckBox.Checked = false;
tireRotationCheckBox.Checked = false;
}
private void ClearOther ()
{
partsCostInputTextBox.Text = null;
laborInputTextBox.Text = null;
}
private void ClearFees ()
{
servicesLaborDispLabel.Text = null;
partsDispLabel.Text = null;
partsTaxDispLabel.Text = null;
totalFeesDispLabel.Text = null;
}
And then, you can call these methods one by one in the onClick method.
However, a better approach is to loop through (or iterate) through the Controls of the form.
private void ClearButtonClick (object sender, EventArgs e) {
foreach (Control control in this.Controls) {
if (control is CheckBox) {
((CheckBox)control).Checked = false;
}
}
}
If you still don't understand, tell me in the comments!
Upvotes: 4
Reputation: 11480
To give you an alternative, though it is more complex:
private void GetAllControlsOfType<TControl>(TControl collection, List<TControl> container) where TControl : Control
{
foreach(Control control in collection)
{
if(control is TControl)
container.Add(control);
if(control.HasControls())
GetAllControlsOfType<TControl>(control.Controls, container);
}
}
Then to utilize, you would simply do:
var checkboxes = new List<Checkbox>();
GetAllControlsOfType<Checkbox>(Page.Controls, checkboxes);
After that is executed it will contain a List
of all the checkbox controls. Then you could simply map them to the state of the object
or you can simply do the following:
foreach(var checkbox in checkboxes)
checkbox.Checked = false;
That would uncheck all, keep in mind this hasn't been tested so it may need some fine tuning.
Upvotes: 2
Reputation: 783
When the clear button is clicked just call the methods you created above to clear the data.
public void clearButton_Click(Object sender, EventArgs e)
{
ClearOilLube();
ClearFlushes();
ClearMisc();
ClearOther();
ClearFees();
// May be required to be called to ensure the UI is up to date.
Update();
}
Edit
The syntax on your question is a little strange. They are written kind of like properties when they should be methods. No 'set' or 'get' is needed. Just make them regular helper methods.
This is just typed in no testing on a compiler or IDE but what I can see is fixed.
private void ClearOilLube()
{
oilChangeCheckBox.Checked = false;
lubeJobCheckBox.Checked = false;
}
private void ClearFlushes()
{
radiatorFlushCheckBox.Checked = false;
transFlushCheckBox.Checked = false;
}
private void ClearMisc()
{
inspectionCheckBox.Checked = false;
replaceMufflerCheckBox.Checked = false;
tireRotationCheckBox.Checked = false;
}
private void ClearOther()
{
partsCostInputTextBox.Text = "";
laborInputTextBox.Text = "";
}
private void ClearFees()
{
servicesLaborDispLabel.Text = "";
partsDispLabel.Text = "";
partsTaxDispLabel.Text = "";
totalFeesDispLabel.Text = "";
}
Upvotes: 4