question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Control Arrays in Design Mode

See original GitHub issue

Is your feature request related to a problem? Please describe.

I’d like to see a similar solution to the one implemented in VB6 for Control Arrays in Design Mode. When can this be useful? When you have complex cases like this one:

Many labels in a window form

I had over 500 pictures that handled an Odontogram. This case was impossible to migrate to .NET because it would have involved a considerable re-engineering process from my side. Also, I believe a similar feature could be brought to MAUI, WPF, and Avalonia. This would increase the flexibility when you have to handle multiple related controls.

Describe the solution you’d like and alternatives you’ve considered

After reading several forums over the years, I was able to write this piece of code that more or less fixed “the problem” of Control Arrays in Design Mode. The example uses a series of Label controls created in Design Mode.

Many labels in a window form

Code examples:

VB.NET:

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

C#:

public class Form1
{

    // To declare the List of controls
    private List<Label> labels = new List<Label>();

    private void Form1_Load(object sender, EventArgs e)
    {
        // To get all controls in the form
        foreach (var control in this.Controls)
        {
            // To search for the specific type that you want to create the array 
            if (control.GetType().Name.Contains("Label"))
                // To add the control to the List
                labels.Add((Label)control);
        }
        // To sort the labels by the ID
        labels = labels.OrderBy(x => x.Name).ToList();
    }
}

I used a List for convenient reasons, but with the pieces of code I shared, anyone can create in design time the controls that they need, and while they keep the “index” as the last character (label1, label2, …, labelN).

Later, they can iterate them with the loop and add them in the blink of an eye. After, they can manipulate them from the object (labels) like before labels(0), labels(1), etc.

There are 3 points to take into consideration:

  1. A List can be used to emulate the large collection of controls.
  2. typeof(Control) helps us define the type of the control to add to the list.
  3. The “index” defined as the last character (label1, label2, …, labelN) is the key. This helps us create a logical order.

Furthermore, here is another example running created design mode using Text Boxes:

Many text boxes in one window

Running:

running example

Will this feature affect UI controls?

Up to a certain point. It might need a new property called Index or something that relates to this feature. I’d say a similar code to the one I shared can be used in the background. Also, another property might be needed to identify if it’s an array.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
RussKiecommented, Feb 15, 2021

Thank you, though I still can’t see a gap in the current Windows Forms API. “Control array” falls into a category of “custom controls” that do not exist in a standard Windows UX. The aim of Windows Forms .NET is to allow developers build apps that look and feel native on Windows. “A feature X existed in language Y” is a weak business proposition, especially when in 20+ years there hasn’t been any significant customer demand for it (if there was 3rd party control vendors would have filled that gap).

  • stackoverflow.com/questions/39541/whats-the-simplest-net-equivalent-of-a-vb6-control-array

I haven’t checked all the links, but IMO this thread has a number of good answers and explanations why VB6 controls array is absent and not needed. There are a number of other ways the problem can be solved in .NET Framework/.NET world including but not limited to the use of user controls, FlowLayoutPanel, and TableLayoutPanel. I don’t think it is totally reasonable to expect to migrate a VB6 codebase into .NET without some kind of expectations of rearchitect/remodel/rewrite.

The designer is obviously oblivious to dynamically added controls at runtime, and can only visualise the DOM described in InitializeComponent method. And this remains the factor with and without control arrays.

1reaction
weltkantecommented, Feb 16, 2021

Its perfectly fine to discuss the WinForms designer for .NET Core here, but I consider WPF/UWP/XAML/etc. discussions a bit too offtopic for the WinForms repo so I won’t go into depth, they each have their own repository after all.

Read more comments on GitHub >

github_iconTop Results From Across the Web

array of control at design time
How to create array of control in design time as in VB6? Is it possible or not? I know code to create that...
Read more >
Simulating Control Arrays in VB
If I wanted a “control array” of four labels, I would do the following: In design mode, place four labels on my form,...
Read more >
Control array in design time | PC Review
Can I create control array (like TextBox array) in design mode. In vb6 I drow some control at the form and then I...
Read more >
How to create Control Arrays in VB .NET
In VB6 there is a feature called Control Arrays, where you name controls the same name and provide them an index value. This...
Read more >
Thread: How Do I Create A Control Array Like I Did In VB6
I want to create a control array but I would like to do so by drawing them in design mode. I know how...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found