WinForms ListView empty rows glitch
I have a glitch in a WinForms C# ListView (with custom modifications to sort and filters on all columns, but it happened also in a standard ListView).
I modify the ListView Items with this (fairly standard) pattern:
BeginUpdate();
// add some items
// remove some other items
Sort();
EndUpdate();
But if I call this code when the ListView is already scrolled, then I get some empty (non selectable) rows before the real items, and 2 scrollabars even if they are not needed.
It looks like a graphic glitch, 开发者_StackOverflowbecause when I scroll the list then the empty items disappear.
Have anyone met this problem before?
Ok, I found the problem. A call to set a column Width = -2 during Resize was messing the owner-draw filters...
This ListView graphic bug. A similar problem can be reproduced, if while changing the ListView size, change the width of its columns. As a solution, the method proposes to change the width in a separate thread.
private void ListView_SizeChanged(object sender, EventArgs e)
{
var widthChangedThread = new Thread(() => SetNewColumnSize()) {IsBackground = true};
widthChangedThread.Start();
}
private void SetNewColumnSize()
{
Invoke(new MethodInvoker(() =>_columnHeader.Width += 10));
}
This control behave in a strange way but setting up the scrollabe property to false in the resize event fixed completly the problem as:
With DirectCast(sender, ListView)
'do not allow scrolling in the resize event
'ortherwise there is a condition where the control
'stop showing the row data
IsScrollEnabled = .Scrollable
.Scrollable = False
ThisColumnHeader = .Columns("colMessage")
If ThisColumnHeader IsNot Nothing Then
.BeginUpdate()
'With .Columns("colMessage")
' .Width = -2
'End With
If .Items.Count > 0 Then
'If MyListDownloadMessage.Count > 0 Then
If ToolStripSerialRxFillDown.Checked Then
.EnsureVisible(.Items.Count - 1)
Else
If .TopItem IsNot Nothing Then
.EnsureVisible(.TopItem.Index)
End If
End If
End If
.EndUpdate()
.Refresh()
End If
.Scrollable = IsScrollEnabled
End With
精彩评论