Controlli Dinamici crea Bottoni





vedi progetto d:\Procedure\Vitiello Pulsanti


Option Explicit
Dim ApriLettura As Boolean
Dim I As Long
Dim Stringa As String
Dim NBottone As Long
Dim myleft As Long
Dim MyTop As Long

' controlli dinamici

Dim t1(11) As VB.CommandButton




Private Sub Check1_Click(Index As Integer)

If ApriLettura = True Then
Exit Sub
End If



Dim I As Integer

' attiva o disattiva visualizzazione pulsante


If Command1(Index).Visible = True Then
Command1(Index).Visible = False

Else
Command1(Index).Visible = True

End If






' scrive log

Open App.Path & "\MyLog.ini" For Output As #1

For I = 0 To 9

If Command1(I).Visible = False Then
Print #1, "Falso;" & Command1(I).Caption
Else
Print #1, "True;" & Command1(I).Caption
End If


Next

Close #1









End Sub



Private Sub Command2_Click()

' scrive log

Open App.Path & "\MyLog.ini" For Output As #1

For I = 0 To 9

If Command1(I).Visible = False Then
Print #1, "Falso;" & Command1(I).Caption
Else
Print #1, "True;" & Command1(I).Caption
End If


Next

Close #1



End Sub

Private Sub Form_Load()
' scrive log

Dim Stringa As String
Dim I As Long
ApriLettura = True

If Dir(App.Path & "\MyLog.ini") <> "" Then
Open App.Path & "\MyLog.ini" For Input As #1

For I = 0 To 9
Line Input #1, Stringa
' Debug.Print Stringa


myleft = 270
MyTop = 1770

If Mid(Stringa, 1, 5) = "Falso" Then
Command1(I).Visible = False
Check1(I).Value = 0
Else
Command1(I).Visible = True
Check1(I).Value = 1
End If

Stringa = Mid(Stringa, 6, Len(Stringa))
Command1(I).Caption = Stringa
Check1(I).Caption = Stringa
Text1(I).Text = Stringa




Next

Close #1


End If

ApriLettura = False

End Sub

Private Sub Form_Resize()
myleft = 270
MyTop = 1770
End Sub

Private Sub mnuAllinea_Click()
Dim Mas As Long

Mas = 270

For I = 0 To 9
If Command1(I).Visible = True Then

Command1(I).Top = 60
Command1(I).Left = Mas
Mas = Command1(I).Left + Command1(I).Width + 10

End If




Next








End Sub

Private Sub mnuAttiva_Click()
If Frame1.Visible = True Then
Frame1.Visible = False
Else
Frame1.Visible = True
End If
End Sub

Private Sub mnudinamico_Click()

NBottone = NBottone + 1
Stringa = "Dinamico" & NBottone
Dim cmd1 As CommandButton

Set cmd1 = Controls.Add("vb.commandbutton", Stringa)
' Set cmd1.Index = NBottone

cmd1.Width = 2000
cmd1.Top = MyTop '(Me.Height / 2 - cmd1.Height / 2 - 100) + NBottone * 3
cmd1.Left = myleft '(Me.Width / 2 - cmd1.Width / 2 - 100) + NBottone * 3

myleft = myleft + cmd1.Width + 10

If (myleft + cmd1.Width) > Me.Width Then
MyTop = MyTop + cmd1.Height + 10
myleft = 270
End If




cmd1.Caption = "Dynamic Button"
cmd1.Visible = True



End Sub

Private Sub mnumeglio_Click()
On Error GoTo Errore

Dim x As Integer
For x = 0 To 10
Set t1(x) = Controls.Add("VB.CommandButton", "ctlCommand" & CStr(x), Form1)
t1(x).Caption = "Command" & x
t1(x).Top = 1140 + t1(x).Height + 50
t1(x).Visible = True
Next x


For x = 0 To 9
t1(x + 1).Left = t1(x).Left + t1(x).Width + 50
Next x


Exit Sub

Errore:
MsgBox "Il controllo e' stato gia' generato", vbCritical, "KO"


End Sub


Private Sub Text1_LostFocus(Index As Integer)
Command1(Index).Caption = Text1(Index).Text
Check1(Index).Caption = Text1(Index).Text
End Sub










( controllidinamicicreabottoni.html )- by Paolo Puglisi - Modifica del 17/12/2023