Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write a C# method to clear a group of checkboxes upon button click?

Tags:

c#

checkbox

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;}
    }
like image 597
CraigM Avatar asked Dec 24 '22 16:12

CraigM


2 Answers

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 = "";
}
like image 113
J Blaz Avatar answered May 18 '23 13:05

J Blaz


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!

like image 32
Sweeper Avatar answered May 18 '23 12:05

Sweeper