born4code
asked on
A Form Handler in VB6?
Does anyone know of a way to handle the events of a VB6 form with a class file?
For example, a command button is clicked, but it goes to a class file instead?
Thanks
For example, a command button is clicked, but it goes to a class file instead?
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
egl1044 ---
I found that if I remove you given code piece of
Private Sub Form_Load()
Set c = New Class1
End Sub
... then the event only fires ONCE. But if I don't then it fires twice. I think that is because you have it in the Class_Initialize event...
So in my form... I only have
Private MyClass As clsMessageClass
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Set MyClass = Nothing
End Sub
That's it for the form! So... is this okay?
Also, what if I have a PUBLIC variable created at startup like this....
Public MyClass as clsMessageClass (in my project startup somewhere)
Then... I suppose I need NOTHING in the form at all???
Thanks for the help!
I found that if I remove you given code piece of
Private Sub Form_Load()
Set c = New Class1
End Sub
... then the event only fires ONCE. But if I don't then it fires twice. I think that is because you have it in the Class_Initialize event...
So in my form... I only have
Private MyClass As clsMessageClass
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Set MyClass = Nothing
End Sub
That's it for the form! So... is this okay?
Also, what if I have a PUBLIC variable created at startup like this....
Public MyClass as clsMessageClass (in my project startup somewhere)
Then... I suppose I need NOTHING in the form at all???
Thanks for the help!
egl1044s method will only work with ONE form...the default instance of Form1. If you create more than one instance of Form1 then it won't work with the additional instances.
ASKER
I'm not following you....
I have not created any instances of Form1, have I?
Do you mean Form_Load or Activate?
Could you elaborate a little?
I have not created any instances of Form1, have I?
Do you mean Form_Load or Activate?
Could you elaborate a little?
Actually I dont see how it would work without setting the object.
Idle mind is refering to instances of form1 that you would call like this
dim f as form
set f = new form1 'new instance
but i wrote the code to just bind to the default form1
However over here the event isn't firing twice for me. Do you have more code maybe something is conflicting?
dim f as form
set f = new form1 'new instance
but i wrote the code to just bind to the default form1
However over here the event isn't firing twice for me. Do you have more code maybe something is conflicting?
ASKER
I went and looked a little closer.
I had the class inside of another class...
MasterClass
- clsSomeClass1
- clsSomeClass2
- clsThisClass <<< it gets instantiated at startup
So, I don't even use a private, or public variable at all.
Neither do I "set" the class to anything, just let the MasterClass instantiate it.
Simply, when my MasterClass starts up, it initializes the Class (cls) and that's it!
So, I think what was happening, was it was doing it once for the Class Initialization in both the MasterClass and the Form. So, I can pick/use either method. Frankly, I prefer just letting my MasterClass initialize the subclass and do all the cleanup work, and just leave my form code-behind blank!
I will keep note about the Idle_Mind's comments... but I really don't do that much, and I much rather prefer having less code behind the form.
What do you think?
I had the class inside of another class...
MasterClass
- clsSomeClass1
- clsSomeClass2
- clsThisClass <<< it gets instantiated at startup
So, I don't even use a private, or public variable at all.
Neither do I "set" the class to anything, just let the MasterClass instantiate it.
Simply, when my MasterClass starts up, it initializes the Class (cls) and that's it!
So, I think what was happening, was it was doing it once for the Class Initialization in both the MasterClass and the Form. So, I can pick/use either method. Frankly, I prefer just letting my MasterClass initialize the subclass and do all the cleanup work, and just leave my form code-behind blank!
I will keep note about the Idle_Mind's comments... but I really don't do that much, and I much rather prefer having less code behind the form.
What do you think?
ASKER
Oh Hey I need your help...
It won't work for Controls that are arrays!
Dim WithEvents CmdBtn As CommandButton <<< if array, then what?
It won't work for Controls that are arrays!
Dim WithEvents CmdBtn As CommandButton <<< if array, then what?
Right...WithEvents can only handle ONE control at a time so it can't be used with Control Arrays.
Let's take a step back.
Why do you want to handle the events in a Class?
Let's take a step back.
Why do you want to handle the events in a Class?
ASKER
Good question. Actually I've been doing a lot of dev with .net 05, and I feel like a fish out of water now going back to old vb projects. Likewise, I would like to separate the code from the forms for the sake of neatness... keeping things in a separate class, etc.. Plus, this particular project, could end up being converted into an asp.net project in the near future. All my asp.net projects use class handlers as well.
So, from asp.net I learned to keep my code away from prying eyes and out of the code-behind... and running everything from class handlers, classes, etc.., and it feels "organized".
Now, when looking at the old vb6 projects I just cringe because there is nothing even close to that functionality. I don't have the time or energy to convert projects, but at least would like to get them closer to the .net way of doing things. The old vb6 projects have code behind each form, code in modules, and code in a class or two. It's frustrating.
Plus, I have to admit that I just like class programming.
What are your thoughts on this? I would really like to read them.
So, from asp.net I learned to keep my code away from prying eyes and out of the code-behind... and running everything from class handlers, classes, etc.., and it feels "organized".
Now, when looking at the old vb6 projects I just cringe because there is nothing even close to that functionality. I don't have the time or energy to convert projects, but at least would like to get them closer to the .net way of doing things. The old vb6 projects have code behind each form, code in modules, and code in a class or two. It's frustrating.
Plus, I have to admit that I just like class programming.
What are your thoughts on this? I would really like to read them.
Unfortunately I don't have experience with asp.net...or much web related stuff at all for that matter...
I completely agree though with how frustrating it can be trying to do things the "right way" in VB6 after working with VB.Net.
To be honest, I just don't try to make things too "proper" in VB6 because most of the time the effort isn't worth it and often times the language can't support it anyways.
I use OOP in VB6 when it makes sense too. I wouldn't concern myself too much with making things better in your old projects. Just accept the VB6 code for what it is and move on...but make sure to develop your new VB.Net apps with an eye towards OOP!
I completely agree though with how frustrating it can be trying to do things the "right way" in VB6 after working with VB.Net.
To be honest, I just don't try to make things too "proper" in VB6 because most of the time the effort isn't worth it and often times the language can't support it anyways.
I use OOP in VB6 when it makes sense too. I wouldn't concern myself too much with making things better in your old projects. Just accept the VB6 code for what it is and move on...but make sure to develop your new VB.Net apps with an eye towards OOP!
ASKER
Perhaps.
Well this has been a good discussion.
Both of you guys have helped, I'll split up the points, wish I could give you both a lot more points.
Thanks.
Well this has been a good discussion.
Both of you guys have helped, I'll split up the points, wish I could give you both a lot more points.
Thanks.
ASKER
Well, the first method offered works (IdleMind)
The second method, I like because it reduces the code in the form.
HOWEVER... if fires the event TWICE. Can't figure out why.
What would cause it to fire twice when clicked?