Click here to Skip to main content
15,885,919 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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:
C#
Dictionary<clssMenu_Item, List<clssMenu_Item>>

clssMenu_Item save properties of one control in a form.

C#
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
}

so there is 2 levels in dictionary:


  • btn1

    • btn1-1
    • btn1-2

  • btn2
  • btn3

btn1 - btn3 are in form A.

btn1-1 and btn1-2 are buttons in Form B, by clicking on btn1 from Form A.

I took a look at http://support.microsoft.com/kb/815707 but wasn't useful for me. because declare a global variable and in this global variable exist forms which had opened before. so I don't have access to all of the forms in project.

UPDATE:
I wanna get properties of controls in all of my forms in project.
because my project is accountancy. so it's clear I need user access management.
Posted
Updated 7-Sep-13 20:37pm
v2
Comments
Sergey Alexandrovich Kryukov 7-Sep-13 10:17am    
It does not seem to make any sense. First of all, what do you mean by "forms"? Types or instances (objects)? And what do you mean by "load"? "Before loaded", nothing exists. And "all forms in projects" exist only in project, as types. When you come to loading, the concept "project" itself does not exist, you only have to assemblies. I'm not sure you understand yourself what you are asking about.

If you want help, you should start well before this point, from your ultimate goals, and then explain your idea, and so on...
—SA
iran_girl 7-Sep-13 10:49am    
in some of applications, it's important to define "User access" for some "Controls" in program. I'm talking about a place(Form) which is defined for "create User access". now I wanna create this form by code not manually. OK?! if there's any further question, inform me :-)
Sergey Alexandrovich Kryukov 7-Sep-13 12:57pm    
You answered only a part of my questions... As it is, your question simply makes no sense.
—SA
iran_girl 7-Sep-13 13:09pm    
my means is clear my friend! please try to read my topic totally not line by line....
OK I have grammar mistake on topic: it is: get all of forms in project before has been loaded (I just wanted to be shorter)
about 'all of the forms in project': at run time, we have access to a form when it had been loaded.
I hope understood my mean :-(
to solve my problem need to use assembly code, but I dont know about it. please help me, if u can...
thanks for ur attention
Sergey Alexandrovich Kryukov 7-Sep-13 20:05pm    
Project is loaded by a system loaded, and later it is JIT-compiled as the methods are called. The form as instance is created only when a new operator is called, much later. And reflection can only fetch all your types and type members, not objects. And this is a trivial thing. Still, you need to understand the difference between objects and types. Which forms do your need, form types of object?..
—SA

 
Share this answer
 
Comments
iran_girl 7-Sep-13 12:43pm    
I already knew can do it by Reflection. I have a sample code too:
var q = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.IsClass && t == typeof(System.Windows.Forms.Form)
select t;
but I dont know how can I use it in foreach loop.
abbaspirmoradi 7-Sep-13 12:54pm    
this also comes in link:

foreach (assemblyType in assembly.GetTypes())
{
if (assemblyType.IsSubclassOf(typeof(System.Windows.F orms.Form)))
{
// You may need to use assemblyType.FullName here... I don't
know for sure.
System.Windows.Forms.Form f =
(System.Windows.Forms.Form)assembly.CreateInstance (assemblyType.Name,
false, System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.NonPublic, null, null, null, null);
foreach (Control c in f.Controls)
{
... do something with control c of form f ...
}
f.Dispose();
}
}
iran_girl 7-Sep-13 13:00pm    
oh yeah! I dont know why I didnt saw it before!! ;-) I tried it, but it have some errors and because I dont know about reflection, so it wasn't useful for me.
iran_girl 7-Sep-13 13:20pm    
I have error in 1st line:
foreach (assemblyType in Assembly.GetTypes())
{
if (AssemblyType.IsSubclassOf(typeof(System.Windows.Forms.Form)))
{

System.Windows.Forms.Form f =
(System.Windows.Forms.Form)assembly.CreateInstance (assemblyType.Name,
false, System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.NonPublic, null, null, null, null);
foreach (Control c in f.Controls)
{
MessageBox.Show(c.Name.ToString());
}
f.Dispose();
}
}
abbaspirmoradi 7-Sep-13 13:21pm    
put your error here..
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:
C#
static ProcessControls(Control parent, System.Action<Control> processingMethod) {
    processingMethod(parent);
    foreach(Control child in parent.Controls)
        ProcessControls(child, processingMethod);
} //ProcessControls
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
 
Share this answer
 
v2
Comments
iran_girl 8-Sep-13 3:31am    
I've no idea Alex, because I dont have experience in C# like you.
could u tell me when I want call your method, what are the arguments should I pass to it.
Sergey Alexandrovich Kryukov 8-Sep-13 3:36am    
Excuse me, Alex is not my name.
I already explained about parameters: a form instance and some method:

Form someForm;
//...
ProcessControls(someForm, new Action<Control>(
(control) => { // Control control, in this case, inferred type is Control, inferred from Action type
DoSomethingWith(control);
}
));


When? When you need to process all your controls they way you want it...

—SA
iran_girl 8-Sep-13 3:49am    
oh! sorry my friend about your name, I confused by searching in internet to find solution...

about your code, I had found like it before. because of "Form someForm" line, I tried to find a way to dont use this line. u know there is plenty of forms in project. and it's hard and spaghetti to call all of them manually.
iran_girl 8-Sep-13 3:51am    
finally, I think have to create "user access management form" manually :(
Sergey Alexandrovich Kryukov 8-Sep-13 11:19am    
Then you should not have asked this question of asked it this way. You could not see my code: I've written it specially for you. You should not do anything manually. I already told you where you can get all the forms. If you want not open but created forms, you could create them in once centralized facility, where you could collect them all in one place.
—SA
from below reference:
Getting all types in a namespace via reflection

C#
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" and "list1" are my namespaces in project.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900