create an instance with a string Name

  • Thread starter Thread starter wu jianhua
  • Start date Start date
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.
 
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
 
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
 
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
 
"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
 
Back
Top