【问题标题】:Vertical text alignment for listview header text列表视图标题文本的垂直文本对齐
【发布时间】:2018-10-10 19:09:37
【问题描述】:

如何设置listview对象标题列的垂直对齐方式以适应单元格边框内的文本(不能自动设置标题表单的高度)

如下图所示,部分文字未显示;

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

将“strformat”添加到drawcolumnheader 可用于水平对齐,但不适用于垂直

 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

【问题讨论】:

  • 我认为您可以使用 Graphics.MeasureString 来计算加法时的正确宽度?
  • @mino 它不是关于宽度,问题是文本不适合行的高度
  • 您的偏好设置为: - 调整字体大小 - PInvoking 以修改标题高度 - 使用 DataGridView。
  • 使用“strFormat.LineAlignment = StringAlignment.Center”或“strFormat.Alignment = StringAlignment.Center”添加drawcoloumn标头事件效果不佳,是否有类似的想法适用于水平对齐?跨度>
  • StringFormat 正在按预期工作。您的 问题 是没有足够的垂直空间来包含您的字符串。我在之前的评论中列出了 3 种可能的解决方案。

标签: vb.net listview word-wrap


【解决方案1】:

正如@Jimi 所推荐的,处理标题文本对齐的最佳方法是使用 Datagridview 而不是 Listview。 Datagridview 有很多功能,包括标题文本、行和列调整。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-13
    • 2017-08-15
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多