Marie Curie
Marie Curie

Reputation: 125

How does this work with textboxes in win forms

I am learning C#, for my school homeworks. I suddenly try to do something and I thought, I am going to get a bug in the below code

  private void ClearControls()
        {
            this.textBox1 .Text = "";
            this.textBox1 = this.textBox2 = this.textBox3 = this.textBox4;
        }

Wow...it works better what I expected and it cleared all my text boxes in the form , and before this, I was doing like

textBox1 .Text = "";
textBox2 .Text = "";

and so on till some twenty text boxes in a form (this is the method , my teacher told me and all my classmates follow this :( )..

which one is correct, and why the first one works good and how the default attribute assigned to a textbox is always text and not name or tabindex or someother ones ?

if the question is not clear or a little mess, please tell and I will try change it.

Thanks for taking time to clear my doubts :D

Upvotes: 3

Views: 407

Answers (5)

Dan J
Dan J

Reputation: 16708

People have given some good alternatives and comments, but let me address the question of exactly what is going on in these lines of code.

Pardon me if this is too basic, but you did say you were just learning C#. :)

this.textBox1.Text = "";
this.textBox1 = this.textBox2 = this.textBox3 = this.textBox4;

First, understand that textBox1, textBox2, etc. are references to objects of type TextBox which are contained in your Form. Every TextBox has a property called Text that is of type string. If I understand correctly, you'd like to clear the text from all four textboxes on your form, which is to say you want to set the value of the Text property for each TextBox to the empty string "".

Your first line of code does this, but only for the first TextBox. The second line of code, however, as others have pointed out, assigns each textBox<n> reference to refer to the same TextBox as textBox4. If textBox4.Text happens to be the empty string, this will appear to do what you want, but in reality you've changed the state of the references to the TextBoxes themselves, and not the values in each TextBox's Text property.

Make sure you understand the difference between setting a reference (textBox1 = textBox2) and setting a property (textBox1.Text = textBox2.Text). In the first case, you went from having references to two TextBoxes to having two references to the same TextBox. To simply clear out TextBoxes, you want the second case, which merely copies the property value.

In short, given that (hopefully not-too-muddled) explanation, your code as given should become this:

this.textBox1.Text = "";
this.textBox4.Text = this.textBox3.Text = this.textBox2.Text = this.textBox1.Text;

But, strictly speaking, you're much better off using a solution like @Tim Jarvis's or @Brandon's, which work regardless of how many TextBoxes you have on your form.

Good luck! :)

Upvotes: 1

nabrond
nabrond

Reputation: 1388

Looping through controls is always an option too...

foreach (Control objControl in this.Controls) 
{
     if (objControl is TextBox) 
     {
          ((Textbox)objControl).Text = String.Empty;
     }
}

Upvotes: 1

AliPanahi2
AliPanahi2

Reputation: 33

And this is another way

Is this what you want ?

foreach (Control item in this.Controls)
        {
            if (item.GetType() == typeof(TextBox))
                ((TextBox)item).Text = "";
        }

Upvotes: 1

Tim Jarvis
Tim Jarvis

Reputation: 18815

You could also use Linq...

Controls.OfType<TextBox>().ToList().ForEach(tb => tb.Text = "");

Upvotes: 3

Ed Swangren
Ed Swangren

Reputation: 124632

Not quite. This line sets each reference equal to textBox4, which is not what you want. Now these four references all point to the same thing.

this.textBox1 = this.textBox2 = this.textBox3 = this.textBox4

What you wanted was this:

this.textBox1.Text = this.textBox2.Text = this.textBox3.Text = this.textBox4.Text = ""

However, that is a maintenance headache. Create a UserControl for this stuff or at least maintain a collection of TextBox objects that you can iterate through to set common properties instead of adding a new line for every text box.

Upvotes: 4

Related Questions