Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I am working with c#.net window based application and my problem is that I want to detect the form or get the form with the string name because There is requirement of project.

suppose I have added a form named frmMaster.cs then how can i detect that form with string name "frmMaster".


Suppose I have 15 forms in my project and I want to detect a particular form by string name.

Or how can i get all the forms in formcollection class


So please provide any kind of suggestion

Thank You..
Amit Tank.
Posted
Updated 21-Feb-21 20:22pm
v2
Comments
[no name] 11-Jun-10 6:05am    
You should try to clarify your question.Do these forms belong only to single or multiple instances of your application?Are they windows explorer or another app windows?

Iterate through the open forms like this:

foreach (Form form in Application.OpenForms)
{
    if (form is frmMaster)
    {
       // do something
       break;
    }
}


A little googling would have saved you months of time...
 
Share this answer
 
v3
Comments
koool.kabeer 27-Jul-10 6:14am    
i would like to ask you expertise that does the use of For and Foreach loop effect the performance?
Dim myAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim types As Type() = myAssembly.GetTypes()
Dim mytype As Type
For Each myType In types
If mytype.BaseType.FullName = "System.Windows.Forms.Form" Then
MessageBox.Show(mytype.Name)
If (mytype.Name = "Backup") Then
MessageBox.Show("This is for u ")
Exit For
End If
End If
 
Share this answer
 
Comments
Dalek Dave 24-Jul-10 7:30am    
Good answer
Toli Cuturicu 25-Jul-10 9:09am    
Reason for my vote of 1
"Tags: C#3.0", not VB
Hi,

There are property named "Name" which retrieves the form name that you should compare to identify a form.

Please let me know if you could get this done.
 
Share this answer
 
if your forms are opened then try this
MessageBox.Show(Application.OpenForms["frmMaster"].Text);


where "frmMaster is the 'Name' of the the form
 
Share this answer
 
Following is the the C# version of the solution given by"Ratnesh Rai" with minor changes.

string formtocall="frmMyForm";
System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
Type[] types = myAssembly.GetTypes();
foreach (Type type in types)
{
   if (type.BaseType.FullName== "System.Windows.Forms.Form") 
   {
      if (type.Name==formtocall)
      {
         var form = Activator.CreateInstance(Type.GetType("YourNamespace." + formtocall)) as Form;
         form.ShowDialog();
         break;
      }
   }
}
 
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