Creating objects at runtime

  • Thread starter Thread starter Kimmo Laine
  • Start date Start date
K

Kimmo Laine

Hello,

(how) can i do the following (in C#)?

// . . .
class MyForm : Form {
// . . .

protected Button myButton = null;
protected TextBox myTB = null;

protected void AddControl( ref Control ctrl, string strType, Size s,
Point pt, string txt ) {

ctrl = new < ?!?! >;
ctrl.Location = pt;
ctrl.Size = s;
ctrl.Text = txt;

Controls.Add( ctrl );

}

protected override void OnLoad( EventArgs e ) {

AddControl( myButton, "Button", new Size( ...), new Point( ... ),
"Dummy 1" );
AddControl( myTB, "TextBox", new Size( ...), new Point( ... ),
"Dummy 1" );

base.OnLoad( e );
}
}

It would be even better if the second parameter ( in AddControl ) could be
the actual type:

AddControl( myButton, Button, new Size( ...), new Point( ... ),
"Dummy 1" );
AddControl( myTB, TextBox, new Size( ...), new Point( ... ), "Dummy
1" );


Thx!


- Kimmo Laine
 
Hi Kimmo,

You can use the following classes and methods:

Assembly.CreateInstance(...) - this one accepts full type name
Activator.CreateInstance(...) - this one accepts a Type instance
 
Hi,

don?t work

Type t = Type.GetType( "System.Windows.Forms.Button" );
Button b = (Button)Activator.CreateInstance( t );

- Kimmo Laine


Dmitriy Lapshin said:
Hi Kimmo,

You can use the following classes and methods:

Assembly.CreateInstance(...) - this one accepts full type name
Activator.CreateInstance(...) - this one accepts a Type instance


--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Kimmo Laine said:
Hello,

(how) can i do the following (in C#)?

// . . .
class MyForm : Form {
// . . .

protected Button myButton = null;
protected TextBox myTB = null;

protected void AddControl( ref Control ctrl, string strType, Size s,
Point pt, string txt ) {

ctrl = new < ?!?! >;
ctrl.Location = pt;
ctrl.Size = s;
ctrl.Text = txt;

Controls.Add( ctrl );

}

protected override void OnLoad( EventArgs e ) {

AddControl( myButton, "Button", new Size( ...), new Point( .... ),
"Dummy 1" );
AddControl( myTB, "TextBox", new Size( ...), new Point( ... ),
"Dummy 1" );

base.OnLoad( e );
}
}

It would be even better if the second parameter ( in AddControl ) could be
the actual type:

AddControl( myButton, Button, new Size( ...), new Point( ... ),
"Dummy 1" );
AddControl( myTB, TextBox, new Size( ...), new Point( ... ), "Dummy
1" );


Thx!


- Kimmo Laine
 
Kimmo,

This doesn't work because you are making the call to GetType outside of
the assembly that the type is in. In the documentation for the GetType
method on the Type class, it will tell you how to get the fully-qualified
type name, which is what you need to use for types contained in assemblies
outside of the one where the call to GetType is made.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- nick(dot)paldino=at=exisconsulting<dot>com

Kimmo Laine said:
Hi,

don?t work

Type t = Type.GetType( "System.Windows.Forms.Button" );
Button b = (Button)Activator.CreateInstance( t );

- Kimmo Laine


Dmitriy Lapshin said:
Hi Kimmo,

You can use the following classes and methods:

Assembly.CreateInstance(...) - this one accepts full type name
Activator.CreateInstance(...) - this one accepts a Type instance


--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Kimmo Laine said:
Hello,

(how) can i do the following (in C#)?

// . . .
class MyForm : Form {
// . . .

protected Button myButton = null;
protected TextBox myTB = null;

protected void AddControl( ref Control ctrl, string strType, Size s,
Point pt, string txt ) {

ctrl = new < ?!?! >;
ctrl.Location = pt;
ctrl.Size = s;
ctrl.Text = txt;

Controls.Add( ctrl );

}

protected override void OnLoad( EventArgs e ) {

AddControl( myButton, "Button", new Size( ...), new Point( ... ),
"Dummy 1" );
AddControl( myTB, "TextBox", new Size( ...), new Point( ... ),
"Dummy 1" );

base.OnLoad( e );
}
}

It would be even better if the second parameter ( in AddControl )
could
 
Kimmo Laine said:
Type t = Type.GetType( "System.Windows.Forms.Button" );
Button b = (Button)Activator.CreateInstance( t );

When Type.GetType is only given the name of the type without
assembly/version information, it only looks in the currently executing
assembly and mscorlib. If you want it to look elsewhere, you should
either provide the assembly/version information, or use
Assembly.GetType instead on the appropriate assembly.
 
Back
Top