4

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;
  }
Dominic Zukiewicz
  • 8,258
  • 8
  • 43
  • 61
user3448117
  • 85
  • 1
  • 6
  • 2
    The box[...] = richTextBoxx.Visible stuff doesn't actually do anything. You're initialising an array only to then rewrite the entire contents before you do something with it... – Martin Milan Apr 01 '14 at 08:13

6 Answers6

11

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;
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • This assumes that there are no other RTBs on the form than the 25 RTBs he want's to manipulate. I already consider 25 RTBs on the same form a WTF, but maybe you should change your query so that the names are taken into account like the OP does in his question. – Thorsten Dittmar Apr 01 '14 at 08:18
  • 1
    @ThorstenDittmar: I have edited my answer to take only those which start with `"richTextBox"` although that seems almost arbitrary. But maybe that helps OP to use something more meaningful. – Tim Schmelter Apr 01 '14 at 08:22
  • Well, he could name the RTBs that should be handled to a different prefix than the ones that should not be handled. Good approach. I'd go with your answer :-) – Thorsten Dittmar Apr 01 '14 at 08:25
  • @ThorstenDittmar the reason i got all of those is that i'm just now learning how to use Forms and so i started off by creating a notpad that can have 25 files open at once. – user3448117 Apr 01 '14 at 09:40
4

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];
}
Kaf
  • 33,101
  • 7
  • 58
  • 78
2

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;
}
Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
Richard
  • 106,783
  • 21
  • 203
  • 265
0

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;
}
PMF
  • 14,535
  • 3
  • 23
  • 49
0

Put all your controls inside a asp:Panel then change the visibility of the panel.visible = false.... why over complicate things?

Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
  • Why assume this is ASP.NET? Why not consider the possibility that not all the RichTextBoxes can be put into the same panel? etc. etc. – Thorsten Dittmar Apr 01 '14 at 08:26
  • @ThorstenDittmar Why not consider they are on seperate pages? Why not consider they are in seperate assemblies? Besides that though C# was a .Net project, so it's a reasonable assumption and in my humble opinion does not warrant a negative vote. – Paul Zahra Apr 01 '14 at 08:36
  • What if they can be put into the same panel? – Paul Zahra Apr 01 '14 at 08:47
  • If they could be put onto the same panel, this would be a solution. Still, there is no `RichTextBox` control for ASP.NET. Case closed. – Thorsten Dittmar Apr 01 '14 at 09:15
0

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;
}
Dominic Zukiewicz
  • 8,258
  • 8
  • 43
  • 61