65.9K
CodeProject is changing. Read more.
Home

Auto-resize list view using a shortcut trick

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.40/5 (19 votes)

Aug 5, 2003

1 min read

viewsIcon

98411

downloadIcon

348

This is a little trick to auto-resize the columns width of a listview with only one line of code


Introduction

Windows has many shortcuts, but there is one very interesting to auto-resize the columns width of any grid, CTRL and "+" of the numeric pad. To try this feature try to set focus to the list view of Explorer (for example by selecting a file) and then press the CTRL button and the "+" button of the numeric pad, you'see columns moving untill them are perfectly sized so there's no text hided by their little width. So I tought, can I use this feature into my programs ? And this is what I done.

Using the code

Now, to send keystrokes to the active application you have to use the SendKeys.Send function. The code of the CTRL character is "^", while the plus sign is equal to {+}.

SendKeys.Send ("^{+}");

Demo project

The demo project is a simple program to search any file trought the directory tree and I used these lines of code to resize the column of the listview:
if (chkResizeGrid.Checked)
{
    lstViewFile.Focus();
    SendKeys.Send ("^{+}");
}

I have to set the focus to the grid so I'm sure that the next instruction, the Send function, will be received by the listview. Sorry, but in the demo project all the text and comments are in italian.

Conclusion

This is a little trick by Francesco Natali.