动态控件的广告事件
嗨,我有这个问题:我创建了一个函数来动态创建标签和文本控件
但是我不能将任何事件与我用该函数动态创建的每个控件相关联!
谁能帮帮我,拜托!
函数ad _ Control(LBL As String)
Set COM _ label = FRM _ tavoleupdate . MP _ 01。Pages(0).Controls.Add("Forms。Label.1 "," lbl_" lbl,Visible)
与COM_label
。Left = 5
高度=步长。Top = startY。宽度= 85
。Caption = lbl。Enabled = True。WordWrap = False。text align = fmtextalign right
End With
Set COM _ txt = FRM _ tavoleupdate . MP _ 01。Pages(0).Controls.Add("Forms。TextBox.1 "," txt_" & lbl,Visible)
与COM_txt
。Left = 120
高度=步长。Top = startY。宽度= 85
。Enabled = True。word wrap = False
End With
startY = startY+step
End函数
**** Hidden Message ***** 为了响应动态创建控件的事件,您需要在代码的常规声明部分中为该控件声明一个变量。
例如,这里是我编写的一个类的一些摘录。它基本上用它动态生成的控件填充了一个图片框。它是为VB编写的,但同样的想法适用于VBA。
在常规声明部分
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
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
哦,是的,这是lContainer_Resize()事件。(lContainer是我在一般声明部分声明的变量)
现在,您可能需要研究控件数组。我不确定您是否可以声明这样的
并使其产生与控件数组等效的变量。
Tnx家伙,我会试试的!
我让你知道!
/r
页:
[1]