在加载之前获取项目中的所有表单 [英] get all of forms in project before loaded

查看:70
本文介绍了在加载之前获取项目中的所有表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为用户提供用户访问级别:



我有一个表单。它显示所有控件都以win-project的形式存在。直到管理员用户可以通过标记checkboxList来管理应用程序中的控件。



我按字典计划单独的表单。例如:

to have levels of user access for the users:

I have a form. it shows all of the controls exist in forms of win-project. until admin user can manage the controls in app by marking checkboxList.

I plan separate forms by Dictionary. Like:

Dictionary<clssMenu_Item, List<clssMenu_Item>>



clssMenu_Item保存一个控件的属性一个表格。




clssMenu_Item save properties of one control in a form.

public class clssMenu_Item
{
    public string name; //control's name
    public string text; //control's text
    public string strKey; //Example: in 'MbtnAcc' = 'A' is strKey
}



因此字典中有2个级别:





  • btn1


    • btn1-1
    • btn1-2
    • 推荐答案

      看看这个链接。可以帮助你:



      http:// bytes。 com / topic / c-sharp / answers / 545315-list-all-controls-inside-each-form [ ^ ]
      Look at this link.maybe help you:

      http://bytes.com/topic/c-sharp/answers/545315-list-all-controls-inside-each-form[^]


      这是你如何找到所有的在任何给定时刻打开表单: http://msdn.microsoft .com / zh-cn / library / system.windows.forms.application.openforms.aspx [ ^ ]。



      您无需查找任何其他形式,因为只有开放表格可用。如果你想在显示之前检查一些表单,你需要创建表单(你总是在你的代码中创建它们,所以你总是可以收集对它们的引用)并创建你想要检查的表单的集合。



      现在,表单是 Control ,模式完全正确, System.Windows .Forms.Control 。所有形式的孩子也是控制。唯一的问题是:要遍历表单上的所有控件,您将需要使用递归,因为控件是分层次的父级。它可能是这样的:

      This is how you can find all the open forms at any given moment of time: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.openforms.aspx[^].

      You don't need to find any other forms, as only open forms are usable. If you want to examine some forms before they are shown, you need to create forms (you always create them in your code, by yourself, so you can always collect references to them) and make your own collections of the forms you want to examine.

      Now, a form is a Control, mode exactly, System.Windows.Forms.Control. And all form children are also controls. The only problem is: to traverse all controls on the form, you will need to use recursion, as controls are parented hierarchically. It could be something like this:
      static ProcessControls(Control parent, System.Action<Control> processingMethod) {
          processingMethod(parent);
          foreach(Control child in parent.Controls)
              ProcessControls(child, processingMethod);
      } //ProcessControls

      我不知道做什么你想做的处理方法。也许你只需要处理一些单独类型的控件,或者只处理不是容器的控件(然后你可以检查(someControl.Controls.Count == 0))。如果您只需要收集满足某些集合中某些条件的所有控件,则需要一个附加参数来进行集合。例如,对于列表,它可以是 System.Action< Control,System.Collections.Generic.IList< Control>>



      您创建一些处理每个控件的方法,并使用必需的参数将其作为 processingMethod 参数传递(在许多情况下建议使用匿名方法,但不是必需),并将其传递给 ProcessControl ;第一个参数应该是每个表单(你还记得表单也是 Control )。该方法将在表单本身之后以递归方式处理所有嵌套控件。



      此架构可以有许多变体。



      -SA

      I don't know what do your want to do in the processing methods. Perhaps you need to process only the controls of some separate types, or only the controls which are not containers (and then you can check for (someControl.Controls.Count == 0)). If you need just to collect all controls satisfying certain criteria in some collection, you would need an additional parameter, for the collection. For lists, for example, it could be System.Action<Control, System.Collections.Generic.IList<Control>>.

      You create some method processing each control with required parameter(s) to pass it as the processingMethod parameter (anonymous method recommended in many cases, but not required), and pass it to ProcessControl; the first parameter should be each form (you remember that the form is also Control). The method will recursively process all nested control, after the form itself.

      There can be many variants of this schema.

      —SA


      来自以下参考:

      通过反射获取命名空间中的所有类型



      from below reference:
      Getting all types in a namespace via reflection

      List<string> @namespace = new List<string>(new string[] { "list1._1", "list1"});
                  for (int i = 0; i < @namespace.Count; i++ )
                  {
                      var q = from t in Assembly.GetExecutingAssembly().GetTypes()
                              where t.IsClass && t.Namespace == @namespace[i]
                              select t;
                      q.ToList().ForEach(t => MessageBox.Show(t.Name.ToString()));
                  }





      list1._1和list1是项目中的命名空间。



      "list1._1" and "list1" are my namespaces in project.


      这篇关于在加载之前获取项目中的所有表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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