English Sentence Loading...
英语句子加载中...
在VB中动态添加和删除控件
作者:junyuqin 日期:2007-08-22
例子1:
Option Explicit
'创建控件
Private Sub Command1_Click()
Load Label1(1)
Label1(1).Left = 0
Label1(1).Top = 0
Label1(1).Caption = "动态控件"
Label1(1).Visible = True
End Sub
'删除控件
Private Sub Command2_Click()
Unload Label1(1)
End Sub
例子2:
两个Command控件,一个PictureBox控件,将Picture1的Index属性设置为0
Private Sub cmdLoad_Click()
Dim i As Integer
For i = 1 To 5
Load Picture1(i)
Set Picture1(i).Container = Picture1(0)
Picture1(i).Visible = True
Picture1(i).Left = i + i * 5
Picture1(i).Top = i + i * 5
Next
cmdLoad.Enabled = False
cmdUnload.Enabled = True
End Sub
Private Sub cmdUnload_Click()
Dim i As Integer
For i = 1 To 5
Unload Picture1(i)
Next
cmdLoad.Enabled = True
cmdUnload.Enabled = False
End Sub
例子3:
动态添加按钮
Option Explicit
Dim WithEvents cmdSayHello As CommandButton
Dim WithEvents cmdClose As CommandButton
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub cmdSayHello_Click()
MsgBox "Hello world!", vbInformation, Me.Caption
End Sub
Private Sub Form_Load()
Set cmdSayHello = Me.Controls.Add("VB.CommandButton", "cmdSayHello")
With cmdSayHello
.Left = 4000
.Top = 250
.Width = 1200
.Height = 350
.Caption = "&Say Hello"
.Visible = True
End With
Set cmdClose = Me.Controls.Add("VB.CommandButton", "cmdClose")
With cmdClose
.Left = 4000
.Top = 700
.Width = 1200
.Height = 350
.Caption = "&Close"
.Visible = True
End With
End Sub
Option Explicit
'创建控件
Private Sub Command1_Click()
Load Label1(1)
Label1(1).Left = 0
Label1(1).Top = 0
Label1(1).Caption = "动态控件"
Label1(1).Visible = True
End Sub
'删除控件
Private Sub Command2_Click()
Unload Label1(1)
End Sub
例子2:
两个Command控件,一个PictureBox控件,将Picture1的Index属性设置为0
Private Sub cmdLoad_Click()
Dim i As Integer
For i = 1 To 5
Load Picture1(i)
Set Picture1(i).Container = Picture1(0)
Picture1(i).Visible = True
Picture1(i).Left = i + i * 5
Picture1(i).Top = i + i * 5
Next
cmdLoad.Enabled = False
cmdUnload.Enabled = True
End Sub
Private Sub cmdUnload_Click()
Dim i As Integer
For i = 1 To 5
Unload Picture1(i)
Next
cmdLoad.Enabled = True
cmdUnload.Enabled = False
End Sub
例子3:
动态添加按钮
Option Explicit
Dim WithEvents cmdSayHello As CommandButton
Dim WithEvents cmdClose As CommandButton
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub cmdSayHello_Click()
MsgBox "Hello world!", vbInformation, Me.Caption
End Sub
Private Sub Form_Load()
Set cmdSayHello = Me.Controls.Add("VB.CommandButton", "cmdSayHello")
With cmdSayHello
.Left = 4000
.Top = 250
.Width = 1200
.Height = 350
.Caption = "&Say Hello"
.Visible = True
End With
Set cmdClose = Me.Controls.Add("VB.CommandButton", "cmdClose")
With cmdClose
.Left = 4000
.Top = 700
.Width = 1200
.Height = 350
.Caption = "&Close"
.Visible = True
End With
End Sub
评论: 0 | 引用: 0 | 查看次数: 3030