Prev: Asynchronous uploading problem with FtpWebRequest
Next: Lauch .RDP from a C# App while keeping taskbar hidden
From: Adhal on 15 May 2010 14:12 Hello, I have a listview set to "Details" view. The listview is empty and I set the column[0].width to -2. For some unknown reason the width is set to beyond the listview size, and the horizontal bar appears. In another form, the listview works fine and behaves perfectly when I autosize. I couldn't find much info on how to fix it just that it is a known bug according to one forum posting. Maybe my WWW search capabilities suck. Is there a simple way around this. Is Microsoft going to fix this bug? Appreciate any advice on how to fix this. Thank you :)
From: Peter Duniho on 16 May 2010 20:25 Adhal wrote: > Hello, > I have a listview set to "Details" view. The listview is empty and I > set the column[0].width to -2. For some unknown reason the width is set > to beyond the listview size, and the horizontal bar appears. In another > form, the listview works fine and behaves perfectly when I autosize. Setting the ColumnHeader's Width property will cause the column to be automatically sized to fit the width of the column heading. One reason that you would see different behavior on one computer as compared to another is when the two computers are rendering the column header differently. For example, they aren't the same operating system version, or they are using a different font, or the font scaling has been set differently, etc. Whether that's the issue you're seeing is impossible to say, especially without an actual concise-but-complete code example, never mind two specific computers that are known to produce different results. But hopefully that provides you with a way to understand the behavior better. > I > couldn't find much info on how to fix it just that it is a known bug > according to one forum posting. Maybe my WWW search capabilities suck. > > Is there a simple way around this. Is Microsoft going to fix this bug? I suspect that it's not a bug. But there's no way for me to know for sure given the information provided. Pete
From: Adhal on 17 May 2010 15:22 Thanks Pete, It's on one computer within the same IDE. First thing I tried was on a different PC see if it was my NET install. Error was still there. I test it on a clean VirtualBox Win-XP image. Here's the code: >> listViewFiles.Columns[0].Width = -2; It's only got one column header. In one of the list, I first populate the list with filenames from the system then call that. There are several listviews with only two producing this error. One is on blank form with just it. The other is part of a directory like display within splitters. Setting the last column width to -2 should always autosize to either max size or maximum text (label) length. Whichever is greater. I don't see how one can type it wrong or differently. Couldn't figure out the reason but I am not the only one who is having this issue. I have found others complaining about the same issue while googling. One guy said it was a known bug (I am unable to find the link again, sorry). Anyhow, I went back to what I use to do in VB6. :) SendMessage and LVSCW_AUTOSIZE_USEHEADER. Works fine now. Solution was staring my in the face and I couldn't see it. I just replaced one line. So instead of calling: ---------------- listViewFiles.Columns[0].Width = -2; ---------------- I call ResizeColumn (implementation below). ---------------- [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); const int LVM_SETCOLUMNWIDTH = 0x101E; const int LVSCW_AUTOSIZE_USEHEADER = -2; public static void ResizeColumn(IntPtr hWnd, int columnIndex) { SendMessage(hWnd, LVM_SETCOLUMNWIDTH, columnIndex, LVSCW_AUTOSIZE_USEHEADER); } ---------------- Painfully sweet. :)
From: Adhal on 18 May 2010 18:04
Spoke to soon. The error is still there it just seems not to occur as often or perhaps that might be in my imagination only. I'll auto-size it myself once I figure out how to do it. :) |