open form

S

Steph

I want to crete a generic function to open forms using a string parameter

Form MyForm = OpenForm ("FrmMainMenu");
MyForm.Show()

How can I do that?

thanks

Steph
 
R

Rich P

Here is a routine I wrote which does what I believe you are trying to do
- except for multiple forms

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}

frmDelegateLambda frmDelLam = null;
frmZipStuff frmZip = null;
frmImgDocDgrv frmDg = null;
frmXmlStuff frmXml = null;
frmDrawingStuff frmDrw = null;
frmCircle frmCirc = null;
frmMultiBtns frmMB = null;
frmTextFileStuff frmTxFL = null;

Form[] frmN;

private void frmMain_Load(object sender, EventArgs e)
{
lst1.Items.Add("frmDelegateLambda");
lst1.Items.Add("frmZipSutf");
lst1.Items.Add("frmImgDocDgrv");
lst1.Items.Add("frmXmlStuff");
lst1.Items.Add("frmDrawingStuff");
lst1.Items.Add("frmCircle");
lst1.Items.Add("frmMultiBtns");
lst1.Items.Add("frmTextFileStuff");

frmN = new Form[] { frmDelLam, frmZip, frmDg, frmXml, frmDrw,
frmCirc, frmMB, frmTxFL };
}

private void lst1_Click(object sender, EventArgs e)
{
if (frmN[lst1.SelectedIndex] == null)
{
switch (lst1.SelectedIndex)
{
case 0: frmN[lst1.SelectedIndex] = new frmDelegateLambda();
break;
case 1: frmN[lst1.SelectedIndex] = new frmZipStuff();
break;
case 2: frmN[lst1.SelectedIndex] = new frmImgDocDgrv();
break;
case 3: frmN[lst1.SelectedIndex] = new frmXmlStuff();
break;
case 4: frmN[lst1.SelectedIndex] = new frmDrawingStuff();
break;
case 5: frmN[lst1.SelectedIndex] = new frmCircle();
break;
case 6: frmN[lst1.SelectedIndex] = new frmMultiBtns();
break;
case 7: frmN[lst1.SelectedIndex] = new frmTextFileStuff();
break;
}

frmN[lst1.SelectedIndex].FormClosed += frm_Closed;
frmN[lst1.SelectedIndex].Show();

}
else
{
frmN[lst1.SelectedIndex].BringToFront();

}
}

private void frm_Closed(object sender, FormClosedEventArgs e)
{
frmN[lst1.SelectedIndex] = null;
}

}


In my routine I declare form vars to existing forms in the project.
Then I have a form array of the forms vars. Then I list the actual form
names in a listbox that I call lst1. From here I can select a desired
form in the Listbox which will instantiate a form var from the form
array. I also have a form Closed event which I assign to each form var,
otherwise when you think you closed the form - it is still in memory and
will cause some conflicts.
Rich
 
S

Steph

I would like a function that I could reuse in multiple applications without
having to do modifications each time.
 
F

Family Tree Mike

Steph said:
I want to crete a generic function to open forms using a string parameter

Form MyForm = OpenForm ("FrmMainMenu");
MyForm.Show()

How can I do that?

thanks

Steph


.

It sounds like you want to use reflection. For some examples, look at this
link:
http://www.csharp-examples.net/reflection-examples/

In general though, I would try to avoid this. If you know the names of the
form classes, why do you not know the class constructors to do this the
"normal" way?

Mike
 
K

Konrad Neitzel

Hi,

I don't think that using reflection is that bad as long as you keep an eye
on the performance. I see two easy things, that could be of interest:

1) If you know the class-Names and just want to get the Type:
Type.GetType("TypeName") returns the type.

2) Loading external assemblies from a file:
You know what type you have to load (e.g. Forms). The steps are easy:
- Load the Assembly through Assembly.Load or Assembly.LoadFrom.
- Check all Types that you get from GetTypes() if they are a Form
(IsSubclassOf(...) )
- Maybe check, if your interface is imlemented (Or you make your own
Form-Class that the plugin has to use. Both is possible.)
- GetConstructor is required to get the constructor and then you call Invoke
on the ConstructorInfo Member.

Calling the functions, you want, is no problem, because you have the
interface or superclass.

My experience with loading assemblies and getting a list of easy (and fast)
useable types is good. Getting all the required types is not that hard and
the speed is ok, because you only use it when initializing the application.

Konrad
 
S

Steph

Sorry guys but to me... you just spoke chinese... I didn't understand half
of what you said.
I'm new to C# and I don't know what reflection is or what a classname is or
what an assembly is... is that a dll file?
i'll look into it but if you can give me some more explanations about how
to do it, I would really appreciate it...

thanks!

Steph
 
K

Konrad Neitzel

Hi Steph!

Steph said:
Sorry guys but to me... you just spoke chinese... I didn't understand
half of what you said.
I'm new to C# and I don't know what reflection is or what a classname
is or what an assembly is... is that a dll file?
i'll look into it but if you can give me some more explanations about
how to do it, I would really appreciate it...

I think it will be to much to give a complete introduction into this
topic here. Mike gave you a nice link. You can also look into the MSDN
Library and read alot there. (An I doubt that I could explain stuff
better than Microsoft!)

In short: Assembly is the compiled output from your c# / VB.Net code.
(normaly a DLL or EXE). And you can load these Assembly in your code,
too (e.g. to insert an PlugIn.)

Reflection is generally to look at something in your managed code. You
can look at an Object or Type and find out, what it is / contains
(Methods, Constructors, Properties, ...).
And then you can do stuff with it e.g. invoking a member or get an
instance of a given type.

Inside the MSDN Library you find a lot of stuff about that topic. Check:
- Reflection - about Reflection
- Assembly class
(I am to lazy to search the online MSDN Library to give the URLs. I
think that all developers will have a MSDN Library installed locally. If
not: Just get it quickly and install it. It is free!
http://www.microsoft.com/express/Downloads/)

With kind regards,

Konrad
 
C

Chris Dunaway

I want to crete a generic function to open forms using a string parameter

Form MyForm = OpenForm ("FrmMainMenu");
MyForm.Show()

How can I do that?

thanks

Steph

I'm confused. If you know the form when you are coding, why can't you
just code this:

FrmMainMenu myForm = new FrmMainMenu();
myForm.Show();

What advantage does your OpenForm method give you?

You can try to use the Activator.CreateInstance method:

http://msdn.microsoft.com/en-us/library/d133hta4.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top