Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/core/compatibility/6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ If you're migrating an app to .NET 6, the breaking changes listed here might aff
| - | - |
| [Selected TableLayoutSettings properties throw InvalidEnumArgumentException](windows-forms/6.0/tablelayoutsettings-apis-throw-invalidenumargumentexception.md) | Preview 1 |
| [DataGridView-related APIs now throw InvalidOperationException](windows-forms/6.0/null-owner-causes-invalidoperationexception.md) | Preview 4 |
| [ListViewGroupCollection methods throw new InvalidOperationException](windows-forms/6.0/listview-invalidoperationexception.md) | RC 2 |
| [NotifyIcon.Text maximum text length increased](windows-forms/6.0/notifyicon-text-max-text-length-increased.md) | Preview 1 |
| [Some APIs throw ArgumentNullException](windows-forms/6.0/apis-throw-argumentnullexception.md) | Preview 1-4 |
| [TreeNodeCollection.Item throws exception if node is assigned elsewhere](windows-forms/6.0/treenodecollection-item-throws-argumentexception.md) | Preview 1 |
4 changes: 4 additions & 0 deletions docs/core/compatibility/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ items:
href: windows-forms/6.0/apis-throw-argumentnullexception.md
- name: DataGridView APIs throw InvalidOperationException
href: windows-forms/6.0/null-owner-causes-invalidoperationexception.md
- name: ListViewGroupCollection methods throw new InvalidOperationException
href: windows-forms/6.0/listview-invalidoperationexception.md
- name: NotifyIcon.Text maximum text length increased
href: windows-forms/6.0/notifyicon-text-max-text-length-increased.md
- name: TableLayoutSettings properties throw InvalidEnumArgumentException
Expand Down Expand Up @@ -843,6 +845,8 @@ items:
href: windows-forms/6.0/apis-throw-argumentnullexception.md
- name: DataGridView APIs throw InvalidOperationException
href: windows-forms/6.0/null-owner-causes-invalidoperationexception.md
- name: ListViewGroupCollection methods throw new InvalidOperationException
href: windows-forms/6.0/listview-invalidoperationexception.md
- name: NotifyIcon.Text maximum text length increased
href: windows-forms/6.0/notifyicon-text-max-text-length-increased.md
- name: TableLayoutSettings properties throw InvalidEnumArgumentException
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: "Breaking change: ListViewGroupCollection methods throw new InvalidOperationException"
description: Learn about the breaking change in .NET 6 where some ListViewGroupCollection methods throw a new InvalidOperationException if the ListView is in virtual mode.
ms.date: 09/23/2021
---
# ListViewGroupCollection methods throw new InvalidOperationException

Previously, an <xref:System.InvalidOperationException> was thrown if <xref:System.Windows.Forms.ListViewGroupCollection> methods were invoked on a <xref:System.Windows.Forms.ListViewItem.ListView> in virtual mode *and* the <xref:System.Windows.Forms.Control.Handle> had already been created. Starting in .NET 6, these <xref:System.Windows.Forms.ListViewGroupCollection> methods now only check if the <xref:System.Windows.Forms.ListViewItem.ListView> is in virtual mode. If it is, they throw an <xref:System.InvalidOperationException> with a more descriptive message.

## Previous behavior

Consider the following code that adds a <xref:System.Windows.Forms.ListViewGroup> to a <xref:System.Windows.Forms.ListViewItem.ListView>:

```csharp
ListViewGroup group1 = new ListViewGroup
{
Header = "CollapsibleGroup1",
CollapsedState = ListViewGroupCollapsedState.Expanded
};

listView.Groups.Add(group1);
```

This code produced an <xref:System.InvalidOperationException> with the following message:

**When the ListView is in virtual mode, you cannot enumerate through the ListView items collection using an enumerator or call GetEnumerator. Use the ListView items indexer instead and access an item by index value.**

## New behavior

The same code from the [Previous behavior](#previous-behavior) section produces an <xref:System.InvalidOperationException> with the following message:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous/new behavior sections don't show anything about the handle being created or not. Is there a way to show that in the code?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SergeySmirnov-Akvelon to confirm on that.

Copy link

@SergeySmirnov-Akvelon SergeySmirnov-Akvelon Sep 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially, the issue was reproduced only when the Handle was created. The presence of the Handle caused an additional thread of code to be called, which tried to access the Items and failed because access to Items in virtual mode was denied. The user did not see any information about the Handle, only an exception with access to Items.

Now, since the virtual mode is always checked, we also display an error only about the virtual mode, without any data about the Handle.


**You cannot add groups to the ListView groups collection when the ListView is in virtual mode.**

## Reason for change

The new <xref:System.InvalidOperationException> message is more understandable. In addition, it closes a workaround where the developer could add a <xref:System.Windows.Forms.ListViewGroup> to the <xref:System.Windows.Forms.ListViewItem.ListView> before the <xref:System.Windows.Forms.Control.Handle> was created.

## Version introduced

.NET 6.0 RC 2

## Recommended action

- Review and, if necessary, update your code so that it doesn't add a <xref:System.Windows.Forms.ListViewGroup> to a <xref:System.Windows.Forms.ListViewItem.ListView> in virtual mode.
- If your code handles <xref:System.InvalidOperationException> exceptions, you may need to update the message to reflect that the <xref:System.Windows.Forms.ListViewItem.ListView> is in virtual mode.

## Affected APIs

- <xref:System.Windows.Forms.ListViewGroupCollection.Add%2A?displayProperty=fullName>
- <xref:System.Windows.Forms.ListViewGroupCollection.AddRange%2A?displayProperty=fullName>
- <xref:System.Windows.Forms.ListViewGroupCollection.Insert(System.Int32,System.Windows.Forms.ListViewGroup)?displayProperty=fullName>