How does this work with win-win text fields?

I study C # for my homework at school. I suddenly try to do something, and I thought: I'm going to get an error in the code below

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

Wow ... it works better than I expected, and it cleared all my text fields on the form, and before that I did as

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

etc. up to twenty text fields in the form (this is a method, my teacher told me, and all my classmates follow this :() ..

which one is correct, and why does the first one work well, and how does the default attribute assigned to a text field always be text, not a name or tabindex or others?

If the question is not clear or a little mess, let me know and I will try to change it.

Thanks for taking the time to clear my doubts: D

+3
5

. textBox4, , . .

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

, :

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

. UserControl , , TextBox, , .

+4

Linq...

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

...

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

, ?

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

, , .

, , , #.:)

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

-, , textBox1, textBox2 .. TextBox, Form. TextBox Text, string. , , Text TextBox "".

, TextBox. , , textBox<n> TextBox textBox4. textBox4.Text , , , TextBoxes , TextBox Text.

, (textBox1 = textBox2) (textBox1.Text = textBox2.Text). TextBox. TextBox, , .

In short, given the (hopefully not too confusing) explanation, your code, as indicated, should be as follows:

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

But strictly speaking, you are much better off using a solution like @Tim Jarvis's or @Brandon , which work no matter how many text fields you have in your form.

Good luck! :)

+1
source

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


All Articles