Solved

A Form Handler in VB6?

Posted on 2006-06-08
14
365 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
  • 7
  • 4
  • 3
14 Comments
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 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 250 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
 
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 85

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 85

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 85

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now