简体   繁体   中英

What is the best approach to assign the visibility for multiple controls

OK so i'm trying to clean up my code because it is a mess and what i have is 25 richtext boxes and i want to put their .Visible variable into an array and have a for statement go through and make each false so that the text box doesn't show up what i have tried hasn't worked and i can't figure it out what i have is.

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

You canot change the bool in the array and expect that that changes the Visible state of the TextBoxes.

You have to change that property. Therefore you either have to store these controls in a collection or use a different approach: If they are in the same container control (like Form , GroupBox , Panel etc.) you could use Enumerable.OfType .

For example:

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

I think, this is what you need:

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

Booleans are value types, and thus when you assign:

box[0] = richTextBox1.Visible;

you are only copying the boolean, this is completely independent of the object (referenced by richTextBox1 ) and changing to a different boolean value will only change the content of the array, there is no link to an object to change its property.

The simplest approach – there are others that might suit better but are more complex – is to store the object references in an array and set the property directly:

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

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

TextBox.Visible is a property and as such returns a value . Your array of Boolean therefore contains values as well. Changing this value does nothing to your textbox, because it doesn't know anything of the textbox anymore.

Storing a reference to a value is not possible in C#, so try the following instead:

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

将所有控件放在asp:Panel中然后更改panel.visible = false的可见性....为什么复杂的事情?

Sometimes you can't get around it, so put it in a separate method and assign them all in one go

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM