Click here to Skip to main content
15,881,657 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to convert string to form object in c# window application Pin
Rizny Mubarak5-Aug-13 23:43
Rizny Mubarak5-Aug-13 23:43 
QuestionRe: How to convert string to form object in c# window application Pin
Eddy Vluggen6-Aug-13 7:47
professionalEddy Vluggen6-Aug-13 7:47 
AnswerRe: How to convert string to form object in c# window application Pin
susanna.floora7-Aug-13 19:07
susanna.floora7-Aug-13 19:07 
AnswerRe: How to convert string to form object in c# window application Pin
ExcellentOrg6-Aug-13 9:22
ExcellentOrg6-Aug-13 9:22 
AnswerRe: How to convert string to form object in c# window application Pin
Jean A Brandelero8-Aug-13 10:45
Jean A Brandelero8-Aug-13 10:45 
GeneralRe: How to convert string to form object in c# window application Pin
susanna.floora9-Aug-13 2:02
susanna.floora9-Aug-13 2:02 
GeneralRe: How to convert string to form object in c# window application Pin
Jean A Brandelero9-Aug-13 7:51
Jean A Brandelero9-Aug-13 7:51 
AnswerRe: How to convert string to form object in c# window application Pin
BillWoodruff11-Aug-13 0:13
professionalBillWoodruff11-Aug-13 0:13 
I usually don't comment on a question like this, when the OP has a made a comment of the form "it works," in response to a solution.

But, I'd like to note that, imho, Eddie Vluggen asked what I think is the critical question here:

"The name of a form-object (name property) or the name of the forms' class? In other words, does the form already exist that you're looking for, or are you showing a new instance of that form?"

To which the OP responded:

"Form already exists in the application.
form name is in a string.
how do i open that form."

The code shown in the response by Jean Brandelero will create a new instance of the Form based on its class-name. And, perhaps, that is exactly what the OP wanted.

But, it also seems possible the OP does not want to create a new instance, but to "open" an existing Form.

In that case there are two alternatives:

a. the Form instance is already open, and is visible: your done.

Whether an instance of a Form is visible, or hidden, it's in the Application.OpenForms collection. Note that the collection has a very limited set of operators: you can't use something like 'Contains. And, each Form in that collection is in a "vanilla" type-form: you can't automatically assign it to a variable of the type of FormX: you have to cast the result to ProjectName.FormX.

b. the Form instance is now hidden: your job is to 'Show it.

So, now you can define the alternatives here clearly: based on whether, as Eddie was pointing out, the "string" you have contains a Class Name of a Form, or the string contains the name of a form already created as an instance.

You can use Brandelero's code to create a new instance, if that's what you want, and it doesn't matter to you if there already any other instances of the Type that exist.

And, if you want to create an instance only if an instance does not already exist, or you just want to make sure an existing instance is visible: you can mess around with the Application.OpenForms collection to see what's what.

Let's look at simple test case:
// in Form scope
        private Form2 newForm2 = new Form2();
        private Form2 newForm2a = new Form2();

// in Form Load event
        newForm2.Name = "newForm2";
        newForm2.Show();
        newForm2.Hide();

        newForm2a.Name = "newForm2a";
        newForm2a.Show();
        newForm2a.Hide();

        foreach (Form theCandidateForm in Application.OpenForms)
        {
            if (theCandidateForm.Name == "newForm2")
            {
               // you found a match by Name
               Console.WriteLine("Found match by Name: " + theCandidateForm.Name);
            }

            if( theCandidateForm.GetType() == newForm2.GetType())
            {
                // you found one, of perhaps more than one, instance of 'newForm2
                Console.WriteLine("Found match by Type " + theCandidateForm.Name);
            }
        }
The output of this in the VS 'Output window would be:

Found match by Name: newForm2
Found match by Type newForm2
Found match by Type newForm2a

I hope this information adds something to this thread.


“This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal

GeneralRe: How to convert string to form object in c# window application Pin
susanna.floora26-Aug-13 21:47
susanna.floora26-Aug-13 21:47 
QuestionAutocomplete TextBox no find other words in string Pin
juliogyn5-Aug-13 16:49
juliogyn5-Aug-13 16:49 
AnswerRe: Autocomplete TextBox no find other words in string Pin
Abhinav S5-Aug-13 17:07
Abhinav S5-Aug-13 17:07 
GeneralRe: Autocomplete TextBox no find other words in string Pin
juliogyn5-Aug-13 18:08
juliogyn5-Aug-13 18:08 
AnswerRe: Autocomplete TextBox no find other words in string Pin
Abhinav S5-Aug-13 18:31
Abhinav S5-Aug-13 18:31 
GeneralRe: Autocomplete TextBox no find other words in string Pin
juliogyn6-Aug-13 9:18
juliogyn6-Aug-13 9:18 
QuestionTry, Catch, Finally question Pin
JD865-Aug-13 15:15
JD865-Aug-13 15:15 
AnswerRe: Try, Catch, Finally question Pin
Abhinav S5-Aug-13 16:45
Abhinav S5-Aug-13 16:45 
GeneralRe: Try, Catch, Finally question Pin
JD866-Aug-13 6:18
JD866-Aug-13 6:18 
AnswerRe: Try, Catch, Finally question Pin
ExcellentOrg5-Aug-13 22:06
ExcellentOrg5-Aug-13 22:06 
AnswerRe: Try, Catch, Finally question Pin
Bernhard Hiller5-Aug-13 22:46
Bernhard Hiller5-Aug-13 22:46 
GeneralRe: Try, Catch, Finally question Pin
JD866-Aug-13 6:18
JD866-Aug-13 6:18 
GeneralRe: Try, Catch, Finally question Pin
Jean A Brandelero8-Aug-13 10:47
Jean A Brandelero8-Aug-13 10:47 
QuestionClick-Once :: Publishing And Updates In Different Locations Pin
Matt U.5-Aug-13 8:17
Matt U.5-Aug-13 8:17 
AnswerRe: Click-Once :: Publishing And Updates In Different Locations Pin
Pete O'Hanlon6-Aug-13 23:05
mvePete O'Hanlon6-Aug-13 23:05 
GeneralRe: Click-Once :: Publishing And Updates In Different Locations Pin
Matt U.7-Aug-13 2:01
Matt U.7-Aug-13 2:01 
QuestionC# Garbage Collection and GC.Collect() Pin
DSLoginYea5-Aug-13 7:38
DSLoginYea5-Aug-13 7:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.