How can I write a C # method to clear a group of checkboxes when a button is clicked?

New issue with C # ...

I have to write 6 separate methods to clear 6 groups of checkboxes (all at once) when I click the clear button. I know how to encode it with separate checkboxes, but the problem requires me to create a method, and then call all 6 of them, by pressing the clear button. Help?

I have not yet associated anything with the clearButton_Click event.

    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;}
    }
+4
source share
3 answers

When you click the clear button, simply call the methods 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

. , . "" "". .

IDE, , , .

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 = "";
}
+4

( ?) . , .

: ()

[access modifier] {return value type} {method name} ([parameter list])

, :

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

onClick.

, ( ) .

private void ClearButtonClick (object sender, EventArgs e) {
    foreach (Control control in this.Controls) {
        if (control is CheckBox) {
            ((CheckBox)control).Checked = false;
        }
    }
}

, !

+4

, :

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

, , :

var checkboxes = new List<Checkbox>();
GetAllControlsOfType<Checkbox>(Page.Controls, checkboxes);

After that, it will contain Listall the checkbox controls. Then you can simply map them to a state objector simply follow these steps:

foreach(var checkbox in checkboxes)
     checkbox.Checked = false;

To clear all checkboxes, keep in mind that this has not been checked, so fine tuning may be required.

+2
source

Source: https://habr.com/ru/post/1599602/


All Articles