摆脱长码

时间:2014-02-12 19:50:13

标签: c# .net clear

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

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

有没有办法摆脱再次编写代码n,就像我需要启用一个我已经在另一个按钮上禁用的东西,所以我必须写这个的反向代码来实现任务,它真的烦!还有其他办法吗?

编辑:

尝试了以下代码:

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()'    

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

3 个答案:

答案 0 :(得分:3)

请记住保持 DRY

将重置逻辑划分为适当的小方法,重置逻辑控制组。然后根据需要调用这些方法。

答案 1 :(得分:3)

正如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(pascal),请参阅下文:

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

答案 2 :(得分:2)

我在几个这样的案例中所做的是:

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