TreeNodeCollection.Item throws exception if node is assigned elsewhere

TreeNodeCollection.Item[Int32] throws an ArgumentException if the node being assigned is already bound to another TreeView or to this TreeView at a different index.

Change description

In previous .NET versions, you can assign a tree node to a collection even if it's already bound to a TreeView. This can lead to duplicated nodes. Starting in .NET 6, TreeNodeCollection.Item[Int32] throws an ArgumentException if the node being assigned is already bound to another TreeView or to this TreeView at a different index.

Change category

This change affects binary compatibility.

Reason for change

Validating the input parameter is consistent with the behavior of other TreeNodeCollection APIs.

Version introduced

.NET 6

Make sure to unbind a TreeNode before assigning it to the collection.

Affected APIs

System.Windows.Forms.TreeNodeCollection.Item[Int32]