Author Topic: Ad events for dynamic controls  (Read 2314 times)

0 Members and 1 Guest are viewing this topic.

Ricky_76

  • Guest
Ad events for dynamic controls
« on: March 28, 2006, 07:22:24 PM »
Hi, I have this problem:

I made a function to dynamically create label and text controls
but i can't associate any event to each control I dynamically create with the function!

Can someone help me, please!

Function ad_Control(lbl As String)
Set COM_label = FRM_TavoleUpdate.mp_01.Pages(0).Controls.Add("Forms.Label.1", "lbl_" lbl, Visible)
With COM_label
    .Left = 5
    .Height = step
    .Top = startY
    .Width = 85
    .Caption = lbl
    .Enabled = True
    .WordWrap = False
    .TextAlign = fmTextAlignRight
End With
Set COM_txt = FRM_TavoleUpdate.mp_01.Pages(0).Controls.Add("Forms.TextBox.1", "txt_" & lbl, Visible)
 With COM_txt
    .Left = 120
    .Height = step
    .Top = startY
    .Width = 85
    .Enabled = True
    .WordWrap = False
End With
startY = startY + step
End Function

mmelone

  • Guest
Re: Ad events for dynamic controls
« Reply #1 on: March 28, 2006, 07:35:07 PM »
In order to respond to events for dynamically created controls, you need to declare a variable for that control in the general declarations section of the code.

For example, here are some excerps from a class I wrote.  It basically populates a picturebox with controls that it generates dynamically.  It was written for VB but the same ideas apply for VBA.

In the General Declarations section
Code: [Select]
Private lUserLabel As Label
Private WithEvents lUserTextbox As TextBox
Private lDateLabel As Label
Private WithEvents lDateTextbox As TextBox
Private lTypeLabel As Label
Private WithEvents lTypeTextbox As TextBox
Private lDescriptionLabel As Label
Private WithEvents lDescriptionTextbox As RichTextBox
Private lIDLabel As Label
Private WithEvents lIDTextbox As TextBox
Private lLine As Line
Private WithEvents lContainer As PictureBox
Private lColor As Long

Code: [Select]
Public Sub Init()
On Error GoTo Trap
ErrorTypes.Form.Controls.Remove "cPicturebox" & ErrorTypes.Count
Set lContainer = ErrorTypes.Form.Controls.Add("VB.Picturebox", "cPicturebox" & ErrorTypes.Count, ErrorTypes.PBox)
ErrorTypes.Form.Controls.Remove "cIDLabel" & ErrorTypes.Count
Set lIDLabel = ErrorTypes.Form.Controls.Add("VB.Label", "cIDLabel" & ErrorTypes.Count, lContainer)
ErrorTypes.Form.Controls.Remove "cIDTextbox" & ErrorTypes.Count
Set lIDTextbox = ErrorTypes.Form.Controls.Add("VB.Textbox", "cIDTextbox" & ErrorTypes.Count, lContainer)
ErrorTypes.Form.Controls.Remove "cUserLabel" & ErrorTypes.Count
Set lUserLabel = ErrorTypes.Form.Controls.Add("VB.Label", "cUserLabel" & ErrorTypes.Count, lContainer)
ErrorTypes.Form.Controls.Remove "cUserTextbox" & ErrorTypes.Count
Set lUserTextbox = ErrorTypes.Form.Controls.Add("VB.Textbox", "cUserTextbox" & ErrorTypes.Count, lContainer)
ErrorTypes.Form.Controls.Remove "cDateLabel" & ErrorTypes.Count
Set lDateLabel = ErrorTypes.Form.Controls.Add("VB.Label", "cDateLabel" & ErrorTypes.Count, lContainer)
ErrorTypes.Form.Controls.Remove "cDateTextbox" & ErrorTypes.Count
Set lDateTextbox = ErrorTypes.Form.Controls.Add("VB.Textbox", "cDateTextbox" & ErrorTypes.Count, lContainer)
ErrorTypes.Form.Controls.Remove "cTypeLabel" & ErrorTypes.Count
Set lTypeLabel = ErrorTypes.Form.Controls.Add("VB.Label", "cTypeLabel" & ErrorTypes.Count, lContainer)
ErrorTypes.Form.Controls.Remove "cTypeTextbox" & ErrorTypes.Count
Set lTypeTextbox = ErrorTypes.Form.Controls.Add("VB.Textbox", "cTypeTextbox" & ErrorTypes.Count, lContainer)
ErrorTypes.Form.Controls.Remove "cDescriptionLabel" & ErrorTypes.Count
Set lDescriptionLabel = ErrorTypes.Form.Controls.Add("VB.Label", "cDescriptionLabel" & ErrorTypes.Count, lContainer)
ErrorTypes.Form.Controls.Remove "cDescriptionTextbox" & ErrorTypes.Count
Set lDescriptionTextbox = ErrorTypes.Form.Controls.Add("RichText.RichTextCTRL.1", "cDescriptionTextbox" & ErrorTypes.Count, lContainer)
With lContainer
    .Visible = True
    '.ScaleHeight = 1050
    'mvarHeight = .Height
    '.ScaleWidth = ErrorTypes.PBox.Width
    .Appearance = 0
    .BorderStyle = 0
    .BackColor = vbWhite
    End With
With lIDLabel
    .Visible = True
    .Caption = " " & mvarSType & " ID: "
    .BackColor = 14408667
    .AutoSize = True
    End With
With lIDTextbox
    .Visible = True
    .Text = mvarID
    .BorderStyle = 0
    .BackColor = 14408667
    .Height = lIDLabel.Height
    .Locked = True
    End With
With lUserLabel
    .Visible = True
    .Caption = "User: "
    .BackColor = 14408667
    '.AutoSize = True
    End With
With lUserTextbox
    .Visible = True
    .Text = mvarUser
    .BorderStyle = 0
    .BackColor = 14408667
    .Height = lUserLabel.Height
    .Locked = True
    End With
With lDateLabel
    .Visible = True
    .Caption = "Date: "
    .BackColor = 14408667
    '.AutoSize = True
    End With
With lDateTextbox
    .Visible = True
    .Text = mvarTimestamp
    .BorderStyle = 0
    .BackColor = 14408667
    .Height = lDateLabel.Height
    .Locked = True
    End With
With lTypeLabel
    .Visible = True
    .Caption = " Label: "
    .BackColor = vbWhite
    '.AutoSize = True
    End With
With lTypeTextbox
    .Visible = True
    .Text = mvarEType
    .Height = lTypeLabel.Height
    .Locked = True
    End With
With lDescriptionLabel
    .Visible = True
    .Caption = "Notes: "
    .BackColor = vbWhite
    '.AutoSize = True
    End With
With lDescriptionTextbox
    .Visible = True
    .Text = mvarDescription
    .Height = (((ErrorTypes.Form.TextWidth(mvarDescription) - 150) \ .Width) + 1) * ErrorTypes.Form.TextHeight(mvarDescription) + 150 'lDescriptionLabel.Height
    .Locked = True
    End With
Exit Sub
Trap:
Select Case Err.Number
    Case 730
        Resume Next
    Case Else
        Exit Sub
    End Select

mmelone

  • Guest
Re: Ad events for dynamic controls
« Reply #2 on: March 28, 2006, 07:40:27 PM »
Oh yeah, here is the lContainer_Resize() event.  (lContainer is the variable I declared in the general declarations section)

Code: [Select]
Private Sub lContainer_Resize()
On Error Resume Next
lContainer.ScaleWidth = ErrorTypes.PBox.Width
lIDLabel.Move 0, 0
lIDLabel.AutoSize = True
lIDTextbox.Move lIDLabel.Left + lIDLabel.Width, lIDLabel.Top, lContainer.ScaleWidth / 3 - lIDLabel.Width, 200
lUserLabel.Move lContainer.ScaleWidth / 3, lIDLabel.Top, 500, 200
lUserTextbox.Move lUserLabel.Left + lUserLabel.Width, lUserLabel.Top, lContainer.ScaleWidth / 3 - lUserLabel.Width, 200
lDateLabel.Move lContainer.ScaleWidth * 2 / 3, lUserLabel.Top, 500, 200
lDateTextbox.Move lDateLabel.Left + lDateLabel.Width, lDateLabel.Top, lContainer.ScaleWidth / 3 - lDateLabel.Width, 200
lTypeLabel.Move lIDLabel.Left, lUserTextbox.Top + lUserTextbox.Height + 75, 500, 200
lTypeTextbox.Move lTypeLabel.Left + lTypeLabel.Width, lTypeLabel.Top - 25, lContainer.ScaleWidth - lTypeLabel.Width - 100, 200
lDescriptionLabel.Move lTypeLabel.Left, lTypeTextbox.Top + lTypeTextbox.Height + 75, 500, 200
lDescriptionTextbox.Move lDescriptionLabel.Left + lDescriptionLabel.Width, lDescriptionLabel.Top - 25, lContainer.ScaleWidth - lDescriptionLabel.Width - 100 ', ((ErrorTypes.Form.TextWidth(mvarDescription) \ lDescriptionTextbox.Width) + 1) * ErrorTypes.Form.TextHeight(mvarDescription) + 150 ', 200 ',  lTypeTextbox.Height * 2 - 100
LineCount = SendMessage(lDescriptionTextbox.hwnd, &HBA, 0, ByVal 0&)
lDescriptionTextbox.Height = LineCount * ErrorTypes.Form.TextHeight("Zy") + 150
lContainer.Height = lDescriptionTextbox.Top + lDescriptionTextbox.Height + 50
mvarHeight = lContainer.Height
End Sub

Now, you may want to look into control arrays.  I'm not sure if you can declare something like this

Code: [Select]
Private WithEvents lContainer() as PictureBox
and have it result in the variable equivalent of a control array.

Ricky_76

  • Guest
Re: Ad events for dynamic controls
« Reply #3 on: March 29, 2006, 03:53:58 AM »
 :-D
Tnx guy, I'll try it!
I let you know!
/r