Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
1.12/5 (5 votes)
See more:
I want to loop through all the forms present in my solution to add the name of the form present in my solution to a hashtable programatically.how can i do that?
Posted
Updated 7-Sep-18 4:35am
Comments
Vivek Krishnamurthy 21-Jun-11 5:56am    
More details would be required here as to, when / where are you building this hashtable. Do you need this data at runtime / at design time. where / when I mean, is it in visual studio add in mode, at runtime ?
Sergey Alexandrovich Kryukov 21-Jun-11 18:25pm    
We discussed some options of the meaning of it, which is still uncertain; please see my answer for explanation.
--SA

Solution does not consist of forms, no looping is possible. If you have a running form application, you can list the just the open forms using System.Windows.Forms.Application.OpenForms, see http://msdn.microsoft.com/en-us/library/system.windows.forms.application.aspx[^].

You can also count of form classes using Reflection. Do you need it?

From the run-time standpoint, there is no such thing as solution. From the standpoint of solution, there is no such thing as Form, there is just a set of project files which has a set of source files.

[EDIT]

Solution as proposed as Firo is possible, but it cannot give you reliable result. You can create a form which has no derived form class, you can create a form class by never use it. In view of that, which exactly forms do you need to take into account? If one form class is used to created two different forms, how do you want to count.

All this does not make any sense to me. If you explain the purpose of all of that, you might have a chance to get a better answer.

—SA
 
Share this answer
 
v2
Comments
Firo Atrum Ventus 21-Jun-11 21:08pm    
I agree with you. The solution that I proposed actually just a way to check which class included in the solution, not how many forms(You can check which class inherits System.windows.form but it still wouldn't give you a reliable result).
Sergey Alexandrovich Kryukov 21-Jun-11 21:20pm    
I understand. I just think it has little to know sense as it does not reflect any properties of code (unused form classes are irrelevant, some dynamically created forms can not be detected).
--SA
This is quite complicated

My recommendation is:
1. Read the .sln file
2. Find some lines like this:(Use RegEx)
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "MSK", "MSK\MSK.vbproj", "{0CD3E9B3-0340-43ED-9FAF-49E290122C30}"

3. Find .vbproj directory ("MSK\MSK.vbproj") (Use RegEx)
4. Read .vbproj file
5. Find some lines like this:(Use RegEx)
XML
<Compile Include="controller\Controller.vb" />

or
XML
<Compile Include="frmBuatRealisasiPDJMan.Designer.vb">
      <DependentUpon>frmBuatRealisasiPDJMan.vb</DependentUpon>
    </Compile>

6. Find directory of .vb files(Use RegEx)
7. Find class name (if necessary) from .vb files (Use RegEx)
8. Put anything you want from point 6 or 7 into hashtable
9. Repeat until you got everything you wanted.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 21-Jun-11 18:25pm    
My 4. I cannot be sure it makes any sense. What one would consider a distinct form? This is not certain.
I credited your solution in mine and commented on it; please see.
--SA
Look at the Application.OpenForms[^] property
 
Share this answer
 
Comments
Kunal Singha Roy 21-Jun-11 6:00am    
But i also want to get the name of the forms that are not opened.
Sergey Alexandrovich Kryukov 21-Jun-11 18:20pm    
See my answer. Do you want me to give you more detail on Reflection? This is simple.
--SA
Simon_Whale 21-Jun-11 18:44pm    
you are going to do this with some sort of generic list or collection to store all of your forms in and use it in conjuction with application.openforms to see which are opended
AFAIK, it is not supported according to Microsoft. However they provide this link which may provide a solution for you.

http://support.microsoft.com/kb/308537[^]
 
Share this answer
 
 
Share this answer
 
Try this:

VB
Dim allForms() As Form = (From t As Type In Me.GetType().Assembly.GetTypes() _
                                 Where t.BaseType Is GetType(Form) _
                                 Let f = DirectCast(Activator.CreateInstance(t), Form) _
                                 Select f).ToArray
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 28-Oct-15 3:33am    
This question is 4 years old. It also got answered, and one of the answers already accepted by OP...Please do not answer, such old/answered questions...It may gain you 10 rep-points, but may get you banned too...
itzvivek 6-Nov-15 2:17am    
My goal ain't to gain point(s), The code I posted is really simple to achieve the task that's why I posted.If it against CP's rule then I won't do this in future

and thanks for the headsup !!

Have a nice day
cod-jmjohnson 11-Dec-18 9:07am    
Great answer, thank you!
Lambda version of the solution posted by itzvivek

VB
Dim AllForms As Form() = Me.GetType().Assembly.GetTypes() _
    .Where(Function(t) t.BaseType Is GetType(Form)) _
    .Select(Function(t) New With
    {
        Key .Form = DirectCast(Activator.CreateInstance(t), Form)
    }).Select(Function(x) x.Form).ToArray()


Converted to C#
C#
Form[] allForms = this.GetType().Assembly.GetTypes()
    .Where(t => t.BaseType == typeof(Form))
    .Select(t => new
    {
        Form = (Form)Activator.CreateInstance(t)
    }).Select(x => x.Form).ToArray();
 
Share this answer
 
v3
Comments
CHill60 10-Sep-18 7:00am    
If your user name is your actual email address I suggest that you change your profile name - the bit that can be seen on this open forum. Otherwise you could be targetted by spam bots
cod-jmjohnson 11-Dec-18 9:04am    
I changed it. Thank you for the heads up.

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