3

I have a list view and there are multiple columns with long text values, like a column with destination file path it has a value like c:\users\kavya\new\coding\img1000.jpg something very big.

I want to adjust the text according to size of the column when the users uses the scroll bar: with width something very big all the data c:\users\kavya\new\coding\img1000.jpg should be visible and when he scrolls the column header to very small only the c:\img1000.jpg has to be viewed but the memory should have the entire path actualy we see something like c:\users\kavya…...

How can I do this?

j0k
  • 22,600
  • 28
  • 79
  • 90
  • I think this word is called `ellipsis`. this can be avoid by disabling ellipsis in properties. i am not sure. – Mr_Green Oct 15 '12 at 07:20
  • is there a way i can only view the drive and file name if the column width is decreased so that the actual filename is visible but the memory should have entire path.... – user1746284 Oct 15 '12 at 07:24

1 Answers1

1

By doing Windows API call PathCompactPathEx,

[DllImport("shlwapi.dll", CharSet = CharSet.Auto)]
 static extern bool PathCompactPathEx([Out] StringBuilder pszOut, string szPath, int cchMax, int dwFlags);

static string PathShortener(string path, int length)
 {
     StringBuilder sb = new StringBuilder();
     PathCompactPathEx(sb, path, length, 0);
     return sb.ToString();
 }

OR You can try like this :

string PathShortener(string path)
 {
     const string pattern = @"^(\w+:|\\)(\\[^\\]+\\[^\\]+\\).*(\\[^\\]+\\[^\\]+)$";
     const string replacement = "$1$2...$3";
     if (Regex.IsMatch(path, pattern))
     {
         return Regex.Replace(path, pattern, replacement);
     }
     else
     {
         return path;
     }          
 } 

OR You can use like below :

string ellipsisedPath = OriginalPath + '\0';

visit: Add Ellipsis to a Path in a WinForms Program without Win32 API call (revisited)

Community
  • 1
  • 1
  • sir you have given how to get the required string from entire path but i want how to manage the column width based on the text visible...... – user1746284 Oct 16 '12 at 06:05
  • based on the column header scrolled the text visible changes i want the total text visible at a particular column width and when it is small only c:/filename visible . how to manage column width and text visible in list view – user1746284 Oct 16 '12 at 06:07