How to show long labels on ListView columns?

A

Arto Viitanen

We have a program that lets the user to compare several files. The files
are shown in ListView, with dataitems on lines and each file on separate
column. Names of the files are used as column headers. Here comes the
problem: since there are several files and dataitems are quite short
(a number or short text), the columns are not very wide. But the file
names are quite long and are of something like:

directory\subdirectory\averyveryverylongname.txt
directory\subdirectory\averyveryverylongothername.txt
directory\subdirectory\averyveryverylongthird.txt

and since the ListView shows the name starting from left, each column
seems to have same the name.

I tried to put the columns right aligned, but still the writing starts
from the left.

I guess the ideal case is to show the names like

dir...longname.txt
dir...thername.txt
dir...hirdname.txt

How can I make that?

If not, how to make tooltips with ListView columns (I found some code
with Google, but it assumed some Windows code I was not familiar with)?
 
C

Claes Bergefall

For tooltips you should be able to use the ListView.ShowItemToolTips
property (combined with the ListViewItem.ToolTipText property)

If you want to shorten the display of your names you should be able to use
PathCompactPath or PathCompactPathEx. These are Win32 functions though. I'm
not sure if there's a .NET equivalent.

/claes
 
A

Arto Viitanen

Claes Bergefall kirjoitti:
For tooltips you should be able to use the ListView.ShowItemToolTips
property (combined with the ListViewItem.ToolTipText property)

ListView column headers do not have ToolTipText property.
 
C

Claes Bergefall

Arto Viitanen said:
Claes Bergefall kirjoitti:

ListView column headers do not have ToolTipText property.

Ahh, right, missed that you talked about the headers. Then you'll have to
roll your own.

/claes
 
A

Arto Viitanen

Claes said:
Ahh, right, missed that you talked about the headers. Then you'll have to
roll your own.

/claes

I tried:

[DllImport("shlwapi.dll", CharSet=CharSet.Auto)]
private static extern bool PathCompactPath(IntPtr hDC, [In, Out]
StringBuilder pszPath, int dx);

....
this.ColumnWidthChanged += new
ColumnWidthChangedEventHandler(mtListView_dstdiff_ColumnWidthChanged);

...

private void mtListView_dstdiff_ColumnWidthChanged(object sender,
ColumnWidthChangedEventArgs e)
{
if (e.ColumnIndex > 1)
using (Graphics graphics = Graphics.FromHwnd(this.Handle))
{
IntPtr hDc = graphics.GetHdc();
ColumnHeader header = this.Columns[e.ColumnIndex];
StringBuilder longName = new StringBuilder((string)header.Tag);
if (PathCompactPath(hDc, longName, header.Width))
{
header.Text = longName.ToString();
}
}
}

(Columns 0 and 1 do not contain the file names and I stored the original
name to column's Tag)

This works when the data is loaded, but when I try to change the column
size, the program gets exception that tells that ColumnInfo cannot be
change, in the line "header.Text = longName.ToString()".

Anyhow, the resulting string is not suitable, since as I told, the
filenames are something like

c:\first\second\third\theverylongfilename.txt

and PathToPathCcompact gives something like

....ird\thev...

whereas I'd like to get

c:\fir...ame.txt

Of cource, I could use the length of the string returned by
PathCompactPath and generate my own string, so could someone tell
how to change the column text dynamically?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top