winforms icon indicating copy to clipboard operation
winforms copied to clipboard

TabControl.TabPageCollection.RemoveAt(int) removes wrong item!

Open albahari opened this issue 3 years ago • 3 comments

.NET version

All .NET versions to .NET 6

Did it work in .NET Framework?

No

Did it work in any of the earlier releases of .NET Core or .NET 5+?

Same bug in all previous .NET versions & .NET Framework

Issue description

This RemoveAt method in System.Windows.Forms.TabControl.TabPageCollection has a horrible bug that can cause it to remove the wrong page from the TabControl.

There's only one line of code in the method, so the bug is obvious:

owner.Controls.RemoveAt(index);

You can see that it's applying the index to the Controls collection rather than the TabPages collection. These two collections are not the same; they can sometimes get out of sync when the SelectedTab changes. You used to be able to work around the bug by setting the AllowUpdateChildControlIndexForTabControls application switch to false, but this switch is no longer supported.

You can fix the bug by changing that line of code to:

owner.Controls.Remove(this[index]);

Steps to reproduce

I don't think this is needed - the bug is obvious from looking at the one line of code.

albahari avatar Sep 24 '22 06:09 albahari

Thank you for raising this. Would you like to send a PR with a fix?

RussKie avatar Sep 24 '22 12:09 RussKie

@albahari are you looking to submit a patch to fix this?

elachlan avatar Oct 05 '22 22:10 elachlan

Here's the PR: https://github.com/dotnet/winforms/pull/7911

albahari avatar Oct 10 '22 04:10 albahari