Prikazi cijelu temu 29.05.2018 01:07
Gjoreski Van mreze
Administrator
Registrovan od:02.02.2009
Lokacija:Ohrid Makedonija


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):
  1. Private Sub Form_Load()
  2.  
  3.   Dim lngIndex As Long
  4.   For lngIndex = 1 To 100
  5.     Load Command1(lngIndex)
  6.   Next lngIndex
  7.   For lngIndex = 0 To Command1.UBound
  8.     With Command1(lngIndex)
  9.       .Caption = CStr(lngIndex)
  10.       .Visible = True
  11.     End With
  12.   Next lngIndex
  13.  
  14.  End Sub
  15.  
  16. Private Sub Form_Resize()
  17.   Dim lngIndex As Long
  18.   Dim sngWidth As Single, sngHeight As Single
  19.   Dim lngRow As Long, lngCol As Long
  20.   sngWidth = ScaleWidth / 10
  21.   sngHeight = ScaleHeight / 10
  22.   For lngIndex = 0 To Command1.UBound
  23.     lngRow = lngIndex \ 10
  24.     lngCol = lngIndex Mod 10
  25.     Command1(lngIndex).Move lngCol * sngWidth, lngRow * sngHeight, sngWidth, sngHeight
  26.   Next lngIndex
  27. End Sub

Verzija 2
Samo kreirajte nova forma i upisite ovaj cod:
PreuzmiIzvorni kôd (Visual Basic):
  1. Dim WithEvents Cmd1 As CommandButton
  2. '
  3. Private Sub Form_Load()
  4.   Set Cmd1 = Controls.Add("vb.commandbutton", "Cmd1")
  5.       Cmd1.Width = 2000
  6.       Cmd1.Top = Me.Height / 2 - Cmd1.Height / 2 - 100
  7.       Cmd1.Left = Me.Width / 2 - Cmd1.Width / 2 - 100
  8.       Cmd1.Caption = "Dynamic Button"
  9.       Cmd1.Visible = True
  10.      
  11.   Set Cmd2 = Controls.Add("vb.commandbutton", "Cmd2")
  12.       Cmd2.Width = 2000
  13.       Cmd2.Top = 10
  14.       Cmd2.Left = 10
  15.       Cmd2.Caption = "Dynamic Button 2"
  16.       Cmd2.Visible = True
  17. End Sub
  18. '
  19. Private Sub Cmd1_click()
  20.   MsgBox "I have been Created Dynamically at Run-time", _
  21.     , "Dynamic Controls"
  22. End Sub

Verzija 3
Kreirajte nova forma i upisite ovaj cod :

PreuzmiIzvorni kôd (Visual Basic):
  1. Dim cmdButton(4) As CommandButton
  2.  
  3. Private Sub Form_Load()
  4.  
  5.     Dim i As Integer
  6.  
  7.     For i = 0 To 4
  8.         Set cmdButton(i) = Me.Controls.Add("VB.CommandButton", "cmdButton" & Me.Controls.Count)
  9.         With cmdButton(i)
  10.             .Left = 750 * i
  11.             .Top = 1000
  12.             .Width = 700
  13.             .Height = 500
  14.             .Caption = "Hello"
  15.             .Visible = True
  16.         End With
  17.     Next i
  18.  
  19. End Sub
  20.  
  21. Private Sub Form_Unload(Cancel As Integer)
  22.  
  23.     Dim i As Integer
  24.  
  25.     For i = 0 To 4
  26.         Set cmdButton(i) = Nothing
  27.     Next i
  28.      
  29. End Sub