Answered by:
Listview content/column width issue

Question
-
Hi
I have a listview control that I have set to populate by with all documents in a specified directory, all the files load and it all works really well. The problem is some file names have caused the columns to become pushed out of alignment.
I've tried several ideas i.e. setting the column to the size of a specified header and the ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize) and also column content option.
I'm thinking the best way to approach it is by specifying the column width (meaning some of the file name would only be visable once selected or shortening how much of the file name is displayed initially. I will show the code snippet below that deals with populating the control, if anyone has any thoughts on how I can solve this issue of point me towards any further reading I would be greatful.
Private Sub Select_folder(sender As Object, e As EventArgs) Handles Button2.Click
Dim dir As New System.IO.DirectoryInfo("C:\Test_Dir")
Dim item As ListViewItem
ListView1.BeginUpdate()
Dim file As System.IO.FileInfo
For Each file In dir.GetFiles("*", SearchOption.AllDirectories)
' Set a default icon for the file.
Dim iconForFile As Icon = SystemIcons.WinLogo
item = New ListViewItem(file.Name, 1)
' Check to see if the image collection contains an image
' for this extension, using the extension as a key.
If Not (ImageList1.Images.ContainsKey(file.Extension)) Then
' If not, add the image to the image list.
iconForFile = System.Drawing.Icon.ExtractAssociatedIcon(file.FullName)
ImageList1.Images.Add(file.Extension, iconForFile)
End If
item.ImageKey = file.Extension
ListView1.Items.Add(item)
Next file
ListView1.EndUpdate()
End SubWednesday, June 29, 2016 1:46 PM
Answers
-
Moonlight I appreciate the replay, I can see what this code is trying to do; run through however many columns there are adjusting each to the number set (-2 or -1) but I'm getting the following error.
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll
Additional information: InvalidArgument=Value of '0' is not valid for 'index'.
Hi
You need to adjust the code that Moonlight posted as the Columns are 0 to Count -1
For i = 0 To listview1.Columns.Count - 1 listview1.Columns(i).Width = -2 'Content adaptive -2, header adaptive -1 Next I
BTW: if you post code, please use the code block tool in the toolbar - it makes it *much* easier to view.
Regards Les, Livingston, Scotland
- Edited by leshay Thursday, June 30, 2016 8:45 PM
- Proposed as answer by Moonlight ShengMicrosoft contingent staff Friday, July 1, 2016 12:42 AM
- Marked as answer by Moonlight ShengMicrosoft contingent staff Monday, July 4, 2016 2:34 AM
Thursday, June 30, 2016 8:43 PM -
Hi
I tried with your code plus the additions I have commented and everything 'fits' in the ListView.
' changed path for my testing ************** Dim dir As New System.IO.DirectoryInfo("C:\Users\Les\Documents\Testing") Dim item As ListViewItem '******************************** ' my test additions ' all other Properties default With ListView1 .Font = New Font("Arial", 16) .View = View.SmallIcon .SmallImageList = ImageList1 End With ListView1.BeginUpdate() Dim file As System.IO.FileInfo For Each file In dir.GetFiles("*", SearchOption.AllDirectories) ' Set a default icon for the file. Dim iconForFile As Icon = SystemIcons.WinLogo '******************************** ' tested using FullName to verify column width item = New ListViewItem(file.FullName, 1) ' Check to see if the image collection contains an image ' for this extension, using the extension as a key. If Not (ImageList1.Images.ContainsKey(file.Extension)) Then ' If not, add the image to the image list. iconForFile = System.Drawing.Icon.ExtractAssociatedIcon(file.FullName) ImageList1.Images.Add(file.Extension, iconForFile) End If item.ImageKey = file.Extension ListView1.Items.Add(item) Next file ListView1.EndUpdate()
Regards Les, Livingston, Scotland
- Marked as answer by Arcdreamer Monday, July 4, 2016 11:21 PM
Monday, July 4, 2016 10:35 PM
All replies
-
Hi Arcdreamer,
You could refer to below code:
For i = 0 To listview1.Columns.Count listview1.Columns(i).Width = -2 'Content adaptive -2, header adaptive -1 Next I
Regards,
Moonlight
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.Thursday, June 30, 2016 6:23 AM -
Moonlight I appreciate the replay, I can see what this code is trying to do; run through however many columns there are adjusting each to the number set (-2 or -1) but I'm getting the following error.
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll
Additional information: InvalidArgument=Value of '0' is not valid for 'index'.- Proposed as answer by Moonlight ShengMicrosoft contingent staff Friday, July 1, 2016 12:42 AM
- Unproposed as answer by Moonlight ShengMicrosoft contingent staff Friday, July 1, 2016 12:42 AM
Thursday, June 30, 2016 8:11 PM -
Moonlight I appreciate the replay, I can see what this code is trying to do; run through however many columns there are adjusting each to the number set (-2 or -1) but I'm getting the following error.
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll
Additional information: InvalidArgument=Value of '0' is not valid for 'index'.
Hi
You need to adjust the code that Moonlight posted as the Columns are 0 to Count -1
For i = 0 To listview1.Columns.Count - 1 listview1.Columns(i).Width = -2 'Content adaptive -2, header adaptive -1 Next I
BTW: if you post code, please use the code block tool in the toolbar - it makes it *much* easier to view.
Regards Les, Livingston, Scotland
- Edited by leshay Thursday, June 30, 2016 8:45 PM
- Proposed as answer by Moonlight ShengMicrosoft contingent staff Friday, July 1, 2016 12:42 AM
- Marked as answer by Moonlight ShengMicrosoft contingent staff Monday, July 4, 2016 2:34 AM
Thursday, June 30, 2016 8:43 PM -
Apologies for the forum faux par Les I see the Code block option. Thanks for the tip.Thursday, June 30, 2016 9:04 PM
-
Les sorry for the late response I've been away from the laptop over the weekend. Moonlights answer with your edit rid me of my error message but put me in the same position of having the longer filenames push my columns out of alignment. If I could get my account verified I would post a screenshot of the result.Monday, July 4, 2016 8:45 PM
-
Hi
What are the Properties of the ListView1 that you have altered from the defaults?
Regards Les, Livingston, Scotland
- Edited by leshay Monday, July 4, 2016 10:15 PM
Monday, July 4, 2016 10:15 PM -
Hi
I tried with your code plus the additions I have commented and everything 'fits' in the ListView.
' changed path for my testing ************** Dim dir As New System.IO.DirectoryInfo("C:\Users\Les\Documents\Testing") Dim item As ListViewItem '******************************** ' my test additions ' all other Properties default With ListView1 .Font = New Font("Arial", 16) .View = View.SmallIcon .SmallImageList = ImageList1 End With ListView1.BeginUpdate() Dim file As System.IO.FileInfo For Each file In dir.GetFiles("*", SearchOption.AllDirectories) ' Set a default icon for the file. Dim iconForFile As Icon = SystemIcons.WinLogo '******************************** ' tested using FullName to verify column width item = New ListViewItem(file.FullName, 1) ' Check to see if the image collection contains an image ' for this extension, using the extension as a key. If Not (ImageList1.Images.ContainsKey(file.Extension)) Then ' If not, add the image to the image list. iconForFile = System.Drawing.Icon.ExtractAssociatedIcon(file.FullName) ImageList1.Images.Add(file.Extension, iconForFile) End If item.ImageKey = file.Extension ListView1.Items.Add(item) Next file ListView1.EndUpdate()
Regards Les, Livingston, Scotland
- Marked as answer by Arcdreamer Monday, July 4, 2016 11:21 PM
Monday, July 4, 2016 10:35 PM -
I hadn't changed any of the default settings Les, I'll try your snippet. Thanks for your adviceMonday, July 4, 2016 10:37 PM
-
That snippet is the best revision thus far Les, it now gives the files with much longer names their own space entirely so as not to push others along. I think what could make the whole process simpler is applying a character limit on the filename display?
Monday, July 4, 2016 10:45 PM -
As crazy as this sounds Les I just took the whole thing out I haven't yet included either Moonlight of your snippets and it is working I created a fresh listview, so I think you may have been onto something in the fact I may have altered something in the defaults without knowingMonday, July 4, 2016 11:21 PM