How to clear form fields in c#?

Asked

Viewed 6,715 times

2

I’m doing a project for college, I created a GroupBox and inside there’s RadioButton, Label, MaskedTexbox and TextBox. When I click the cancel button I did the cleaning of the MaskTexbox and TextBox and disable the Textbox. It just cleans and had to repeat the code to clean follows below.

private void toolStripButtonCancelar_Click(object sender, EventArgs e)
{
    rdbAprovadoAberto.Enabled = false;
    rdbReprovadoAberto.Enabled = false;
    lblNomeUser.Text = "Sistema";

    maskDataAprovReprov.Text = "";// limpa o campo MaskedTextBox
    txtMotivoReprovaAberto.Text = "";// limpa o campo TextBox
    this.txtMotivoReprovaAberto.Enabled = false;

    // tenho que duplicar o código senão não limpa os campos e desabilita txtMotivoReprovaAberto

    rdbAbertoAberto.Checked = true;
    maskDataAprovReprov.Text = "";
    txtMotivoReprovaAberto.Text = "";
    txtMotivoReprovaAberto.Enabled = false;
}

and that also worked out but I don’t know if it’s good practices

private void toolStripButtonCancelar_Click(object sender, EventArgs e)
{   
    rdbAprovadoAberto.Enabled = false;
    rdbReprovadoAberto.Enabled = false;
    lblNomeUser.Text = "Sistema";

    if (rdbAprovadoAberto.Checked)
    {
        maskDataAprovReprov.Text = "";
    }

    if (rdbReprovadoAberto.Checked)
    {
        maskDataAprovReprov.Text = "";
        txtMotivoReprovaAberto.Text = "";
        txtMotivoReprovaAberto.Enabled = false;
    }
}
  • 5

    Read your text calmly, think you don’t know the details of the program. Do you think you can understand? The text doesn’t seem to make much sense. I couldn’t understand if you wanted something more than what you’re doing and if something was wrong. Also depending on what you really want this isolated stretch don’t help understand the problem. What I can say is that this section is clearing the data and is marking a radio button as turned on.

4 answers

2

I like to make a generic method. For example, in the following code I clean all the text boxes of a FORM. You can put other conditions to check other types of components of the FORM and do what you wish.

foreach (Control c in this.Controls)
{
    if (c.GetType().ToString() == "System.Windows.Form.Textbox")
    {
        c.Text = "";
    }
}

1

Break your code more, for easy reading. The smaller the code the easier it is to understand and debug.

Then you can slowly improve. is improve to view duplicate code or code (in this case the functions) with more than one responsibility.

Many conditions in the same function can make your code barely readable and difficult to maintain.

1

I usually create a cleaning method only. There are how to make generic methods, for all controls, but I do one for each situation, will your preference.

Avoid using TextBox1.Text = ""; instead prefer to use: TextBox1.Text = string.Empty; besides getting more elegant, avoids you unintentionally pressing the space bar between the quotes and theMela all your code.

Create methods just for this, do not clear on the click of the cancel button as you did, instead call the cleaning method only.

1

Utilize TextBox1.Text = string.Empty;

if there are several do so:

 public void limpaCampos()
 {
   TextBox1.Text  = 
   TextBox2.Text  = 
   TextBox3.Text  = 
   TextBoxn.Text  = string.Empty;
 }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.