create an instance with a string Name

W

wu jianhua

hi.
If I have a form, like FrmAbout, can I create a form instance only with a
string "FrmAbout"?
not like : Form frm = new FrmAbout();
I want the code : Form frm = createInstance( "FrmAbout" );
How to code my createInstance function?

Thanks.
 
M

Michael Nemtsev

Hello wu,

See this code. Comments expain how it works

// Get current assembly
Assembly frmMain = Assembly.GetEntryAssembly();
// Get Type of your form
System.Type formType = frmMain.GetType("WindowsApplication1.frmAbout");
// Create instance of form
object pc = Activator.CreateInstance(formType);
// Cast to Form class
Form frm = (Form)pc;
// Show Form
frm.Show();

wj> hi.
wj> If I have a form, like FrmAbout, can I create a form instance only
wj> with a
wj> string "FrmAbout"?
wj> not like : Form frm = new FrmAbout();
wj> I want the code : Form frm = createInstance( "FrmAbout" );
wj> How to code my createInstance function?
wj> Thanks.

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
M

Michael Nemtsev

Hello Michael ,

And instead of using Assembly u can change these 2 lines below

// Get current assembly
Assembly frmMain = Assembly.GetEntryAssembly();
// Get Type of your form
System.Type formType = frmMain.GetType("WindowsApplication1.frmAbout");

on this

Type formType = typeof(WindowsApplication1.frmAbout);

MN> Hello wu,
MN>
MN> See this code. Comments expain how it works
MN>
MN> // Get current assembly
MN> Assembly frmMain = Assembly.GetEntryAssembly();
MN> // Get Type of your form
MN> System.Type formType =
MN> frmMain.GetType("WindowsApplication1.frmAbout");
MN> // Create instance of form
MN> object pc = Activator.CreateInstance(formType);
MN> // Cast to Form class
MN> Form frm = (Form)pc;
MN> // Show Form
MN> frm.Show();
wj>> hi.
wj>> If I have a form, like FrmAbout, can I create a form instance only
wj>> with a
wj>> string "FrmAbout"?
wj>> not like : Form frm = new FrmAbout();
wj>> I want the code : Form frm = createInstance( "FrmAbout" );
wj>> How to code my createInstance function?
wj>> Thanks.
MN> ---
MN> WBR,
MN> Michael Nemtsev :: blog: http://spaces.msn.com/laflour
MN> "At times one remains faithful to a cause only because its opponents
MN> do not cease to be insipid." (c) Friedrich Nietzsche
MN>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
M

Michael Nemtsev

Hello rmacias,

The IDasm code is similar, except that InvokeMemeber just call virtual function
InvokeMember and CreateInstance is direct call
Using CreateInstance is more readable


r> There are several ways. You can use Activator.CreateInstance() or
r> Type.InvokeMember. I don't remember the pros and cons of these
r> methods but you can probably find out which is most appropriate for
r> your application.
r>
r> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpre
r> f/html/frlrfsystemactivatorclasscreateinstancetopic.asp
r>
r> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpre
r> f/html/frlrfsystemtypeclassinvokemembertopic.asp
r>
r> "wu jianhua" wrote:
r>---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 
J

Joanna Carter [TeamB]

"wu jianhua" <[email protected]> a écrit dans le message de (e-mail address removed)...


| If I have a form, like FrmAbout, can I create a form instance only with a
| string "FrmAbout"?
| not like : Form frm = new FrmAbout();
| I want the code : Form frm = createInstance( "FrmAbout" );
| How to code my createInstance function?

Take a look at Activator.CreateInstance(...)

Joanna
 

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

Similar Threads

how to show an about box 3
position of modal form 2
Form Names 2
Compiler Error CS0201 2
Garbage Collection 8
about form 1
How do I create a modal dialog box? 4
Application.Run() and ShowDialog error 4

Top