none
請問一下如何設定List View subitem的字型顏色 RRS feed

  • 問題

  • 我想再程式內設定一個判斷,ㄧ旦

           subitem的值> 1200時,值變成紅色

                             < 1200時,值變成黑色

    ex:

         Compnay        Amount

         Intel                1000

         Microsoft         2000

         Apple              1100

     

    可是我希望只有subitem的值變色,同一列的其他資料不要變顏色,請問我該怎麼處理.

    My code as following,

                If WW.Length - 1 > 1200 Then
                    lsvWWInfo.Items(i).SubItems.Item(1).ForeColor = Color.Red          
                End If

     

     

    麻煩懂得人敎一下哩, Thanks a lot.

     

    Best regards,

    Steven.

    2007年7月31日 上午 07:39

解答

  • :::

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            With ListView1
                .Columns.Add("Company", 100, HorizontalAlignment.Center)
                .Columns.Add("Amount", 100, HorizontalAlignment.Center)
            End With

            Dim i1 As New ListViewItem("Intel")
            Dim sub_i1 As New ListViewItem.ListViewSubItem
            sub_i1.Text = 1000
            i1.SubItems.Add(sub_i1)

            Dim i2 As New ListViewItem("Microsoft")
            Dim sub_i2 As New ListViewItem.ListViewSubItem
            sub_i2.Text = 2000
            i2.SubItems.Add(sub_i2)

            Dim i3 As New ListViewItem("Apple")
            Dim sub_i3 As New ListViewItem.ListViewSubItem
            sub_i3.Text = 1100
            i3.SubItems.Add(sub_i3)

            ListView1.Items.Add(i1)
            ListView1.Items.Add(i2)
            ListView1.Items.Add(i3)

        End Sub

        Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            For i As Integer = 0 To ListView1.Items.Count - 1
                Dim t As ListViewItem = ListView1.Items(i)
                t.UseItemStyleForSubItems = False
                If t.SubItems(1).Text > 1200 Then
                    t.SubItems(1).ForeColor = Color.Red
                End If
            Next
        End Sub

    2007年7月31日 下午 01:56

所有回覆

  • :::

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            With ListView1
                .Columns.Add("Company", 100, HorizontalAlignment.Center)
                .Columns.Add("Amount", 100, HorizontalAlignment.Center)
            End With

            Dim i1 As New ListViewItem("Intel")
            Dim sub_i1 As New ListViewItem.ListViewSubItem
            sub_i1.Text = 1000
            i1.SubItems.Add(sub_i1)

            Dim i2 As New ListViewItem("Microsoft")
            Dim sub_i2 As New ListViewItem.ListViewSubItem
            sub_i2.Text = 2000
            i2.SubItems.Add(sub_i2)

            Dim i3 As New ListViewItem("Apple")
            Dim sub_i3 As New ListViewItem.ListViewSubItem
            sub_i3.Text = 1100
            i3.SubItems.Add(sub_i3)

            ListView1.Items.Add(i1)
            ListView1.Items.Add(i2)
            ListView1.Items.Add(i3)

        End Sub

        Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            For i As Integer = 0 To ListView1.Items.Count - 1
                Dim t As ListViewItem = ListView1.Items(i)
                t.UseItemStyleForSubItems = False
                If t.SubItems(1).Text > 1200 Then
                    t.SubItems(1).ForeColor = Color.Red
                End If
            Next
        End Sub

    2007年7月31日 下午 01:56
  • 謝謝 小琳,

    我晚點會試試看.....感恩哩.

     

    2007年8月1日 上午 01:14
  • 動態寫法,供參考---

    Public Class Form1
        Dim buttonOK As New Button()
        Dim listView1 As New ListView()
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            CreateMyListView()
        End Sub

     

        Private Sub CreateMyListView()

            buttonOK.Location = New Point(215, 10)
            buttonOK.Size = New Size(75, 25)
            buttonOK.Text = "OK"
            listView1.Bounds = New Rectangle(New Point(10, 10), New Size(200, 200))
            With listView1
                .AllowColumnReorder = True
                .LabelEdit = True
                .FullRowSelect = True
                .View = View.Details
                .GridLines = True
                .Sorting = SortOrder.Ascending
            End With
            Dim Item1 As New ListViewItem("Intel")
            Item1.SubItems.Add("1000")

            Dim Item2 As New ListViewItem("Microsoft")
            Item2.SubItems.Add("2000")

            Dim Item3 As New ListViewItem("Apple")
            Item3.SubItems.Add("1100")

            listView1.Columns.Add("Company", -2, HorizontalAlignment.Left)
            listView1.Columns.Add("Amount", -2, HorizontalAlignment.Left)
            listView1.Items.AddRange(New ListViewItem() {Item1, Item2, Item3})

            Me.Controls.Add(listView1)
            Me.Controls.Add(buttonOK)
            AddHandler buttonOK.Click, AddressOf ChangeColor
        End Sub

     

        Private Sub ChangeColor(ByVal sender As System.Object, ByVal e As System.EventArgs)
            For i As Integer = 0 To listView1.Items.Count - 1
                Dim t As ListViewItem = listView1.Items(i)
                t.UseItemStyleForSubItems = False
                If t.SubItems(1).Text > 1200 Then
                    t.SubItems(1).ForeColor = Color.Red
                End If
            Next
        End Sub
    End Class

    2007年8月2日 上午 01:42