为多个控件分配可见性的最佳方法是什么?(What is the best approach to assign the visibility for multiple controls)

系统教程 行业动态 更新时间:2024-06-14 16:57:39
为多个控件分配可见性的最佳方法是什么?(What is the best approach to assign the visibility for multiple controls)

好,所以我试图清理我的代码,因为它是一个混乱,我有什么是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;

更多推荐

本文发布于:2023-04-13 11:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/b9c34ae424e9a313e4f8217d711672a3.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   控件   分配   见性   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!