vb.net根据一个传入的字符串得到需要使用的类
比方说我传入一个“label”,则生成一个label控件,该怎么实现?
希望各位大大帮个忙。
如果直接传入类型,又该怎么弄。
[解决办法]
'动态添加控件Dim btnNewButton As New ButtonbtnNewButton.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y) 'Point根据自己情况定Me.Controls.Add(btnNewButton)
[解决办法]
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.CreateCtr("textbox", "hello", Me.Controls) End Sub Private Sub CreateCtr(ByVal ctrTypeName As String, ByVal text As String, ByVal ctrCollection As ControlCollection) Dim a As Reflection.Assembly = GetType(Button).Assembly Dim ctrType As Type = a.GetType(String.Concat(a.GetName.Name, ".", ctrTypeName), True, True) Me.CreateCtr(ctrType, text, ctrCollection) End Sub '不考虑位置,事件,名称等 Private Sub CreateCtr(ByVal ctrType As Type, ByVal text As String, ByVal ctrCollection As ControlCollection) Dim ctr As Control = CType(Activator.CreateInstance(ctrType), Control) ctr.Text = text ctrCollection.Add(ctr) End Sub