none
在外面的一个Button1中,如何在istview1里的某项中绘制一个矩形,谢谢!!!,请教高手予以帮助,谢谢!!! RRS feed

  • 问题

  • 我在listvie里离自己绘制了内容(矩形),我想在外面的一个Button1中,改变矩形的填充色,请教高手予以帮助,谢谢!!!

    Private Sub listView1_DrawSubItem(ByVal sender As Object, ByVal e As DrawListViewSubItemEventArgs) Handles ListView1.DrawSubItem
      Dim flags As TextFormatFlags = TextFormatFlags.Left
      Dim sf As New StringFormat()
      Dim SubItemRect As Rectangle
      Dim i As Short
      Try
      Select Case e.Header.TextAlign
      Case HorizontalAlignment.Center
      sf.Alignment = StringAlignment.Center
      flags = TextFormatFlags.HorizontalCenter
      Case HorizontalAlignment.Right
      sf.Alignment = StringAlignment.Far
      flags = TextFormatFlags.Right
      End Select
      ' For i = 1 To ListView1.Items.Count
      If e.ColumnIndex = 1 Then
      SubItemRect = New Drawing.Rectangle(e.Bounds.Left + 5, e.Bounds.Top + 2, e.Bounds.Width - 10, e.Bounds.Height - 5)
      e.Graphics.FillRectangle(Brushes.Green, SubItemRect)
       Return
      End If
      e.DrawText(flags)
      Finally
      sf.Dispose()
      End Try
      End Sub

    2010年7月20日 23:28

答案

  • If e.ItemIndex = 1 AndAlso e.ColumnIndex = 1 Then
                    SubItemRect = New Drawing.Rectangle(e.Bounds.Left + 5, e.Bounds.Top + 2, e.Bounds.Width - 10, e.Bounds.Height - 5)
                    e.Graphics.FillRectangle(Brushes.Green, SubItemRect)
                End If
    http://feiyun0112.cnblogs.com/
    2010年7月30日 1:53
    版主

全部回复

  • Brushes.Green用全局变量代替,Button1中改变,然后listView1.invalidate
    http://feiyun0112.cnblogs.com/
    2010年7月21日 0:44
    版主
  • 谢谢!!!还请你帮助看看:e.ColumnIndex = 1这句是用来判断列的

    下面的句中,我用 If Double.TryParse(e.SubItem.Text, NumberStyles.Currency,  NumberFormatInfo.CurrentInfo, subItemValue) AndAlso  subItemValue = 0 Then来判断单元格是否为我要的单元格,我想通过列和行来判断是否为我要的单元格,可以做到吗,谢谢!!!

     Private Sub listView1_DrawSubItem(ByVal sender As Object, ByVal e As DrawListViewSubItemEventArgs) Handles ListView1.DrawSubItem
            Dim flags As TextFormatFlags = TextFormatFlags.Left
            Dim sf As New StringFormat()

            Dim i As Short
            Try
           
                Select Case e.Header.TextAlign
                    Case HorizontalAlignment.Center
                        sf.Alignment = StringAlignment.Center
                        flags = TextFormatFlags.HorizontalCenter
                    Case HorizontalAlignment.Right
                        sf.Alignment = StringAlignment.Far
                        flags = TextFormatFlags.Right
                End Select

                Dim subItemValue As Double
                If e.ColumnIndex = 1 Then
                    SubItemRect = New Drawing.Rectangle(e.Bounds.Left + 5, e.Bounds.Top + 2, e.Bounds.Width - 10, e.Bounds.Height - 5)
                    If Double.TryParse(e.SubItem.Text, NumberStyles.Currency, _
                    NumberFormatInfo.CurrentInfo, subItemValue) AndAlso _
                    subItemValue = 0 Then
                        e.Graphics.FillRectangle(Brushes.Green, SubItemRect)
                    End If

                    If Double.TryParse(e.SubItem.Text, NumberStyles.Currency, _
                   NumberFormatInfo.CurrentInfo, subItemValue) AndAlso _
                   subItemValue = 1 Then
                        e.Graphics.FillRectangle(Brushes.Red, SubItemRect)
                    End If

                    If Double.TryParse(e.SubItem.Text, NumberStyles.Currency, _
                     NumberFormatInfo.CurrentInfo, subItemValue) AndAlso _
                     subItemValue = 2 Then
                        e.Graphics.FillRectangle(Brushes.Yellow, SubItemRect)
                    End If

                    If Double.TryParse(e.SubItem.Text, NumberStyles.Currency, _
                    NumberFormatInfo.CurrentInfo, subItemValue) AndAlso _
                    subItemValue = 3 Then
                        e.Graphics.FillRectangle(Brushes.Maroon, SubItemRect)
                    End If

                    e.Graphics.DrawRectangle(Pens.Black, SubItemRect)
                    Return
                End If
                    e.DrawText(flags)
                      Finally
                sf.Dispose()
            End Try
        End Sub

     

    2010年7月21日 1:48
  • 请教高手予以帮助,谢谢!!!
    2010年7月21日 8:56
  • If e.ItemIndex = 1 AndAlso e.ColumnIndex = 1 Then
                    SubItemRect = New Drawing.Rectangle(e.Bounds.Left + 5, e.Bounds.Top + 2, e.Bounds.Width - 10, e.Bounds.Height - 5)
                    e.Graphics.FillRectangle(Brushes.Green, SubItemRect)
                End If
    http://feiyun0112.cnblogs.com/
    2010年7月30日 1:53
    版主