I just came up with a hack for .NET ListView to react to users doubleclicking the header separator to auto resize the column to fit column contents. The reason why I need this is that I'm creating a custom control, that’s a mix of ListView and TreeView and in the first column, I'm drawing the plus sign, as there’s in every TreeView
.
However, when user double clicks the header separator of the first column and column is auto resized, that addition of mine is not taken into account. Column is resized to the text width only, leaving some of the text out, as seen in the image below:
So the only way to fix this is to subscribe to the correct event and add to the column width there. Great, isn’t it?. The bad news is that there is no such event in .NET… :( But I had to get around it and I didn’t want to use P/Invoke and call unmanaged code. This is what I came up with:
private int widthChangingCount = 0;
private bool manuallySettingWidth = false;
protected override void OnColumnWidthChanging(ColumnWidthChangingEventArgs e)
{
if (manuallySettingWidth == false && e.ColumnIndex == 0)
widthChangingCount++;
base.OnColumnWidthChanging(e);
}
protected override void OnColumnWidthChanged(ColumnWidthChangedEventArgs e)
{
if (manuallySettingWidth == false && e.ColumnIndex == 0 &&
widthChangingCount == 1)
{
manuallySettingWidth = true;
Columns[e.ColumnIndex].Width += 40;
manuallySettingWidth = false;
}
widthChangingCount = 0;
base.OnColumnWidthChanged(e);
}
So, in the class that derives from ListView
, override these two methods. OnColumnWidthChanging
is called when user clicks on the separator and drags it to resize column. It gets called a few times before the mouse button is released.
The trick with double clicking the header is: OnColumnWidthChanging
gets called only once. I used this as the means to identify resizing in this way. As far as I tested, I couldn't manually resize the header so quick that OnColumnWidthChanging
would get called only once. So in OnColumnWidthChanged
method, I add the width of my image to the column width. Be careful however, that changing column size from code also calls OnColumnWidthChanging
once, that’s why I added the manuallySettingWidth
boolean to help here.
Quite ugly, but works so far. :)
CodeProject