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 scopeprivate Form2 newForm2 = new Form2();
private Form2 newForm2a = new Form2();
// in Form Load event
newForm2.Name = "newForm2";
newForm2a.Name = "newForm2a";
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
If I throw the exception in the catch block does the finally ever execute? Reason why I am doing this is because the DLL's do not perform logging, the calling class does. But I need to make sure I dispose of my objects.
I have an application which I wrote and I also maintain in my facility. Occasionally I add new features based on management's needs. I have the application setup with a Click-Once deployment. Typically I publish the application to a local server. However, my system is on a wireless connection and for some reason the internal network disappears from my list occasionally, so now I can't connect to publish updates directly (second time this has happened). I know that I can publish locally and click "Updates..." to change the update location if it's different from the publish path. Well, how can I publish to my local system but copy it to the server's publish location via USB flash drive? I mean, do I simply copy/paste the content from the local publish folder to the separate (server) location I specify for updates?
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.
If you are deploying your click once to a separate server, you need to use Mage (or MageUI) to update the deployed application. There are all sorts of security items in the deployment that need to be updated, and these tools do all the heavy work for you. You can find a simple step by step guide here[^].
I was testing the garbage collection in C#, and came across something that doesn't make sense to me. I know about Garbage Collection happening "randomly", but I am using GC.Collect() here. Please see code below and then the result information:
publicpartialclass Form1 : Form
TestA b = new TestA("first one");
for (int i = 0; i < 10; i++)
TestA a = new TestA();
MSG = "Not first one";
public TestA(string msg)
MSG = msg;
MessageBox.Show("Destructing A " + MSG);
I am getting "Not first one" (destructor and I am assuming GC'ed) 9 times. Then when I close the form I am getting "first one" 1 time and then "Not first one" 1 more time.
My main question is why isn't instance b of TestA being collected on GC.Collect? Are the ones in the for loop falling out of scope, but not the one before my for loop? If so, please explain what is keeping it in scope as Method() has completed.