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.

[API Proposal]: Allow ControlCollection.AddRange to use params keyword.

See original GitHub issue

Background and motivation

Currently Control.ControlCollection.AddRange method is accepting an array of Control, this means each time that we want to add multiple controls at once, we have to make a new array. By using params keyword using this method will become easier.

API Proposal

  • Add params keyword to all collection AddRange methods that don’t already have it.
namespace System.Windows.Forms;

public class Control
{
    public class ControlCollection
    {
        public virtual void AddRange(params Control[] controls);
    }
}

Likewise we want to update our other collections that have an AddRange:

// Note that parameter names are left as they currently are.

System.ComponentModel.Design.DesignerActionListCollection.AddRange(params DesignerActionList[] value)
System.Windows.Forms.AutoCompleteStringCollection.AddRange(params string[] value)
System.Windows.Forms.ComboBox.ObjectCollection.AddRange(params object[] items)
System.Windows.Forms.Design.Behavior.BehaviorServiceAdornerCollection.AddRange(params Adorner[] value)
System.Windows.Forms.Design.Behavior.GlyphCollection.AddRange(params Glyph[] value)
System.Windows.Forms.ImageList.ImageCollection.AddRange(params Image[] images)
System.Windows.Forms.ListBox.IntegerCollection.AddRange(params int[] items)
System.Windows.Forms.ListBox.ObjectCollection.AddRange(params object[] items)
System.Windows.Forms.ListView.ListViewItemCollection.AddRange(params ListViewItem[] items)
System.Windows.Forms.ListView.ColumnHeaderCollection.AddRange(params ColumnHeader[] values)
System.Windows.Forms.ListViewGroupCollection.AddRange(params ListViewGroup[] groups)
System.Windows.Forms.ListViewItem.ListViewSubItemCollection.AddRange(params ListViewSubItem[] items)
System.Windows.Forms.ListViewItem.ListViewSubItemCollection.AddRange(params string[] items)
System.Windows.Forms.TabControl.TabPageCollection.AddRange(params TabPage[] pages)
System.Windows.Forms.ToolStripItemCollection.AddRange(params ToolStripItem[] toolStripItems)
System.Windows.Forms.ToolStripPanel.ToolStripPanelRowCollection.AddRange(params ToolStripPanelRow[] value)
System.Windows.Forms.TreeNodeCollection.AddRange(params TreeNode[] nodes)

API Usage

this.Controls.AddRange(this.BtnControl1, this.BtnControl2);

Risks

No known risks.

Will this feature affect UI controls?

  • No, VS Designer doesn’t need to support this feature.
  • The impact will be easier use of this method by users.
  • No, this feature doesn’t need to be localized.

Issue Analytics

  • State:closed
  • Created 6 months ago
  • Reactions:3
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
ALiwotocommented, Apr 12, 2023

@elachlan

did you want to do the PR to implement it?

Yes.

@JeremyKuhne can I commit the changes to my current PR? Or I have to revert the last commit from it first?

1reaction
terrajobstcommented, Apr 11, 2023

Video

  • Looks good as proposed
namespace System.Windows.Forms;

public partial class Control
{
    public partial class ControlCollection
    {
        // Existing signature:
        // public virtual void AddRange(Control[] controls);
        public virtual void AddRange(params Control[] controls);
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Refactor `ControlCollection.AddRange` to use params ...
Proposed changes Add params keyword to arguments of ControlCollection.AddRange method. Regression? No Risk None Microsoft Reviewers: Open in CodeFlow.
Read more >
[API Proposal]: Allow ControlCollection.AddRange to use ...
Windows Forms is a .NET UI framework for building Windows desktop applications. - [API Proposal]: Allow ControlCollection.AddRange to use params keyword.
Read more >
Form.ControlCollection.Add(Control) Method
You can use this method to add controls to the form. If you want to add a group of already created controls to...
Read more >
params keyword for parameter arrays - C# reference
In this article ... By using the params keyword, you can specify a method parameter that takes a variable number of arguments. The...
Read more >
C# 10 in a Nutshell
C# also allows parts of your code to be dynamically typed via the dynamic keyword. However, C# remains a predominantly.
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