Click here to Skip to main content
15,886,603 members
Articles / Programming Languages / C#
Article

Auto-resize list view using a shortcut trick

Rate me:
Please Sign up or sign in to vote.
3.40/5 (19 votes)
11 Sep 20031 min read 97.7K   348   30   11
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 {+}.

C#
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:
C#
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionResize the column width with Javascript and Norefresh,What can i do? Pin
bluelins11-Jan-07 15:52
bluelins11-Jan-07 15:52 
GeneralCool thing Pin
Gevik Babakhani9-Oct-03 12:09
Gevik Babakhani9-Oct-03 12:09 
GeneralDemo Programm can't be found Pin
User 5401434-Sep-03 23:59
User 5401434-Sep-03 23:59 
GeneralRe: Demo Programm can't be found Pin
Francesco Natali12-Sep-03 0:41
Francesco Natali12-Sep-03 0:41 
GeneralRe: Demo Programm can't be found Pin
Cool Smith6-Jun-11 21:34
Cool Smith6-Jun-11 21:34 
GeneralWhat's wrong with... Pin
Daniel Turini5-Aug-03 7:26
Daniel Turini5-Aug-03 7:26 
GeneralRe: What's wrong with... Pin
Chris Richner5-Aug-03 16:55
Chris Richner5-Aug-03 16:55 
GeneralRe: What's wrong with... Pin
Nazeeh12-Sep-03 16:46
Nazeeh12-Sep-03 16:46 
GeneralRe: What's wrong with... Pin
Wolfgang Kleinschmit17-Sep-03 1:54
Wolfgang Kleinschmit17-Sep-03 1:54 
GeneralRe: What's wrong with... Pin
Adie_xp10-Nov-04 0:12
Adie_xp10-Nov-04 0:12 
GeneralRe: What's wrong with... Pin
Synetech28-Dec-11 8:16
Synetech28-Dec-11 8:16 
Exactly.

And remember, that list-controls trap any hotkey combination containing Ctrl+Num+. For example, I have Ctrl+Alt+Shift+Num+ set in ATI Tray Tools to reset the screen colors to the default gamma settings, and it is very annoying Mad | :mad: when I have set all of the columns in a program to just the right widths, then I press that hotkey and suddenly all of the columns resize down to the minimums necessary (at that time).

It would be better if people override Ctrl+Num+ in the list-control and set (a different one?) that traps only that hotkey, not every hotkey containing it (and while your at it, also add a control in the header or context-menu or something to resize all columns).
--
Synetech

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.