Text Align column in Listview: first column cannot be centered
Asked Answered
J

3

10

I have a Listview in detail mode with 3 columns. I want to set the text align for the headers to "center". This works for the last two columns but not for the first. If I want to change it to "center" and click on "center", the field keeps being set to "left". Can I change this using the properties or do I need to program this?

Thanks.

Javier answered 28/8, 2012 at 9:0 Comment(0)
E
18

According to the documentation:

Due to a limitation in the underlying control, this property has no effect on the first column in the ListView control, which is always aligned to the left. To work around this limitation in .NET Framework version 2.0, you can handle the ListView.DrawColumnHeader event and paint the column header yourself.

Another alternative workaround is to not use the first column at all and hide it by setting its width to zero.

Exuberance answered 28/8, 2012 at 9:22 Comment(0)
V
5

I have got a simple solution: Add a new (not needed) first column. Change the alignment of the second column (your real first column) to right or center (can now be done in the designer). In Form-Load-Event remove the first (temporary) column. Voila - the textalignent now should be correct.

I have testet this behavior under Windows 7, 8.1 and 10. It should work.

greetings from germany

Versieversification answered 2/12, 2016 at 12:57 Comment(0)
A
1

I realize this is an old post, but I just found another workaround. If you insert whitespaces just before the header text you are inserting or changing in your code (I know it's rough and you need to calculate how many spaces gets your header centered, but it works!) then you can alter the position of the first column's (0) header text as follows:

ListView2.Columns(0).Text = "                              " & ListView1.SelectedItems(0).Text.ToString
Asare answered 10/2, 2023 at 21:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.