Control Arrays in Design Mode
See original GitHub issueIs 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:
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.
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:
- A
List
can be used to emulate the large collection of controls. typeof(Control)
helps us define the type of the control to add to the list.- 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:
Running:
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:
- Created 3 years ago
- Reactions:6
- Comments:9 (5 by maintainers)
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).
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.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.