dotnet / dotnet/winforms

Wrong ListViewItemStates value in OnDrawSubItem method from ListView Control.

Open
#6,975 3 comments 0 reactions 0 assignees View on GitHub
area-controls-ListView help wanted
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
1d 13m
Merged PRs (30d)
85

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.