我如何编写一个C#方法来在单击按钮时清除一组复选框?

我如何编写一个C#方法来在单击按钮时清除一组复选框?,c#,checkbox,C#,Checkbox,新的C#问题 当我单击clear按钮时,我应该编写6个单独的方法来清除6组复选框(一次全部清除)。我知道如何使用单个复选框进行编码,但问题是我需要创建一个方法,然后单击“清除”按钮调用所有6个复选框。帮忙 我还没有任何与clearButton\u Click事件相关的内容 private void ClearOilLube { set {oilChangeCheckBox.Checked = false; lubeJobCheckBox.Check

新的C#问题

当我单击clear按钮时,我应该编写6个单独的方法来清除6组复选框(一次全部清除)。我知道如何使用单个复选框进行编码,但问题是我需要创建一个方法,然后单击“清除”按钮调用所有6个复选框。帮忙

我还没有任何与clearButton\u Click事件相关的内容

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

单击“清除”按钮时,只需调用上面创建的方法来清除数据

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();
}
编辑

你问题的语法有点奇怪。当它们应该是方法时,它们被编写成类似的属性。不需要“设置”或“获取”。让他们成为常规的助手方法

这只是在没有对编译器或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 = "";
}
你的方法(或者是属性?)写得很糟糕。我将假定您希望编写方法而不是属性

方法头采用以下形式:(简化)

因此,您的方法应该如下所示:

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

如果你仍然不明白,请在评论中告诉我

给你一个选择,尽管它更复杂:

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

这将取消选中所有,请记住这尚未测试,因此可能需要一些微调

这是WinForm还是WebForm?访问控件集合,并遍历它们。这是一个Windows窗体。迭代什么?点击
按钮调用所有清除方法?迭代意味着在列表/数组中循环。获取窗口/面板/其他控件上的所有控件,并检查它们是否与您的描述相匹配。您说过您知道如何逐个清除它们,只需调用用于清除单个控件的所有方法。嗯,这很简单…:我应该使用set还是。。。我也不认为我完全正确地设置了void方法。你的方法是最简单的。只是好奇为什么你认为更好的方法是迭代控件?写起来更快@但如果他有很多控制权(我怀疑他有),这显然不是最好的方法…我知道我的方法很糟糕。。。我想找出丢失的碎片。。。ie:private void ClearOilube(),然后在按钮单击事件中调用它。。。这本书在这一点上不是非常清楚,或者我很笨,只是把它读了一遍。你的方法可以更优雅一些。
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);
foreach(var checkbox in checkboxes)
     checkbox.Checked = false;