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:
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")
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)
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?
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?
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:
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?
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.
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.