Results 1 to 13 of 13

Thread: [RESOLVED] Class Events?

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member Fedhax's Avatar
    Join Date
    Aug 2006
    Posts
    293

    Resolved [RESOLVED] Class Events?

    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?

    VB Code:
    1. Option Explicit
    2.  
    3. 'clsForm
    4. Public lblTitleBar          As Label
    5. Public lblCmd1              As Label
    6. Public lblCmd2              As Label
    7.  
    8. Private foForm              As Form
    9.  
    10. Private cmdTmr              As Timer
    11.  
    12. Private shpTitleBar         As Shape
    13. Private shpForm             As Shape
    14.  
    15. Public Sub NewObject(ByRef roForm As Form)
    16.    
    17.     Dim li          As Integer
    18.  
    19.     Set foForm = roForm
    20.  
    21.     Set lblTitleBar = foForm.Controls.Add("VB.Label", "lblTitleBar")
    22.    
    23.     Set lblCmd1 = foForm.Controls.Add("VB.Label", "lblCmd1")
    24.     Set lblCmd2 = foForm.Controls.Add("VB.Label", "lblCmd2")
    25.    
    26.     Set cmdTmr = foForm.Controls.Add("VB.Timer", "cmdTmr")
    27.    
    28.     Set shpTitleBar = foForm.Controls.Add("VB.Shape", "shpTitleBar")
    29.     Set shpForm = foForm.Controls.Add("VB.Shape", "shpForm")
    30.    
    31.     ' Label Objects
    32.     With lblTitleBar
    33.    
    34.         ' Dimensions
    35.         .Top = 2
    36.         .Left = 2
    37.         .Width = roForm.ScaleWidth - 4
    38.         .Height = 19
    39.        
    40.         ' Visibility
    41.         .Visible = True
    42.         .BorderStyle = 0
    43.        
    44.         ' Color
    45.         .BackColor = &H808080
    46.         .ForeColor = &H80000009
    47.        
    48.         ' Text
    49.         .Caption = "Test_Form"
    50.         .Font.Bold = True
    51.         .Font.Name = "MS Sans Serif"
    52.        
    53.     End With
    54.    
    55.     With lblCmd1
    56.                
    57.         ' Dimensions
    58.         .Width = 16
    59.         .Height = 16
    60.         .Top = lblTitleBar.Top + 1
    61.         .Left = roForm.ScaleWidth - 3 - .Width
    62.        
    63.         ' Visibility
    64.         .Visible = True
    65.         .BackStyle = 0      ' Transparent
    66.         .BorderStyle = 1    ' Fixed Single
    67.         .Appearance = 0     ' Flat
    68.         .Alignment = 2      ' Centered
    69.                    
    70.         ' Text
    71.         .Caption = "X"
    72.         .Font.Bold = True
    73.         .Font.Name = "Verdana"
    74.        
    75.         .ZOrder
    76.        
    77.     End With
    78.                
    79.  
    80.     With lblCmd2
    81.    
    82.         ' Dimensions
    83.         .Width = 16
    84.         .Height = 16
    85.         .Top = lblTitleBar.Top + 1
    86.         .Left = roForm.ScaleWidth - 4 - (.Width * 2)
    87.        
    88.         ' Visibility
    89.         .Visible = True
    90.         .BackStyle = 0      ' Transparent
    91.         .BorderStyle = 1    ' Fixed Single
    92.         .Appearance = 0     ' Flat
    93.         .Alignment = 2      ' Centered
    94.                    
    95.         ' Text
    96.         .Caption = "_"
    97.         .Font.Bold = True
    98.         .Font.Name = "Verdana"
    99.        
    100.         .ZOrder
    101.                
    102.     End With
    103.  
    104.     ' Shape Objects
    105.     With shpTitleBar
    106.         .Left = 1
    107.         .Top = 1
    108.         .Width = roForm.ScaleWidth - 2
    109.         .Height = lblTitleBar.Height + 2
    110.         .Visible = True
    111.     End With
    112.  
    113.     With shpForm
    114.         .Left = 1
    115.         .Top = shpTitleBar.Height + shpTitleBar.Top + 1
    116.         .Width = shpTitleBar.Width
    117.         .Height = roForm.ScaleHeight - lblTitleBar.Height - 4
    118.         .Visible = True
    119.     End With
    120.  
    121. End Sub
    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:
    1. Option Explicit
    2. ' Main Form
    3. Private foFormCls       As clsForm
    4.  
    5. Private Sub Form_Load()
    6.    
    7.     Set foFormCls = New clsForm
    8.    
    9.     Call foFormCls.NewObject(Me)
    10.  
    11. End Sub
    12.  
    13. Private Sub lblTitleBar_Click()
    14.    Msgbox "Doesn't Work"
    15. 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. )
    Attached Images Attached Images  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width