Results 1 to 13 of 13

Thread: [RESOLVED] Class Events?

  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  

  2. #2

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

    Re: Class Events?

    I know that this solution works, but surely there is a better way?
    VB Code:
    1. Option Explicit
    2. ' Main Form
    3. Dim WithEvents foFormCls       As clsForm
    4. Dim WithEvents lblTitleBar     As Label
    5.  
    6. Private Sub Form_Load()
    7.    
    8.     Set foFormCls = New clsForm
    9.     Call foFormCls.NewObject(Me)
    10.    
    11.     Set lblTitleBar = foFormCls.lblTitleBar
    12.  
    13. End Sub
    14.  
    15. Private Sub Form_Unload(Cancel As Integer)
    16.  
    17.     Set foFormCls = Nothing
    18.  
    19. End Sub
    20.  
    21. Private Sub lbltitlebar_Click()
    22.  
    23.     MsgBox "Here2"
    24.  
    25. End Sub

  3. #3
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Class Events?

    make a basic form with your GUI and all the setup code already in place - then save it as a template and you won't have to recode it everytime (or mess around with a class)

  4. #4

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

    Re: Class Events?

    Quote Originally Posted by bushmobile
    make a basic form with your GUI and all the setup code already in place - then save it as a template and you won't have to recode it everytime (or mess around with a class)
    It may accomplish the same feat as what I want, but I'm confused as to how you transfer the "setup code" between the template and the instances. Also, the form properties will be different between each form, but the controls follow basic formulas to fit onto the form. Could you provide me with an example?

    It isn't something along of the line of:
    VB Code:
    1. Private WithEvents foForm     as Form
    2.  
    3. Private Sub Form_Load()
    4.  
    5.    Set foForm = frmTemplate
    6.  
    7. End Sub

  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Class Events?

    what i meant is that when you go to Project | Add Form you have a number of templates that you can choose from

    create your own and stick it in the Template folder:
    C:\Program Files\Microsoft Visual Studio\VB98\Template\Forms
    in my case.

    that's just one option.

  6. #6

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

    Re: Class Events?

    Quote Originally Posted by bushmobile
    what i meant is that when you go to Project | Add Form you have a number of templates that you can choose from

    create your own and stick it in the Template folder:
    C:\Program Files\Microsoft Visual Studio\VB98\Template\Forms
    in my case.

    that's just one option.
    Oh, I see. Interesting thought. I guess as long as I didn't want to make changes to the code it would work. However, my "template" form includes some kinds of form validation code, form/window docking, mouseover functionality too. If push comes to shove, you're right that I can make a template and have this basic functionality ready to be enhanced. If I went the route of using classes & events, how would it work that way?

  7. #7

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

    Re: Class Events?

    Quote Originally Posted by Fedhax
    If I went the route of using classes & events, how would it work that way?
    Bump?

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Class Events?

    I am confused whats the problem in simple form?

  9. #9

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

    Re: Class Events?

    Quote Originally Posted by penagate
    I am confused whats the problem in simple form?
    Using code that I've found on this forum and other forums, I have added enhancements: Windows/Border Docking, MouseOver functionality, etc that I would like to carry across all of my forms for this application. After developing 3 forms, I found myself doing a copy/paste on each new form to carry forward this enhanced functionality. Then, it dawned on me if I needed to fix/correct/enhance this functionality, I'd have to make the changes on each form individually. This line of thought led me to ask how I could write the code for this functionality once and use it multiple times which made me think of Global functions (kludgy?) or a Class.

    The problems with a class is capturing the form's events and passing it on to the class which led me to writing code like:
    VB Code:
    1. Public WithEvents foFormCls             As clsForm
    2.  
    3. Private WithEvents lblTitleBar          As Label
    4. Private WithEvents lblCmdEnd            As Label
    5. Private WithEvents lblCmdMinimize       As Label
    6.  
    7. Private WithEvents cmdTmr               As Timer
    8.  
    9. '*************************************************
    10. '***    Form
    11. '*************************************************
    12.  
    13. Private Sub Form_Load()
    14.    
    15.     Set foFormCls = New clsForm
    16.         Call foFormCls.NewObject(Me)
    17.         Call foFormCls.Set_WindowState(geDisplayType.Frame)
    18.    
    19.         Set lblTitleBar = foFormCls.lblTitleBar
    20.         Set lblCmdEnd = foFormCls.lblCmdEnd
    21.         Set lblCmdMinimize = foFormCls.lblCmdMinimize
    22.    
    23.         Set cmdTmr = foFormCls.cmdTmr
    24.     ' Finished with Class
    25.  
    26. End Sub
    27.  
    28. Private Sub Form_GotFocus()
    29.  
    30.     Call foFormCls.Form_GotFocus
    31.  
    32. End Sub
    33.  
    34. Private Sub Form_LostFocus()
    35.  
    36.     Call foFormCls.Form_LostFocus
    37.  
    38. End Sub
    39.  
    40. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    41.  
    42.     Call foFormCls.Form_MouseMove(Button, Shift, X, Y)
    43.  
    44. End Sub

    Ultimately, is this implementation the best way to do it or should I be looking into other avenues to use my form class and pass on events from the main form to the class?

  10. #10
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Class Events?

    The main solution I can see is to create a window completely through the API in each class. That way, you have complete control over how its drawn.

    I still have this example I gave to someone on these forums over a year ago. Check it out..

    chem
    Attached Files Attached Files

    Visual Studio 6, Visual Studio.NET 2005, MASM

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Class Events?

    Quote Originally Posted by Fedhax
    The problems with a class is capturing the form's events and passing it on to the class which led me to writing code like:

    [snip]

    Ultimately, is this implementation the best way to do it or should I be looking into other avenues to use my form class and pass on events from the main form to the class?
    I see now. You're halfway there as you have declared the class WithEvents on the form. You need to do the same in reverse as well so that the class receives and can handle the events of the form. You already have a variable reference in the class pointing to the form, it is just a matter of adding the WithEvents modifier. Then, you can code any form event handlers using the notation foForm_eventname.

  12. #12

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

    Resolved [Resolved] Class Events?

    Quote Originally Posted by penagate
    I see now. You're halfway there as you have declared the class WithEvents on the form. You need to do the same in reverse as well so that the class receives and can handle the events of the form. You already have a variable reference in the class pointing to the form, it is just a matter of adding the WithEvents modifier. Then, you can code any form event handlers using the notation foForm_eventname.
    Wow. Just wow... Now, my spaghetti code is in one class and the forms now use only 20-25 lines of code--with white spaces--to provide that functionality. It's tighter; it's cleaner; it works. I'm impressed. I'm sure that I'll see how else I can push the boundry, but for now, this issue is resolved.

    Thanks for the API code and assistance.

  13. #13
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] Class Events?

    I'm glad you solved it.

    Remember, if code ever feels messy, that means it is.

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