Private WithEvents NewTextBox As TextBox
'通过使用WithEvents关键字声明一个对象变量为新的命令按钮
Private Sub Command1_Click()
If NewTextBox Is Nothing Then
Set NewTextBox = Controls.Add("VB.TextBox", "cmdNew", Form1)
NewTextBox.Visible = True
End If
End Sub
Exit Sub
Else
Controls.Remove NewTextBox
Set NewTextBox = Nothing
Visual Basic .NET支持动态属性,其动态属性储存在一个应用程序配置文件中,该文件在应用程序执行时将会被读到.使用动态属性可以在不重新编译应用程序的情况下改变一些属性值.你可以使用它们保存数据库连接字符串、应用程序日志信息或者服务器连接信息.
要看动态属性的例子,首先在Visual Basic中创建一个Windows应用程序.添加一个TextBox到默认窗体中;确认TextBox是被选中的,然后在属性窗口中定位Dynamic属性.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TabControl1.TabPages.Add("aa") /添加一个选项卡
TabControl1.TabPages.Add("NEW")
Dim ttl As Integer = TabControl1.TabPages.Count
TabControl1.SelectedIndex = ttl - 1
添加动态按钮,及关联按钮事件的方法如下
----------------------
' 声明对象
Dim newButton As New System.Windows.Forms.Button
' 预设对象
With newButton
.Name = [按钮控件名称]
.Text = [按钮显示内容]
.Location = New Point([x位置], [y位置])
.Size = New System.Drawing.Size([控件宽], [控件高])
.FlatStyle = FlatStyle.Standard
End With
' 生成添加对象
Controls.Add(newButton)
' 相关事件链接
AddHandler newButton.Click, AddressOf myButtonHandler_Click
若为标签,则 首行对象为System.Windows.Forms.Label
若为文本框,则 首行对象为System.Windows.Forms.TextBox
如果要处理其他事件,自己在[相关事件链接]部分添加链接就可以了
举着例子如TextBox的TextChanged事件
AddHandler newTextBox.TextChanged, AddressOf myTextBox_TextChanged
AddHandler 后为事件对象, AddressOf 后为为事件对象分配的名称
这样在处理事件是,就可以用名称操作
Private Sub myTextBox_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
...
以上就是土嘎嘎小编为大家整理的vb.net动态添加标签相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!