将窗体上的所有控件添加到集合或arraylist的最简单方法? [英] The easiest way to add all controls on a form to a collection or an arraylist?

查看:185
本文介绍了将窗体上的所有控件添加到集合或arraylist的最简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道最简单的方法是使用RECURSION,但是我讨厌递归,只是为了它的工作方式根本不想使用它!
实际上,所有的递归函数都可以使用其他LOOP语句转换为普通函数.但是我确实看到,如果不使用递归(例如浏览二进制树中的所有节点?),某些问题将变得更加复杂.我将表单上的所有控件添加到集合或arraylist的问题是另一种情况,如果使用递归,则太简单了,但是使用我正在使用的解决方案(不使用递归),我觉得这真的很难表单具有如此多的控件,其中包含该控件,也包含该控件(许多子级-父级关系).我的表单仅停留在4个这样的层,但是它至少需要4个foreach才能浏览所有(如果使用TabControl,则需要更多的foreach).您能否告诉我更多有关递归与替换"或递归的重要性"的信息,并给我提供解决方案,而不用最简单,最轻松地使用递归来解决我的问题?
非常感谢!
另外,您能给我一些理由让我喜欢递归吗?
嗯,开发人员会在编码和发布.NET库之类的库时使用它吗?
谢谢!

I know the easiest way is to use RECURSION, but I hate recursion, don''t want to use it at all, just for the way it works!
In fact, all recursive functions can be able to converted to normal functions using a misc of LOOP statements. But I really see that, some problem will be more complex if we don''t use recursion (such as Browsing all nodes in a binary tree?). And my problem with adding all controls on a form to a collection or an arraylist is another case, too simple if using Recursion, but with the solution I am using (don''t use recursiom) I feel it is really hard to do if the form has so many controls, this contains that and this contains that (many layers of child - parent relationship). My form stops at only 4 such layers but It needs at least 4 foreaches to browse all (the more foreaches will be needed if using TabControls). Could you please tell me more about "RECURSION AND THE REPLACEMENT" or "THE IMPORTANCE OF RECURSION" and give me the solution to solve my problem without using RECURSION the most simply and easily?
Thank you so much!
Plus, can you give me some reason to make me love recursion?
Ah, Do developers use it in coding and publishing libraries like .NET library?
Thank you!

推荐答案

用其他任何东西代替递归都不容易,这就是为什么它是如此强大的技术.
阅读 [ ^ ],然后再回到这里继续阅读.

要执行递归功能而不进行递归意味着记住您的位置,然后稍后再回到该点.当您不知道要遍历的列表的潜在深度时,这根本不是一件容易的事.

最好通过递归进行:设置简单且易于理解.
是的,太简单了,太容易理解了,功能强大,只是在某些情况下,如果我们喜欢简单性,就无法取代它,不是吗?
那我的问题呢?我应该使用递归吗?非常感谢!"


是的.考虑一下:控件包含控件列表,因此它是自然递归的结构.在没有处理的情况下处理它会使事情变得非常复杂,超出了必要的范围.
Replacing recursion with anything else is not easy, that is why it is such a powerful technique.
Read this[^] first, then come back here and read on.

To do a recursive function without recursion means remembering where you are, and coming back to exactly that point later. This is not simple at all, when you do not know the potential depth of the lists you are traversing.

Much better to do it with recursion: it is simple to set up and easy to understand.
"Yeah, too simple and easy to understand, powerful, just in some cases we can''t replace it if we like simplicity, isn''t it?
So what about my problem? Should I use recursion? Thank you so much!"


Yes. Think about it: a control contains a list of controls, so it is a naturally recursive structure. To process it without treating it as such is hugely complicating things beyond what is necessary.


也许这会有所帮助吗?

Maybe this will help?

static Controls[] GetControlsArray(Control container)
{
  List<control> controlsList = new List<control>;
  TabControl tabCtrl;

  foreach(Control cont in container.Controls)
  {
     controlsList.Add(cont);
     switch(cont.GetType().ToString())
     {
         //TabControl 
         case "System.Windows.Forms.TabControl":
           tabCtrl = (TabControl) cont;
           foreach(TabPage tp in tabCtrl.TabPages)
           {
              controlsList.Add(GetControlsArray(tp));
           }

         //handle other non-typical controls in the same manner

         //others
         default:
           controlsList.Add(GetControlsArray(cont));
           break;
     }
  }

  //TODO: some sorting perhaps?
  return controlsList.ToArray();
}</control></control>


我强烈建议您使用递归.
但是,如果您真的讨厌它,这是另一种(差,慢)的方法:反射
只需枚举Form class的字段,然后过滤掉不是Control s的字段即可.

注意:此技术不涵盖动态添加的控件.

P.S.了解反射可能是一个很好的练习.
I would strongly recommend you to use recursion.
But, if you really hate it, here is another (poor, slow) approach: Reflection
Just enumerate the fields of your Form class and filter out those which are not Controls.

Note: Dynamically added Controls are not covered by this technique.

P.S. It may be a good exercise to learn about reflection.


这篇关于将窗体上的所有控件添加到集合或arraylist的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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