[RESOLVED] Dim WithEvents for an command button array
What I want to do is to create a dynamic button array and use WithEvents for the events for the array. How can one do this?
What I have Tried is
Dim WithEvents cmdButtons as CommandButton
This works as it should for one button but
Dim WithEvents cmdButtons() as CommandButton
is a VB no-no...
I am using Controls.Add to create the buttons and what I want is the events sub to handle the button array, like such:
Private Sub cmdButtoms(Index)
How to?
Re: Dim WithEvents for an command button array
The easiest way is to create a template control at design time (which you don't actually use), and use Load to create copies of it.
Alternatively, see the article about this in our Classic VB FAQs (in the Misc. section), which explains other methods.
Re: Dim WithEvents for an command button array
Yes, I am aware of that method but I wanted to do a little something different this time... It helps the learning experiences... Any ideas?
Re: Dim WithEvents for an command button array
There are at least two work-arounds described in the WithEvents FAQ article.. I haven't seen any others (if I had, I would have added them there).
Re: Dim WithEvents for an command button array
Where is this "WithEvents FAQ" article?
Re: Dim WithEvents for an command button array
Go up a level in the forums, VB FAQs section. Here is the direct link
Re: Dim WithEvents for an command button array
Re: Dim WithEvents for an command button array
I don't believe that you can use WithEvents on a control array.
Re: Dim WithEvents for an command button array
Yes, not directly I found out.
Re: Dim WithEvents for an command button array
MartinLiss, one can't. si_the_geek tried to direct poster to a method to workaround the problem, by using an implementation. I have used that same procedure before with satisfactory results.
Re: Dim WithEvents for an command button array
I don't see why anyone would want to take the troble to use a workaround instead of using the method si described in post#2. I would say however that there's no reason why the "template" control can't be used like any of the ones that are Load-ed. You just need to make it Visible/not Visible rather than loading/unloading it.
Re: Dim WithEvents for an command button array
Sometime one want to see if another way can be done "Just Because..." That is how new things and ways are found. Not an issue, just wanted to see if I was overlooking something.
Re: Dim WithEvents for an command button array
Well, here is one scenario (not the best example). A class adds controls to a host form, but the host form isn't required to provide a template control. What kind of controls woulld the class add? I don't know, maybe a few pictureboxes to act as sizing handles? That is just an example of course, there could be other reasons for the workaround...
Re: Dim WithEvents for an command button array
That's fine - I have some masochistic tendencies myself:)