I've coded myself into a corner, and I don't even know which terms/words to use to search for a solution to solve my problem. My original goal was that I noticed I was using the same kinds of controls and control names for multiple forms: lblTitleBar, lblCmd1, lblCmd2, cmdTmr, etc. I figured that I could consolidate this duplicated code into a class and create an instance on each form. At that point, my basic controls would be setup, formatted, and ready to use for each form. As it stands now, the visible GUI part is working as planned, but I can't figure out how to now capture the events for these objects. Am I not setting them up properly? Is there a way I can pass basic VB events (Click, DblClick, MouseMove, etc) to the class or reference these events on the parent form?
Now, I've seen that I can create the variables on each form and then, they will work as planned, but it would kinda defeat the purpose of trying to centralize the code if I still need to have multiple variable declarations, etc per form, no?
Now, all I want to do is know when someone clicks or when the mouse is over one of these newly created labels...VB Code:
Option Explicit 'clsForm Public lblTitleBar As Label Public lblCmd1 As Label Public lblCmd2 As Label Private foForm As Form Private cmdTmr As Timer Private shpTitleBar As Shape Private shpForm As Shape Public Sub NewObject(ByRef roForm As Form) Dim li As Integer Set foForm = roForm Set lblTitleBar = foForm.Controls.Add("VB.Label", "lblTitleBar") Set lblCmd1 = foForm.Controls.Add("VB.Label", "lblCmd1") Set lblCmd2 = foForm.Controls.Add("VB.Label", "lblCmd2") Set cmdTmr = foForm.Controls.Add("VB.Timer", "cmdTmr") Set shpTitleBar = foForm.Controls.Add("VB.Shape", "shpTitleBar") Set shpForm = foForm.Controls.Add("VB.Shape", "shpForm") ' Label Objects With lblTitleBar ' Dimensions .Top = 2 .Left = 2 .Width = roForm.ScaleWidth - 4 .Height = 19 ' Visibility .Visible = True .BorderStyle = 0 ' Color .BackColor = &H808080 .ForeColor = &H80000009 ' Text .Caption = "Test_Form" .Font.Bold = True .Font.Name = "MS Sans Serif" End With With lblCmd1 ' Dimensions .Width = 16 .Height = 16 .Top = lblTitleBar.Top + 1 .Left = roForm.ScaleWidth - 3 - .Width ' Visibility .Visible = True .BackStyle = 0 ' Transparent .BorderStyle = 1 ' Fixed Single .Appearance = 0 ' Flat .Alignment = 2 ' Centered ' Text .Caption = "X" .Font.Bold = True .Font.Name = "Verdana" .ZOrder End With With lblCmd2 ' Dimensions .Width = 16 .Height = 16 .Top = lblTitleBar.Top + 1 .Left = roForm.ScaleWidth - 4 - (.Width * 2) ' Visibility .Visible = True .BackStyle = 0 ' Transparent .BorderStyle = 1 ' Fixed Single .Appearance = 0 ' Flat .Alignment = 2 ' Centered ' Text .Caption = "_" .Font.Bold = True .Font.Name = "Verdana" .ZOrder End With ' Shape Objects With shpTitleBar .Left = 1 .Top = 1 .Width = roForm.ScaleWidth - 2 .Height = lblTitleBar.Height + 2 .Visible = True End With With shpForm .Left = 1 .Top = shpTitleBar.Height + shpTitleBar.Top + 1 .Width = shpTitleBar.Width .Height = roForm.ScaleHeight - lblTitleBar.Height - 4 .Visible = True End With End Sub
( I've attached a sample screenshot to show how the GUI appears. If you don't click on one of the labels, Form_Click(), etc will fire. )VB Code:
Option Explicit ' Main Form Private foFormCls As clsForm Private Sub Form_Load() Set foFormCls = New clsForm Call foFormCls.NewObject(Me) End Sub Private Sub lblTitleBar_Click() Msgbox "Doesn't Work" End Sub
