0
votes

How can i set vertical alignment of header column of listview object to fit the text within the cell border ( cannot set the height of the header form automatically)

Some of the text are not shown as seen below ;Listview screenshot

   ListView1.Columns.Add(items.InnerText, 90, HorizontalAlignment.Center)

adding "strformat" to drawcolumnheader is working for horizontal alignment but not for vertical

 Private Sub ListView3_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles ListView3.DrawColumnHeader
    Dim strFormat As New StringFormat()
    If e.Header.TextAlign = HorizontalAlignment.Center Then
        strFormat.LineAlignment = StringAlignment.Center
        strFormat.Alignment = StringAlignment.Center
    ElseIf e.Header.TextAlign = HorizontalAlignment.Right Then
        strFormat.LineAlignment = StringAlignment.Far
    End If

    e.DrawBackground()
    e.Graphics.FillRectangle(Brushes.SteelBlue, e.Bounds)
    Dim headerFont As New Font("Arial", 8, FontStyle.Bold)

    e.Graphics.DrawString(e.Header.Text, headerFont, Brushes.White, e.Bounds, strFormat)

End Sub
1
I think you could use Graphics.MeasureString to calculate correct width at addition? - Panu Oksala
@mino it is not about the width, problem is the text are not fit into the height of the row - Ali
Your preferences goes to: - sizing the Font - PInvoking to modify the Header height - using a DataGridView. - Jimi
adding drawcoloumn header event with "strFormat.LineAlignment = StringAlignment.Center" or "strFormat.Alignment = StringAlignment.Center" is not working as well, any similar idea which is working for horizontal allignment? - Ali
StringFormat is working as expected. Your problem is that there's not enough vertical space to contain your strings. 3 of the possible solutions, I've listed in the previous comment. - Jimi

1 Answers

0
votes

As recommended by @Jimi the best way to handle the header text alignment is using Datagridview instead of Listview. Datagridview has lots of features both with header text, rows and columns adjustment.