Skip to content

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

@ghost

Description

.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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions