click olayı tanımlama

Katılım
14 Kasım 2017
Mesajlar
618
Excel Vers. ve Dili
2010 Türkçe
Altın Üyelik Bitiş Tarihi
07-01-2024
Aşağıdaki kodda userformun içine dinamik olarak oluşturulan labellere click olayını ekleyemedim. Yardımcı olabilir misiniz ?

Kod:
Private WithEvents lbl As MSForms.Label

Private Sub UserForm_Initialize()
    Dim col As Integer
    Dim lbl As MSForms.Label
    Dim lbl2 As Object

    For col = 1 To 4
        ' Dış label
        Set lbl = Me.Controls.Add("Forms.Label.1", "lblCol" & col)
        With lbl
            .Left = 20
            .Top = 10 + (col - 1) * 40
            .BackColor = RGB(255, 255, 255)
            .Width = 500
            .Height = 30
        End With

        ' İç label
        Set lbl2 = Me.Controls.Add("Forms.Label.1", "lbl2Col" & col)
        With lbl2
            .Caption = "İç Etiket " & col
            .Left = lbl.Left + 20
            .Top = lbl.Top + 10
            .Height = 10
            .Font.Name = "Nunito"
            .Font.Size = 10
            .BackStyle = 0
            
        End With
    Next col
    
End Sub

Private Sub lbl2_Click()
With lbl
    .BackColor = RGB(255, 255, 0)
    End With
End Sub
 

MusaPEKEL

Altın Üye
Katılım
29 Ağustos 2016
Mesajlar
65
Excel Vers. ve Dili
2013
Altın Üyelik Bitiş Tarihi
16-01-2027
Kod:
Private WithEvents LabelsCollection As Collection

Private Sub UserForm_Initialize()
    Dim col As Integer
    Dim lbl As MSForms.Label
    Dim lbl2 As MSForms.Label

    ' Koleksiyonu oluştur
    Set LabelsCollection = New Collection

    For col = 1 To 4
        ' Dış label
        Set lbl = Me.Controls.Add("Forms.Label.1", "lblCol" & col)
        With lbl
            .Left = 20
            .Top = 10 + (col - 1) * 40
            .BackColor = RGB(255, 255, 255)
            .Width = 500
            .Height = 30
        End With

        ' İç label
        Set lbl2 = Me.Controls.Add("Forms.Label.1", "lbl2Col" & col)
        With lbl2
            .Caption = "İç Etiket " & col
            .Left = lbl.Left + 20
            .Top = lbl.Top + 10
            .Height = 10
            .Font.Name = "Nunito"
            .Font.Size = 10
            .BackStyle = 0
        End With

        ' Koleksiyona ekle
        LabelsCollection.Add lbl2
    Next col
End Sub

Private Sub lbl_MouseDown(ByVal Index As Integer, ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Dim clickedLabel As MSForms.Label
    Set clickedLabel = LabelsCollection(Index)

    ' Tıklanan etiketin rengini değiştir
    clickedLabel.BackColor = RGB(255, 255, 0)
End Sub
 
Katılım
14 Kasım 2017
Mesajlar
618
Excel Vers. ve Dili
2010 Türkçe
Altın Üyelik Bitiş Tarihi
07-01-2024
Kodu denediniz mi hocam. Hata veriyor.
 
Üst