Connecting Tech Pros Worldwide Forums | Help | Site Map

List All Form and All Control in each Form in a project

Newbie
 
Join Date: Oct 2009
Posts: 3
#1: Oct 21 '09
I'm using a assembly to load my apllication, and now I need to
list all forms in my apllication and list all controls.
I have read the topic on List all controls inside of each form and do like that, i only listed all form's name but i can't listed any control in any form. When i used the CreatInstance function to get the controls of the form, it always return null value.
Please help me!

tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,779
#2: Oct 21 '09

re: List All Form and All Control in each Form in a project


I've read over the article you mentioned.
Would you provide the code that you actually using that gives you the error?

And just because I'm curious... What is the purpose/need for going through all this? If you are loading your application doesn't that mean you already have access to all the forms and controls in it?
Newbie
 
Join Date: Oct 2009
Posts: 3
#3: Oct 21 '09

re: List All Form and All Control in each Form in a project


Thank for your anwser.
Here is my code :

Expand|Select|Wrap|Line Numbers
  1. Assembly assembly = Assembly.GetExecutingAssembly();
  2.  
  3.             List<string> listAllForm = new List<string>();
  4.             List<string> listAllControl = new List<string>();
  5.             string control = string.Empty, form = string.Empty;
  6.  
  7.             foreach (Type assemblyType in assembly.GetTypes())
  8.             {
  9.                 if (assemblyType.IsSubclassOf(typeof(Form)))
  10.                 {
  11.                     AssemblyName[] name = assembly.GetReferencedAssemblies();
  12.  
  13.                     listAllForm.Add(assemblyType.Name);
  14.                     form += "\r\n" +  assemblyType.Name + "\r\n";
  15.  
  16.                     Form frm = (Form)assembly.CreateInstance(assemblyType.BaseType.Name,
  17.                                     false, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, CultureInfo.GetCultureInfo("en-us"), null);
  18.                     if (frm != null)
  19.                     {
  20.                         if (frm.Controls.Count > 0)
  21.                         {
  22.                             foreach (Control ctrl in frm.Controls)
  23.                             {
  24.                                 listAllControl.Add(ctrl.Name);
  25.                                 control += ctrl.Name;
  26.                                 control += "\r\n";
  27.                             }
  28.                         }
  29.                     }
  30.                     else
  31.                     {
  32.                         control = "Can't load";
  33.                     }
  34.                 }
  35.             }
  36.  
  37.             richTextBox1.Text = control;
  38.             richTextBox2.Text = form;
I'm trying to decentralize my application follow the function of the user logged in.So, I need to list all the controls (such as: Button, TextBox, ...) in the forms that the user can do action.
Please help me!
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,779
#4: Oct 21 '09

re: List All Form and All Control in each Form in a project


TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,779
#5: Oct 21 '09

re: List All Form and All Control in each Form in a project


Quote:
I'm trying to decentralize my application follow the function of the user logged in.So, I need to list all the controls (such as: Button, TextBox, ...) in the forms that the user can do action.
Honestly, I can't even envision how you are going to go about taking a list of all the forms and all the control names, and make that relate to user privileges when they log in. It seems that if everything is tied to the form names and control names then it will be hard to manage. Every time you add a control, or change a name then you have to rework your permissions code. I'd love to see how you go about accomplishing this based on form and control names.

I go a much different route: A "Priveleges" class. When a user logs in their Priveleges class is populated. The class contains booleans like "AllowEdit", "AllowMakeUsers", "AllowExit", "ShowTools", "AllowCustomize" and so on. Then the code of all the other forms just gets the user's Priveleges class to see if they are allowed to do something. If the user tries to open the 'Preferences' but "AllowCustomize" is false, then they get a dialog saying they don't have permissions.

In some places I just use the Priveleges to determine if certain menu options are even visible
EditMenu.Visible = AllowEdit;


Anyway, getting back to the way you want to do requiring getting all the form names and control names... I've never played with doing that. I'll play around with the code you provided and see what I can come up with.
Newbie
 
Join Date: Oct 2009
Posts: 3
#6: Oct 21 '09

re: List All Form and All Control in each Form in a project


Thank for your sharing.
But, I have my reason to choose that way. If you can help me to solving above codes, i'll feel very happy.
All that i need is how to list all Forms and all controls inside each Form.
Thanks!
Reply