I have my own ListView class, inheriting from Forms.ListView. When double clicking on the column divider, the columns resize to show the column items in full size.

However, for me this doesnt work properly - some words are partially hidden because I am using large font size to draw items.

Now, when the column is double clicked, I want to calculate the column size by measuering the size of largest string in the column. Any one know how to find the largest string in the listview column without iterating each item in the listview?

Recommended Answers

All 9 Replies

I don't think it's really possible, unless you compare the length of each item as it's added, perhaps recording the index of the longer one. You could also do it so that the iteration is kind of hidden. Create your own sorter and assign it to the listviewitemsorter. Something like:

        private static int NewListviewItemSorter(ListViewItem A, ListViewItem B)
        {
            if (A.Text.Length < B.Text.Length) return -1;
            if (A.Text.Length > B.Text.Length) return 1;
            else return 0;
        }

When the ListVIew column width=-1, the column will be resized to fit its column contents. How does the length or size is calculated by the listview? Is there any windows message to get thelongest content in the listview? Any idea...?

It probably compares the length of the subitem, being added, to the current width and adjusts if needed. You could probably do the same by extending the add and addrange methods.

If you're inheriting the listview class you can use the autosize feature. In the double-click event change the column width to -1 and let it resize it should account for font size.

Thank you tinstaafl for the replay.

In my ownerdrawn listview, I had overrided the OnDrawSubItem() method to draw the text. The text is drawn as below.

e.SubItem.Font=customFont
stringFormat.Trimming=StringTrimming.EllipsCharacter
stringFormat.FormatFlags=StringFormatFlags.NoWrap
e.Graphics.DrawString(string,customFont,brush,e.Bounds,stringFormat)

In the double-click event, column width is setted to -1. When the column divider is double clicked, the column is resized according to the size of customFont. But still the text is not displayed completely. Is there any problem with the StringFormat?

When I change the draw string method as below
e.Graphics.DrawString(string,customFont,brush,e.Bounds.X,e.Bounds.Y)
and double-clicks the column divider, the column will be resized to show the text completely. Here I cann't set the string format to display the ellipses. So I cannot use this method.

(1) Any Idea? What should I do to display the whole text completely? Does I am missing any thing in the DrawString() ?

(2)In ListBox we have "LB_ADDSTRING" message which is posted when any item is added to the ListBox. In the ListView is there any equalent messages which is raised when any items are added, deleted or updated?

Perhaps something like this will work: stringFormat.FormatFlags=(StringFormatFlags.NoWrap & StringFormatFlags.NoClip).

stringFormat.FormatFlags=(StringFormatFlags.NoWrap & StringFormatFlags.NoClip).

I tried this. But no use.

one other thought are you sure the string rectangle is the right size? other than that here's the article on the different flags your can use maybe one or more of them will work.

The column resize issue is solved by manually calculating the columnwidth(iterating through each column content to find the largest one) in the columnDivider double click event.

You could get the HitInfo for the DGV and then use that to set the AutoSizeMode property of the DGV's column like this:

private DataGridView.HitTestInfo hitInfo;

private Void MyDataGridView_ColumnHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
    //// Gets the details of the click that occurred on the DGV \\\\
    hitInfo this.MyDataGridView.HitTest(e.X, e.Y);

    if (this.hitInfo.Type == DataGridViewHitTestType.ColumnHeader)
    {
        //// Sets the AutoSizeMode property of the column clicked \\\\
        this.MyDataGridView.Columns[hitInfo.ColumnIndex].AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;            
    }
}

It may be more managable to do it this way and DataGridView.HitTestInfo is extremely useful when working with DGVs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.