dotnet / winforms

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

Wrong ListViewItemStates value in OnDrawSubItem method from ListView Control. #6975

Open ghost opened 2 years ago

ghost commented 2 years ago

.NET version

6.0, 4.8

Did it work in .NET Framework?

No

Issue description

In the ListView control, set OwnerDraw = true, then you can overload the OnDrawSubItem function. https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listview.ondrawsubitem

When you select an Item, then click on an empty/blank space in the ListView. This operation will result in no items being selected (ListView.SelectedItems.Count==0). But the wrong value may appear in the OnDrawSubItem method (DrawListViewSubItemEventArgs e).

e.ItemState still equals ListViewItemStates.Selected | ListViewItemStates.Focused

Test Code: .net 6.0 / Visual Studio 2022

namespace ListViewItemStatesTest
{
    internal static class Program
    {
        [STAThread]
        static void Main()
        {
            ApplicationConfiguration.Initialize();
            Application.Run(CreateForm());
        }

        private class NewListView : ListView
        {
            private static readonly SolidBrush Brush = new SolidBrush(Color.Black);

            protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e)
            {
                if (e.ItemState == (ListViewItemStates.Selected | ListViewItemStates.Focused))
                {
                    e.SubItem.BackColor = Color.Orange;
                }
                else
                {
                    e.SubItem.BackColor = Color.LightBlue;
                }

                e.DrawDefault = true;
                base.OnDrawSubItem(e);
            }
        }

        private static Form CreateForm()
        {
            var frm = new Form()
            {
                Width = 800,
                Height = 600,

                StartPosition = FormStartPosition.CenterScreen
            };

            var control = new NewListView()
            {
                Dock = DockStyle.Fill,
                View = View.Details,

                OwnerDraw = true,
                GridLines = true,
                FullRowSelect = true,
            };

            control.Columns.Add("Column 1", 100);
            control.Columns.Add("Column 2", 600);

            var item1 = control.Items.Add("1");
            item1.SubItems.Add("Sub Item 1");

            var item2 = control.Items.Add("2");
            item2.SubItems.Add("Sub Item 2");

            frm.Controls.Add(control);

            var button1= new Button() { Text = "Button 1", Height = 50, Dock = DockStyle.Top };
            frm.Controls.Add(button1);

            return frm;
        }
    }
}

Steps to reproduce

normal control behavior:

  1. Run the above code.
  2. Click ListView's item 1. The item's background turns blue.
  3. Click Button 1, The item's background turns gray.

Incorrect control behavior:

  1. Restart program (you need to restart the program, because the focus of the control may have changed).
  2. Click ListView's item 1. The item's background turns blue.
  3. Click on other blank area (white color) of ListView (don't click on Item area or column area, but blank area). The item's background turns orange.

Because the value of the parameter of DrawListViewSubItemEventArgs e (e.ItemState) is wrong, the item background behaves as Orange.

If you can't reproduce the process, or need a video, please let me know.

dreddy-work commented 2 years ago

Thank you @roland5572 for reporting this. Given this has been the case from Framework, we are unlikely to get it investigated on priority basis. If you like to investigate and propose a solution, we will be happy to help get it reviewed/fixed.

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!

RutulPatel8 commented 1 month ago

Let me start working on these issues. I will come up with a solution shortly.