?
Solved

Handling events from a control array in VB6

Posted on 2003-11-12
7
Medium Priority
?
2,245 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
[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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

765 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