【问题标题】:vb.net apply color to listview subitemvb.net 将颜色应用于列表视图子项
【发布时间】:2013-11-17 11:38:48
【问题描述】:

我需要更改列表视图中某一列的字体颜色。我需要将此应用于列表中的子项。我创建了一些代码来设置颜色,但不确定如何使用此信息对子项进行编码,这是 vb.net 的一个相当新的用户。

更新代码:

Dim tmpColor As Color = Color.Red

    While dr.Read()
        ListView1.Items.Add(CDate(dr(4).ToString())).UseItemStyleForSubItems = False
        ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(11).ToString())
        ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(7).ToString())
        with ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(CDbl(dr(5)).ToString("C")).ForeColor = tmpColor

    End With

    ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(14).ToString())
    ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(3).ToString())

End While

【问题讨论】:

标签: vb.net visual-studio-2010 visual-studio


【解决方案1】:

您必须将给定项目的UseItemStyleForSubItems 属性设置为false,然后对您想要的子项目执行相应的更改。示例代码:

Dim tmpColor As Color = Color.FromName("Blue") 'or just = Color.Blue

While dr.Read()

    ListView1.Items.Add(CDate(dr(4).ToString())).UseItemStyleForSubItems = False

    With ListView1.Items(ListView1.Items.Count - 1).SubItems
        With .Add(dr(11).ToString()))
            .ForeColor = tmpColor 'The font color of this column will be blue
        End With

        .Add(dr(7).ToString())
        .Add(CDbl(dr(5)).ToString("C"))
        .Add(dr(14).ToString())

        With .Add(dr(3).ToString())
            .ForeColor = tmpColor 'The font color of this column will be blue
        End With
    End With

End While

【讨论】:

  • 我已经用您建议的修改更新了我的原始代码,然后颜色仍然是黑色。谢谢
  • @user1532468 不客气(我正在查看您的代码,但没有发现任何错误)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-18
  • 1970-01-01
  • 2012-08-10
相关资源
最近更新 更多