Thread: Vis. C# Expr. - Get all Form Names

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    8

    Vis. C# Expr. - Get all Form Names

    I would like to get all form names available - not just open ones.

    I've seen a number of snippets to do this, and they use Assembly, but Vis. Expr. will not let me do it.

    Here is an example I found:

    Code:
     //Assembly[] array = AppDomain.CurrentDomain.GetAssemblies();
    
                //foreach (Assembly a in array)
                //{
    
                //    this.treeView1.Nodes.Add(a.GetName().Name, a.GetName().Name);
    
                //    Type[] types = a.GetTypes();
    
                //    foreach (Type t in types)
                //    {
    
                //        if (t.IsPublic && t.BaseType == typeof(Form))
    
                //            this.treeView1.Nodes[a.GetName().Name].Nodes.Add(t.FullName);
    
                //    }
    
                //}
    
                //treeView1.ExpandAll();
    Vis. C# Express will not even let me do the first line: Assembly[] array = AppDomain.CurrentDomain.GetAssemblies()

    It doesn't have Assembly[] available.


    Thanks in advance.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Moved to C#

    Welcome to the board, but can you please be a little more careful under which board you post?

    Thanks...

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Assembly is located in System.Reflection so either you state
    using System.Reflection;
    at the top of the cs file or you declare the Assembly object as such:
    System.Reflection.Assembly[] assemblies;

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    Thanks, Shakti.

    Spot on!

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    As a follow-up, I'd like to have the form opened when user selects it in the tree. How would I do this? I can get the name stored in the tree node value. But I do not know how to use the value.

    Thanks.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    This is how i managed to open a second form, not sure if its the correct way though since i havent dealt much with reflection.
    Code:
    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
    Form f = null;
    foreach (Assembly a in assemblies)
    {
        Type[] types = a.GetTypes();
        foreach (Type t in types)
        {
            if (t.IsPublic && t.BaseType == typeof(Form))
            {
                if (t.FullName.Contains("Form1"))
                {
                    f = (Form)a.CreateInstance(t.FullName);
                }
            }
        }
    }
    
    if (f != null)
        f.Show();
    You might want to explore the Tag property so you can assign a certain Assembly to a certain node and so on. This should make the code something along the lines of
    Code:
    Form f = (SelectedNode.Tag as Assembly).CreateInstance(SelectedNode.Text) as Form;

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    Yes! Very minor modification to change "Form1" to treeview1.SelectedNode.Text and to add f.TopMost=true.

    Works like a champ!

    Thanks, again!

    Last question on this topic: Is there a way to run debug on the present form, as opposed to having it start at the root? It would be nice to program a form and check it out without having to go through a bunch of hassle.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    You mean switch from normal run to debug mode in the program? As far as I know this is not possible but this is really outside my knowledge.

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    No. I mean to just debug the current form in the design view. Let's say I have six forms open. I am working on one of them. I want to just test that one instead of having to use the tree we made to get to it and open it.

    I plan on having many forms in the project. The tree will become large, and I will get tired of having to scroll to find the form I want to test.

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    So just add code to the main form so that the form you are working on is opened automatically when you start the program?

  11. #11
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    I could do that. Was just hoping for something easier since will be dealing with lots of forms. Anyway, your help above was what I really needed most. Thanks so much. Have a great day!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++builder6 change form names problem
    By Leite33 in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2008, 08:20 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. simple calculator
    By Micko in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2005, 06:47 AM
  4. Accessing main form from functions in other classes
    By pj_martins in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2004, 09:27 AM
  5. My UserControls dissapear off my form
    By zMan in forum C# Programming
    Replies: 2
    Last Post: 09-15-2004, 08:55 AM