Get rid of the long code

I am working on software in which I want to clear every thing after completing a specific task, for example, clear all fields, view a list, etc. etc., and I have to write a lot of things that need to be cleaned, like the code for my new button:

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

Is there a way to get rid of re-writing the code again, for example, I need to enable one thing that I turned off here on a different button, so I need to write the reverse code of this to accomplish this task, and it is really annoying! is there any other way to do this?

EDIT:

tried the following code:

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

but it gives an error on TextBox.Clear();, error - error 4

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

Please let me know where I am wrong .. !!

+4
3

, .

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, , .

:

TextBox foreach. , TextBox ( ), TextBox (), . :

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

reset , , reset . , .

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

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


All Articles