[Mono-list] Ported code from VB6 to VB.NET problem

Rolf Bjarne Kvinge rolf at xamarin.com
Thu Mar 21 22:01:47 UTC 2013


On Thu, Mar 21, 2013 at 4:57 PM, Paul Johnson
<paul at all-the-johnsons.co.uk> wrote:
> Hi,
> I've ported some of my old code from VB6 to VB.NET and have fixed just about
> all of the issues except for one.
> In VB6, there are collection arrays (so you can have the likes of an array
> of buttons etc). To get around the lack of these in VB.NET, I'm using a List
> for them
> For example
> Dim WithEvents cmdButtons As New List(Of Button)
> then in the Load event
> cmdButtons.AddRange(New Button() {_cmdButton_0, _cmdButton_1})
> etc
> This works fine. The problem is with the event handling. You obviously can't
> trigger a Click event (say) on a List.
> Short of creating a tonne of the same methods to fire off the events for
> each cmdButton, is there some generic way that everything within that List
> fires off the event?

The easiest solution is to not use WithEvents, but attach event
handlers manually (in any case your WithEvents declaration is wrong,
you'd be listening for events on the List, not events from the
individual buttons).

For Each button In cmdButtons
    AddHandler button.Click, cmdButton_Click


> Paul
> --
> "Space," it says, "is big. Really big. You just won't believe how vastly,
> hugely, mindbogglingly big it is. I mean, you may think it's a long way down
> the road to the chemist's, but that's just peanuts to space, listen..."
> Hitch Hikers Guide to the Galaxy, a truly remarkable book!
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list

More information about the Mono-list mailing list