首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

vb.net根据一个传入的字符串得到需要使用的类解决方法

2012-02-13 
vb.net根据一个传入的字符串得到需要使用的类比方说我传入一个“label”,则生成一个label控件,该怎么实现?希

vb.net根据一个传入的字符串得到需要使用的类
比方说我传入一个“label”,则生成一个label控件,该怎么实现?
希望各位大大帮个忙。
如果直接传入类型,又该怎么弄。


[解决办法]

VB.NET code
'动态添加控件Dim btnNewButton As New ButtonbtnNewButton.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y)  'Point根据自己情况定Me.Controls.Add(btnNewButton)
[解决办法]
VB.NET code
    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 

热点排行