【问题标题】:clear each thing after doing achieving a specific task完成特定任务后清除每件事
【发布时间】:2014-02-12 19:50:12
【问题描述】:

我正在开发一个软件,我想在完成特定任务后清除每一件事,比如清除所有字段、列表视图等,我要写很多要清除的东西,就像我的新按钮的代码是:

 private void btnNew_Click(object sender, EventArgs e)
    {
        txtProductCode1.ReadOnly = false;
        txtInvoiceNo.ReadOnly = false;
        cmbCustoemerType.Enabled = false;
        cmbCustomerName.Enabled = false;
        button1.Enabled = true;
        txtProductCode1.Text = null;
        txtWageName.Text = null;
        txtWageCost.Text = null;
        btnFirst.Visible = false;
        btnPrevious.Visible = false;
        btnNext.Visible = false;
        btnLast.Visible = false;
        cmbProductName.Enabled = true;
        txtQty.ReadOnly = false;
        txtPercant.ReadOnly = false;
        txtDiscount.ReadOnly = false;
        cmbCustoemerType.Enabled = true;
        cmbCustomerName.Enabled = true;
        button5.Enabled = true;
        btnDelete.Enabled = true;
        dtp1.Enabled = true;
        btnSave.Text = "&Save";

        cmbCustoemerType.Text = null;
        cmbCustomerName.Text = null;
        txtInvoiceNo.Clear();
        txtDiscount.Clear();
        txtGrandTotal.Clear();
        txtProductName.Clear();
        txtSalePrice.Clear();
        txtQty.Clear();
        txtTotal.Clear();

        txtExtraWages.Text = null;
        lvExtraWages.Items.Clear();
        lvTransaction.Items.Clear();
        lblCount.Text = null;
        lblNetTotal.Text = null;
        txtPercant.Text = null;

        txtInvoiceNo.Text = invoice.ToString();
    }

有什么办法可以摆脱再次编写代码的麻烦,就像我需要在另一个按钮上启用我在此处禁用的一件事,所以我必须编写反向代码来完成任务,这真的是恼人的!有没有其他方法可以做到这一点?

编辑:

尝试了以下代码:

private void btnNew_Click(object sender, EventArgs e)
        {   
           //using setboxdefault function
            SetTextBoxDefaults();

            cmbCustoemerType.Text = null;
            cmbCustomerName.Text = null;
        cmbCustoemerType.Enabled = false;
        cmbCustomerName.Enabled = false;
        cmbProductName.Enabled = true;
        cmbCustoemerType.Enabled = true;
        cmbCustomerName.Enabled = true;

        button5.Enabled = true;
        btnDelete.Enabled = true;  
        button1.Enabled = true;          
        btnFirst.Visible = false;
        btnPrevious.Visible = false;
        btnNext.Visible = false;
        btnLast.Visible = false;          


        btnSave.Text = "&Save";

        lvExtraWages.Items.Clear();
        lvTransaction.Items.Clear();

        lblCount.Text = null;
        lblNetTotal.Text = null;

        dtp1.Enabled = true;

        txtInvoiceNo.Text = invoice.ToString();
    }

    private void SetTextBoxDefaults()
    {
        var textBoxes = GetControls(this, typeof(TextBox));
        foreach (var textBox in textBoxes)
        {
            TextBox.Clear();
        }
    }

    public IEnumerable<Control> GetControls(Control control, Type type)
    {
        var controls = control.Controls.Cast<Control>();
        return controls.SelectMany(ctrl => GetControls(ctrl, type)).Concat(controls).Where(c => c.GetType() == type);
    }

但它在 TextBox.Clear(); 上给出错误,错误是错误 4

An object reference is required for the non-static field, method, or property 'System.Windows.Forms.TextBoxBase.Clear()'    

请让我知道我在哪里弄错了..!!

【问题讨论】:

  • 你试过循环遍历控件集合吗?
  • @Tim 不,遗憾的是我不知道
  • 这可能就是你要找的东西:stackoverflow.com/questions/15569641/…
  • 看他怎么不知道自己在做什么,应该是winforms吧。
  • 是的,但如果是 1500 行的属性分配,那么之后你可能会远远少于一半。

标签: c# .net


【解决方案1】:

正如 Tim 所建议的,您可以遍历控件并相应地设置每个控件..

public IEnumerable<Control> GetControls(Control control,Type type)
{
    var controls = control.Controls.Cast<Control>();
    return controls.SelectMany(ctrl => GetControls(ctrl,type)).Concat(controls).Where(c => c.GetType() == type);
}

private void btnNew_Click(object sender, EventArgs e)
{
    SetComboBoxDefaults();
    SetTextBoxDefaults();
    SetButtonDefaults();
}

private void SetComboBoxDefaults()
{
    var comboBoxes = GetControls(this, typeof(ComboBox));
    foreach (var comboBox in comboBoxes)
    {
        ((ComboBox)comboBox).Enabled = false;
        ((ComboBox)comboBox).Text = null;
    }
}

private void SetTextBoxDefaults()
{
    var textBoxes = GetControls(this, typeof(TextBox));
    foreach (var textBox in textBoxes)
    {
        ((TextBox)textBox).Clear();
    }
}

private void SetButtonDefaults()
{
    var buttons = GetControls(this, typeof(Button));
    foreach (var button in buttons)
    {
        ((Button)button).Visible = false;

        if (button.Name == "btnSave") ((Button)button).Text = "&Save";
    }
}

您还可以有单独的 GetControl 方法,以便它返回特定类型的 Control,从而减少转换为派生类型的需要。

更新编辑:

您正在使用TextBox 类型而不是foreach 循环中的变量名。更新它以使用textBox(小写),而不是TextBox(帕斯卡)见下文:

private void SetTextBoxDefaults()
{
    var textBoxes = GetControls(this, typeof(TextBox));
    foreach (var textBox in textBoxes)
    {
        ((TextBox)textBox).Clear();
    }
}

【讨论】:

  • 哇,终于有一些漂亮干净的代码了……我一定会试试这个
  • @JackFrost 您可能需要根据控件名称添加一些特定的条件,如果它与所有其他相同类型的控件不同(如 SetButtonDefaults() 所示)
  • 现在出现此错误 错误 1 ​​'System.Windows.Forms.Control' 不包含 'Clear' 的定义,并且没有扩展方法 'Clear' 接受类型为 'System.Windows 的第一个参数.Forms.Control' 可以找到(您是否缺少 using 指令或程序集引用?)
  • 现在工作很酷,但如果我必须清空一些文本框但不是全部? @d.moncada
【解决方案2】:

记得保持干燥

将重置逻辑划分为合适的小方法,以重置控件的逻辑分组。然后在需要时调用这些方法。

【讨论】:

    【解决方案3】:

    我在几个这样的情况下所做的是:

    void SetTextBoxReadOnly(bool val, param TextBox[] textBoxes)
    {
        if(textBoxes != null && textBoxes.Length > 0)
        {
            foreach(TextBox textBox in textBoxes)
            {
                if(textBox != null)
                {
                    textBox.ReadOnly = val;
                }
            }
        }
    }
    
    void SetControlEnabled(bool val, param Control[] ctrls)
    {
        if(ctrls != null && ctrls.Length > 0)
        {
            foreach(Control ctrl in ctrls)
            {
                if(ctrl != null)
                {
                    ctrl.Enabled = val;
                }
            }
        }
    }
    
    // etc set of methods for similar situations.
    // there methods can be invoked as
    
    ...
    ...
    ...
    
    SetTextBoxReadOnly(true, txtProductCode1,
                             txtInvoiceNo,
                             txtQty,
                             txtPercant,
                             txtDiscount);
    

    【讨论】:

    • @AhsanMughal - 这种方法可能会产生一些负面影响,但我还没有看到任何东西。此外,它可以帮助我保持代码的简洁和相关性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 2016-04-09
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 2019-05-17
    • 2021-05-06
    相关资源
    最近更新 更多