dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.42k stars 984 forks source link

TreeView.Sorted Property has Attribute [EditorBrowsable(EditorBrowsableState.Never)] #7027

Closed ghost closed 9 months ago

ghost commented 2 years ago

.NET version

6.0 / 4.8

Did it work in .NET Framework?

No

Issue description

If you have a TreeView control, and you set TreeView.TreeViewNodeSorter = new TreeNodeComparer(). Then .net will set TreeView.Sorted=true.

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.treeview.treeviewnodesorter

But if you set TreeView.TreeViewNodeSorter = null, Then .net will not set TreeView.Sorted=false. This means .net will still sort all nodes.

So if you have set TreeView.TreeViewNodeSorter = new TreeNodeComparer(), you may need to call two steps:

  1. Set TreeView.TreeViewNodeSorter = null
  2. Set TreeView.Sorted=false.

Then you can cancel the automatic sorting function of TreeView.

But TreeView.Sorted Property has Attribute [EditorBrowsable(EditorBrowsableState.Never)], So you may not find this property in a text editor.

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.treeview.sorted

Steps to reproduce

Full Test .net code: .net 6.0 with Visual studio 2022

using System.Collections;

namespace TreeViewNodeSorterTest
{
    internal static class Program
    {
        [STAThread]
        static void Main()
        {
            ApplicationConfiguration.Initialize();
            Test();
        }

        private class TreeNodeComparer : IComparer
        {
            public int Compare(object x, object y)
            {
                throw new NotImplementedException();
            }
        }

        private static void Test()
        {
            TreeView view = new TreeView();

            MessageBox.Show("Sorted: " + view.Sorted);

            view.TreeViewNodeSorter = new TreeNodeComparer();

            MessageBox.Show("Sorted: " + view.Sorted);

            view.TreeViewNodeSorter = null;

            MessageBox.Show("Sorted: " + view.Sorted);
        }
    }
}
dreddy-work commented 2 years ago

@roland5572 Thanks for reporting this!

As this isn't a regression from .NET Framework it is unlikely that the WinForms team will be able to get to the issue. If you or another community member wants to investigate a possible fix we'll be happy to look at any pull requests.

Note that all changes to existing behavior are considered breaking changes and have to be evaluated for potential impact to existing applications. See Behavior breaking change for the guidelines we follow.

ghost commented 2 years ago

This issue is now marked as "up for grabs", and we’re looking for a community volunteer to work on this issue. If we receive no interest in 120 days, we will close the issue. To learn more about how we handle feature requests, please see our documentation.

Happy Coding!

John-Qiao commented 9 months ago

Verified this issue on .NET 9.0.100-preview.2.24101.9 with dlls built from winforms repo of main branch, it was fixed that: view.Sorted is false when set TreeViewNodeSorter to null.

7027-testresult

Eudora-Li01 commented 8 months ago

Verified this issue with .NET 9 Preview 2 test pass build, it was fixed. Test result is same as above.