2014-04-01 28 views
4

好吧,所以我想清理我的代碼,因爲它是一個爛攤子,我有25個富文本框,我想把他們的.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; 
    } 
+2

盒[...] = richTextBoxx.Visible東西實際上並沒有做任何東西。你只是初始化一個數組,然後重新編寫整個內容...... –

回答

10

您canot數組中改變bool並期望改變文本框的Visible狀態。

您必須更改該屬性。因此,您必須將這些控件存儲在一個集合中或使用不同的方法:如果它們位於同一個容器控件中(如Form,GroupBoxPanel等),則可以使用Enumerable.OfType

例如:

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

這裏假定表單上沒有其他的RTB比他想操作的25個RTB更好。我已經在同一個表格上考慮了25個RTB,但也許你應該改變你的查詢,以便像OP在他的問題中那樣考慮名字。 –

+1

@ThorstenDittmar:我已經編輯了我的答案,只接受那些以「」richTextBox「開始的答案,儘管這看起來幾乎是任意的。但也許這有助於OP使用更有意義的東西。 –

+0

嗯,他可以將應該處理的RTB命名爲不同於應不處理的RTB。好方法。我會去與你的答案:-) –

0

TextBox.Visible是一個屬性,因此返回一個。因此你的布爾數組也包含值。更改此值對您的文本框沒有任何影響,因爲它不再知道文本框的任何內容。

存儲的值一提的是不是在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; 
} 
4

我想,這是你所需要的:

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

布爾值是值類型,因此,當你分配:

box[0] = richTextBox1.Visible; 

你只複製布爾值,這完全獨立於對象(由richTextBox1引用)和改變爲不同的布爾值只會改變數組的內容,沒有鏈接到對象來改變它的屬性。

最簡單的辦法 - 有可能更適用,但更復雜的人 - 是對象的引用直接存儲在數組中,並設置屬性:

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

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

把你所有的控件的ASP內部:面板然後改變panel.visible = false的可見性....爲什麼複雜的東西?

+0

爲什麼認爲這是ASP.NET?爲什麼不考慮不是所有RichTextBox都可以放在同一個面板中的可能性?等等。 –

+0

@ThorstenDittmar爲什麼不考慮他們在單獨的頁面?爲什麼不考慮他們在獨立的議會?除此之外,儘管C#是一個.Net項目,所以這是一個合理的假設,並且我的愚見認爲不需要反對投票。 –

+0

如果他們可以放在同一個面板中怎麼辦? –

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; 
} 
相關問題