vb6 control arrays in .net?
Asked Answered
I

4

4

Are control arrays supported in .Net? We are talking about converting a legacy app from VB6 to .NET. The app has a lot of control arrays. I've read different articles that differ if .NET support control arrays.

Can anyone give me a definitive answer if .Net support control arrays like VB6 does? Is this for the latest version of .Net 4.0? Or is there a version limitation?

Also, if this is possible is it a straight conversion? If not what flaming hoops would one have to jump through to make this happen?

Implead answered 21/4, 2011 at 0:15 Comment(3)
See also these near duplicates https://mcmap.net/q/1164010/-control-array-in-vb-net and https://mcmap.net/q/1164010/-control-array-in-vb-netBrave
VB6 -> VB.Net is rarely a straight conversion, there is lots of discussion of the flaming hoops on the vb6-migration tag. You may wish to order some flame-resistant underwear before approaching the hoops (or reading some of the posts) :(Brave
possible duplicate of What's the simplest .NET equivalent of a VB6 control array?Pertussis
H
2

A "straight conversion" is not possible, but you can create control arrays in a different way: Creating Control Arrays in Visual Basic .NET and Visual C# .NET

Hawkes answered 21/4, 2011 at 0:20 Comment(4)
I noticed this article is written in '03. I would imagine that 4.0 supports this as well?Implead
+1 It is a good article. It is 2011 now, and one obvious improvement is to use generics. Rather than creating a new class for every control type, there could be a generic class to do most of the donkey work. Maybe there could be a generic BaseControlArray(Of T) which inherits List(Of T), not CollectionBase as in the article. Then ButtonArray would inherit BaseControlArray(Of Button) and expose the events, which will be different depending on the type of control.Brave
... Also the Microsoft.VisualBasic.Compatibility library includes some replacements for control arrays, but it has been dropped in .Net 4, so you might wish to avoid it. It was used automatically by Microsoft's VB6 -> VB.Net upgrade wizard, which has been dropped in VS2010. In 2003 when the article was written, Microsoft.VisualBasic.Compatibility did not include these replacements - now that the library's been dropped, that article looks useful again!Brave
When you say "you might wish to avoid it". Are you referring to the compatibility library or .Net 4 or Control arrays?Implead
D
3

VB.NET has no trouble with arrays of controls. The only thing that's missing is that the designer doesn't support them. Easily worked around with code. Like this:

Public Class Form1
    Private TextBoxArray() As TextBox

    Public Sub New()
        InitializeComponent()
        TextBoxArray = New TextBox() { TextBox1, TextBox2, TextBox3 }
    End Sub

End Class
Demiurge answered 21/4, 2011 at 0:24 Comment(1)
Only part of the puzzle. The OP will need unified event handlers too.Brave
H
2

A "straight conversion" is not possible, but you can create control arrays in a different way: Creating Control Arrays in Visual Basic .NET and Visual C# .NET

Hawkes answered 21/4, 2011 at 0:20 Comment(4)
I noticed this article is written in '03. I would imagine that 4.0 supports this as well?Implead
+1 It is a good article. It is 2011 now, and one obvious improvement is to use generics. Rather than creating a new class for every control type, there could be a generic class to do most of the donkey work. Maybe there could be a generic BaseControlArray(Of T) which inherits List(Of T), not CollectionBase as in the article. Then ButtonArray would inherit BaseControlArray(Of Button) and expose the events, which will be different depending on the type of control.Brave
... Also the Microsoft.VisualBasic.Compatibility library includes some replacements for control arrays, but it has been dropped in .Net 4, so you might wish to avoid it. It was used automatically by Microsoft's VB6 -> VB.Net upgrade wizard, which has been dropped in VS2010. In 2003 when the article was written, Microsoft.VisualBasic.Compatibility did not include these replacements - now that the library's been dropped, that article looks useful again!Brave
When you say "you might wish to avoid it". Are you referring to the compatibility library or .Net 4 or Control arrays?Implead
C
1

You can have arrays of controls but they are not as built in as control arrays were in vb6. You can however create arrays of controls or have a unified event handlers similar to vb6.

Clarkia answered 21/4, 2011 at 0:20 Comment(0)
R
0

I think I found the solution, I'm not the only former VB6 developer who has struggled with this limitation. A long time ago, I tried to migrate software, but I failed because it had a tough dependency on control arrays. I read many forums and I was able to write this simple code:

Public Class Form1

'To declare the List of controls
Dim labels As New List(Of Label)()

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'To get all controls in the form
    For Each control In Me.Controls
        'To search for the specific type that you want to create the array 
        If control.[GetType]().Name.Contains("Label") Then
            'To add the control to the List
            labels.Add(DirectCast(control, Label))
        End If
    Next
    'To sort the labels by the ID
    labels = labels.OrderBy(Function(x) x.Name).ToList()
End Sub
End Class

I used a List for convenient reasons, but with that piece of code, you can create in design time the controls that you need and while you keep the "index" as the last characters (label1, label2, ..., labelN)

Many labels in a window form

Later, you can iterate them with the loop and add them in the blink of an eye. Next, you are going to be able to manipulate them from the object with labels(0), labels(1), etc.

I hope this piece of code, it's going to help more programmers in the future.

Roxieroxine answered 6/3, 2017 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.