What is the best approach for determining visibility for multiple controls

OK, so I'm trying to clear my code because it is a mess, and that I have 25 richtext blocks, and I want to put their .Visible variable in an array and have an instruction to execute and make every false so the text box doesn’t show what I tried didn’t work, and I can’t understand what I have.

bool[] box =  new bool[25];

box[0] = richTextBox1.Visible;
box[1] = richTextBox2.Visible;
box[2] = richTextBox3.Visible;
box[3] = richTextBox4.Visible;
box[4] = richTextBox5.Visible;
box[5] = richTextBox6.Visible;
box[6] = richTextBox7.Visible;
box[7] = richTextBox8.Visible;
box[5] = richTextBox6.Visible;
box[6] = richTextBox7.Visible;
box[7] = richTextBox8.Visible;
box[8] = richTextBox9.Visible;
box[9] = richTextBox10.Visible;
box[10] = richTextBox11.Visible;
box[11] = richTextBox12.Visible;
box[12] = richTextBox13.Visible;
box[13] = richTextBox14.Visible;
box[14] = richTextBox15.Visible;
box[15] = richTextBox16.Visible;
box[16] = richTextBox17.Visible;
box[17] = richTextBox18.Visible;
box[18] = richTextBox19.Visible;
box[19] = richTextBox20.Visible;
box[20] = richTextBox21.Visible;
box[21] = richTextBox22.Visible;
box[22] = richTextBox23.Visible;
box[23] = richTextBox24.Visible;
box[24] = richTextBox25.Visible;

for(int y = 0; y <25; y++)
  {
    box[y] = false;
  }
+4
source share
6 answers

You cannot change boolin an array and expect this to change state Visiblein TextBoxes.

. , : (, Form, GroupBox, Panel ..), Enumerable.OfType.

:

var allRichTextBoxes = this.Controls.OfType<RichTextBox>()
    .Where(txt => txt.Name.StartsWith("richTextBox"));
foreach(var rtb in allRichTextBoxes)
    rtb.Visible = false;
+10

, , :

for(int y =0; y < box.Length; y++)
{
    ((RichTextBox)this.FindControl("richTextBox" + (y+1).ToString()))
                      .Visible = box[y];
}
+4

- , :

box[0] = richTextBox1.Visible;

, ( richTextBox1) , .

- , , - :

var boxes = new RichTextBox[...]
boxes[0] = richTextBox1;
...

for (int y = 0; y < boxes.Lengthl y++) {
  boxes[y].Visible = false;
}
+2

TextBox.Visible . , Boolean . , .

#, :

RichtTextBox[] box =  new RichTextBox[25];

box[0] = richTextBox1;
box[1] = richTextBox2;
box[2] = richTextBox3;
box[3] = richTextBox4;
box[4] = richTextBox5;
// ...

for(int y = 0; y <25; y++)
{
    box[y].Visible = false;
}
0

asp: Panel, . visible = false.... ?

0

,

private void SetVisibilityForAllTextBoxesTo(bool isVisible)
{
    this.richTextBox1.Visible = 
    this.richTextBox2.Visible = 
    this.richTextBox3.Visible = 
    this.richTextBox4.Visible = 
    this.richTextBox5.Visible = 
    this.richTextBox6.Visible = 
    this.richTextBox7.Visible = 
    this.richTextBox8.Visible = 
    this.richTextBox6.Visible = 
    this.richTextBox7.Visible = 
    this.richTextBox8.Visible = 
    this.richTextBox9.Visible = 
    this.richTextBox10.Visible = 
    this.richTextBox11.Visible = 
    this.richTextBox12.Visible = 
    this.richTextBox13.Visible = 
    this.richTextBox14.Visible = 
    this.richTextBox15.Visible = 
    this.richTextBox16.Visible = 
    this.richTextBox17.Visible = 
    this.richTextBox18.Visible = 
    this.richTextBox19.Visible = 
    this.richTextBox20.Visible = 
    this.richTextBox21.Visible = 
    this.richTextBox22.Visible = 
    this.richTextBox23.Visible = 
    this.richTextBox24.Visible = 
    this.richTextBox25.Visible = isVisible;
}
0

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


All Articles