C#显示和隐藏面板 [英] C# showing and hiding panels

查看:124
本文介绍了C#显示和隐藏面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#-我有一个winform,上面有1到n个面板,其中n可以等于50或更大.我确实知道当前可见面板的名称(并且可以将名称分配给字符串变量),但是如果我想使该面板可见= false,而使另一个可见= true,我需要知道如何做到这一点.

C# -- I have a winform that has 1 to n panels on it, where n could equal 50 or more. I do know the name (and can assign the name to a string variable) of the current panel that is visible, but if I want to make that panel visible = false and another visible = true, I need to know how to do that know only the name that is in a string variable.

推荐答案

您要将变量(面板)的名称存储为字符串吗?在某种收藏中?我真的要问为什么?

我建议将变量(面板)存储在集合中.它们存储在任何地方吗?
You are storing the names of your variables (panels), as strings? In some sort of collection? I really have to ask why?

I recommend storing the variables (panels) in a collection. Are they stored anywhere?


您不应使用Name属性.如今,使用字符串数据代替实际数据是初学者最糟糕的趋势之一,很难说清原因.您需要存储对象本身,即引用.现在,我只能假设您在Designer中创建了全部50个或更多面板.这是完全错误的-大量的手工工作,仅此而已.您应该在代码中创建它们,而不是在自动生成的代码中创建它们. (根本不要使用< codename> ;!)这样,您可以将引用存储在数组或其他集合中,以方便访问并避免体力劳动. (您是否是软件开发人员?:-)).然后,是,< code> panel.Visible = false; /*…*/panel.Visible = true; …你不会错的…

—SA
You should not use the Name property. Using string data instead of actual data is one of the worst trends of the beginners these days, hard to say why. You need to store objects themselves, references. Now, I can only assume you created all 50 or more panels in Designer. This is totally wrong — a lot of manual work, nothing else. You should create them in your code, not auto-generated code. (Don''t use <codename> at all!) This way, you can store the reference in an array or some other collection, for easy access and to avoid manual labor. (Are you a software developer, anyway? :-)). And then, yes, <code>panel.Visible = false; /* … */ panel.Visible = true; … you cannot go wrong with that…

—SA


您可以使用LINQ和要使用的面板名称将面板从Controls集合中删除.这将从名称为"panel1"的面板中取出一个面板.

You can get the panel out of the Controls collection using LINQ and the name of the Panel that you want to work with. This gets a panel out of the collection that has the name "panel1".

var p = from c in this.Controls.Cast<Control>()
                      where c.Name == "panel1"
                      select c;
            Panel pan = (Panel)p.FirstOrDefault();



从那里,您可以使用它进行任何操作,使其可见,不可见并更改背景颜色.



From there you can do whatever you want with it, make it visible, invisible, change the background color.


这篇关于C#显示和隐藏面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆