?
Solved

Handling events from a control array in VB6

Posted on 2003-11-12
7
Medium Priority
?
2,321 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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 375 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 86

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
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.

 

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 375 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

809 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