好,所以我试图清理我的代码,因为它是一个混乱,我有什么是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; }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; }最满意答案
你可以改变数组中的bool ,并期望这会改变TextBoxes的Visible状态。
你必须改变那个属性。 因此,您必须将这些控件存储在集合中或使用不同的方法:如果它们位于同一个容器控件中(如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;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;更多推荐
发布评论