Predmet:Dinamicko kreirajne Comman Button
Tri varijante kako napraviti dinamicki kontroli ( Command Button ) na forme
Verzija 1
Kreirajte nova Forma Form1 i na njoj stavite jedan command button Commatd1 i jos stavite i
Command1.index=0
Zatim iskopirajte sledeci kod na forme
PreuzmiIzvorni kôd (Visual Basic):Private Sub Form_Load()
Dim lngIndex As Long
For lngIndex = 1 To 100
Load Command1(lngIndex)
Next lngIndex
For lngIndex = 0 To Command1.UBound
With Command1(lngIndex)
.Caption = CStr(lngIndex)
.Visible = True
End With
Next lngIndex
End Sub
Private Sub Form_Resize()
Dim lngIndex As Long
Dim sngWidth As Single, sngHeight As Single
Dim lngRow As Long, lngCol As Long
sngWidth = ScaleWidth / 10
sngHeight = ScaleHeight / 10
For lngIndex = 0 To Command1.UBound
lngRow = lngIndex \ 10
lngCol = lngIndex Mod 10
Command1(lngIndex).Move lngCol * sngWidth, lngRow * sngHeight, sngWidth, sngHeight
Next lngIndex
End Sub
Verzija 2
Samo kreirajte nova forma i upisite ovaj cod:
PreuzmiIzvorni kôd (Visual Basic):Dim WithEvents Cmd1 As CommandButton
'
Private Sub Form_Load()
Set Cmd1 = Controls.Add("vb.commandbutton", "Cmd1")
Cmd1.Width = 2000
Cmd1.Top = Me.Height / 2 - Cmd1.Height / 2 - 100
Cmd1.Left = Me.Width / 2 - Cmd1.Width / 2 - 100
Cmd1.Caption = "Dynamic Button"
Cmd1.Visible = True
Set Cmd2 = Controls.Add("vb.commandbutton", "Cmd2")
Cmd2.Width = 2000
Cmd2.Top = 10
Cmd2.Left = 10
Cmd2.Caption = "Dynamic Button 2"
Cmd2.Visible = True
End Sub
'
Private Sub Cmd1_click()
MsgBox "I have been Created Dynamically at Run-time", _
, "Dynamic Controls"
End Sub
Verzija 3
Kreirajte nova forma i upisite ovaj cod :
PreuzmiIzvorni kôd (Visual Basic):Dim cmdButton(4) As CommandButton
Private Sub Form_Load()
Dim i As Integer
For i = 0 To 4
Set cmdButton(i) = Me.Controls.Add("VB.CommandButton", "cmdButton" & Me.Controls.Count)
With cmdButton(i)
.Left = 750 * i
.Top = 1000
.Width = 700
.Height = 500
.Caption = "Hello"
.Visible = True
End With
Next i
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer
For i = 0 To 4
Set cmdButton(i) = Nothing
Next i
End Sub