-1

I've come through some auto column (ColumnHeader) widths on WinForms ListView control.

Width = -1 sets auto-width based on content width and -2 sets based on header and content. However I cannot find any way to get the auto-computed width which is then set by -1/-2. Width property of ColumnHeader items returns -1.

Is there any way to obtain the real width in pixels which was auto-computed by setting the property to -1/-2?

Thank you!

  • Read the ColumnHeader.Width property right after assigning the -1 or -2 values. It will return the real width. – dr.null 2 days ago
  • 1
    Use the ColumnHeader.AutoResize() method to set the size mode, the meaning is much more clear. The Width property of the ColumnHeader always returns the value set in the native Control (via SendMessage -> LVM_GETCOLUMNWIDTH): it always returns the size in pixels. – Jimi yesterday
  • @dr.null It will not return the real width. Adding 20 to width will result in width 19. See here: i.ibb.co/w0jZwF6/stackoverflow.png – user3625699 yesterday
  • Yes. 20 - 1 = 19. Double check what you are doing and read carefully the second comment. – dr.null yesterday
0

Using ColumnHeader.AutoResizeColumns(ColumnHeaderAutoResizeStyle style) will do.

Then ColumnHeader.Width property returns the actual width.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.