public MyClass CreateMyObject(string DllFileName) { string filepath = null; if (DllFileName.StartsWith("/")) { filepath = Application.StartupPath + DllFileName.Replace("/","\\"); } else { filepath = DllFileName; } if (File.Exists(filepath)) { if (this._assembly==null) { this._assembly = Assembly.LoadFile(filepath); } MyClass obj = (MyClass)this._assembly.CreateInstance(DllClassName); return obj; } else { throw new FileNotFoundException(); } }
这是不是你想要的效果? Private Sub BtnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnAdd.Click Dim TextBox1 As New TextBox With TextBox1 .Text = "动态加的TextBox" .Visible = True .Width = 100 .Top = 20 .Left = 50 End With Me.Controls.Add(TextBox1) End Sub
private System.Windows.Forms.Button myButton = null;
private void button1_Click(object sender, System.EventArgs e)
{
myButton = new Button();
myButton.Left = 100;
myButton.Top = 100;
myButton.Width = 200;
myButton.Height = 34;
myButton.Text = "New Button";
myButton.Visible = true;
this.Controls.Add( myButton );
}
例如,如果一个panel中要加一个控件
panel.add(control)
{
string filepath = null;
if (DllFileName.StartsWith("/"))
{
filepath = Application.StartupPath + DllFileName.Replace("/","\\");
}
else
{
filepath = DllFileName;
} if (File.Exists(filepath))
{
if (this._assembly==null)
{
this._assembly = Assembly.LoadFile(filepath);
}
MyClass obj = (MyClass)this._assembly.CreateInstance(DllClassName);
return obj;
}
else
{
throw new FileNotFoundException();
}
}
Private Sub BtnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
Dim TextBox1 As New TextBox
With TextBox1
.Text = "动态加的TextBox"
.Visible = True
.Width = 100
.Top = 20
.Left = 50
End With
Me.Controls.Add(TextBox1) End Sub