简体   繁体   English

为多个控件分配可见性的最佳方法是什么

[英]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. 好吧所以我正在尝试清理我的代码,因为它是一个混乱,我有25个richtext框,我想将他们的.Visible变量放入一个数组并有一个for语句通过并使每个错误,以便文本框没有显示我尝试过没有工作,我无法弄清楚我有什么。

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. 您可以更改数组中的bool并期望它更改TextBoxes的Visible状态。

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 . 因此,您必须将这些控件存储在集合中或使用不同的方法:如果它们位于同一容器控件(如FormGroupBoxPanel等)中,则可以使用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. 你只是复制boolean,这完全独立于对象(由richTextBox1引用)并且更改为不同的boolean值只会更改数组的内容,没有链接到对象来更改其属性。

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 . TextBox.Visible是一个属性,因此返回一个 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: 在C#中无法存储对值的引用,因此请尝试以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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