Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

A Form Handler in VB6?

Posted on 2006-06-08
14
Medium Priority
?
377 Views
Last Modified: 2010-04-07
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
0
Comment
Question by:born4code
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
14 Comments
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1000 total points
ID: 16865634
Not sure if this is what you're looking for...

' ----------
'  Form1
' ----------
Private c As New Class1

Private Sub Form_Load()
    Set c.btn = Me.Command1
End Sub

' ----------
'  Class1
' ----------
Public WithEvents btn As CommandButton

Private Sub btn_Click()
    MsgBox "Button Clicked", vbOKOnly, "Class1"
End Sub
0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 1000 total points
ID: 16865666
'Sure..

Start new standard Exe project
'Add 1 Class module
'Add 1 command button to form

'--------- Paste into Class1.cls ---------

Option Explicit

Dim WithEvents CmdBtn As CommandButton

Private Sub Class_Initialize()
    Set CmdBtn = Form1.Command1
End Sub

Private Sub Class_Terminate()
    Set CmdBtn = Nothing
End Sub

Private Sub CmdBtn_Click()
    MsgBox "hello world!"
End Sub


'-------- Paste into Form1 ---------

Option Explicit

Dim c As Class1

Private Sub Form_Load()
    Set c = New Class1
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Set c = Nothing
End Sub

0
 
LVL 2

Author Comment

by:born4code
ID: 16866002
Hmmm.

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?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Author Comment

by:born4code
ID: 16866049
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!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16866069
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.
0
 
LVL 2

Author Comment

by:born4code
ID: 16866110
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?
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 16866119
Actually I dont see how it would work without setting the object.
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 16866131
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?
0
 
LVL 2

Author Comment

by:born4code
ID: 16866458
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?
0
 
LVL 2

Author Comment

by:born4code
ID: 16866480
Oh Hey I need your help...

It won't work for Controls that are arrays!

Dim WithEvents CmdBtn As CommandButton  <<< if array, then what?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16866558
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?
0
 
LVL 2

Author Comment

by:born4code
ID: 16866886
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.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16867022
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!
0
 
LVL 2

Author Comment

by:born4code
ID: 16867131
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question