Solved

Handling events from a control array in VB6

Posted on 2003-11-12
7
2,180 Views
Last Modified: 2007-12-19
Hi.  I'm using VB6.  On my main form there is a control array of TextBox's.

I have a class with which I would like to handle the events for this control array.  I'm setting up the association in this manner.

---- MyClass Start ----

Private m_editTextArray As Object

Private Sub Class_Initialize()
   Set m_editTextArray = fMainForm.editTextArray
End Sub

---- MyClass End ----

As you can see, I'm assigning the control array object to a member of my class.  I cannot handle events on the control array within my class..

Ideally, I'd like to be able to declare my text array, in the class, as:
Private WithEvents m_editTextArray As Object

I could then handle any events through m_editTextArray.
However, the compiler disallows this.  I've tried some variations using "As TextBox" (and WithEvents), but am having no luck.  I know that I could just declare one TextBox control per index (within my class) from the main form's control array, but that kind of defeats the purpose!

Anyone have an idea on how I can get the events to be fired and handled by my class?  Thanks.
0
Comment
Question by:jeffb022197
  • 3
  • 2
  • 2
7 Comments
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 125 total points
ID: 9736841
According to the help file for WithEvents: "You can declare as many individual variables as you like using WithEvents, but you can't createarrays with WithEvents."

So, unfortunately, I don't think it can be done in this manner.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9736853
What functionality were you trying to achieve with the class?  Perhaps we could help you come up with a different solution.

Idle_Mind
0
 
LVL 1

Expert Comment

by:InTheWind
ID: 9737043
I'm sorry, but I just don't see the advantage to doing the event handling this way... too complicated perhaps?

What I have done in the past is assigned functions to each event. Since the arrays events all fire concurrently, it's easy to handle them all at once.

For example, if you change the text in one text control the event that fires is ..._change(index as integer)...

So, you can handle the events by determining which text box had the change event through interogating the index. If the index is zero, then you know it's the text box with the index of zero... and you deal with it accordingly.

Please clarify you needs so that we may better understand and possibly assist.

Cheers!
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:jeffb022197
ID: 9739694
Thanks for your comments.  I'm, admittedly, not much of a VB guru.  Here's what I'm trying to accomplish:

I have an "explorer style" user interface with a tree control on the left.  When the user selects an item from the tree, the remaining client area of the UI will display a set of controls related to the selection.  I have about 16 different tree items (and hence 16 different sets of controls).

I was "grouping" controls within a picture box (up to 64 controls sometimes), and then simply hiding all the other picture boxes and displaying the one related to the tree selection.

This solution worked great except that my main form has *so* much code within it that it was becoming unmanageable.  I was hoping to refactor some things out and move them to separate modules or classes in attempt to clean up the code and make it readable and maintainable.

My goal here is to handle events related to each set of controls within its own module.  That will keep unrelated code separate, and a lot more manageable.

Any tips?  I realize this is a broad request, but you asked  :-)
Cheers.

     -jb
0
 
LVL 1

Accepted Solution

by:
InTheWind earned 125 total points
ID: 9740086
jb, I have done this kind of thing in the past, as a commercial application. What I did was use a tab control and had several arrays of text boxes/labels with their own functions/factors/conditions all logic was discriminated by the index of the control array.

To make it manageable, for you the programmer, you only really need to leave yourself notes within the code to determine the function of the item you're looking at. I had about 400 objects that were running through the same module to look for their own action, triggered by an 'on click' event. When that event was fired, it looked for the control through the index and then ran that items function, which was in a separate module.

Having some of the events using the same code, with different data provided by the original 'on click', can further streamline your code, and making as much of it reusable as possible will help a great deal in simplifying the job as well.

Moving it into separate classes will actually (initially) make the program more complicated, because you are moving things out of the event. But, OOP modularization is the best way to handle multiple decision trees in the long run.

Also, if you have data you are loading to respond to each event you can speed things up by loading it into a memory array, of a structure, at start up. It takes a little longer to load up, not that much really, but increases the speed of operation by several orders as you aren't doing disk read/writes. I did this by loading my entire database into memory, granted this was just a bunch of 0's and 1's in my case though.

I can't stress enough that you will need to make lots of comments...but, you've already figured that out I'm sure. :-)
0
 

Author Comment

by:jeffb022197
ID: 9742652
Thanks for the tips and info guys.  I split the points 50/50.

    -jb
0
 
LVL 1

Expert Comment

by:InTheWind
ID: 9743563
Thanks, didn't really do it for the points :)

Just liked the question and have had some similar experience.

Good luck.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…

706 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

20 Experts available now in Live!

Get 1:1 Help Now