Results 1 to 5 of 5

Thread: ListView SubItem Alignment

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    ListView SubItem Alignment

    I had a quick search in the forum (with arguments of 'ListView subitem alignment') but can't find an answer.

    Is it possible to change the Alignment property of an individual SubItem within a Listview? If so, could someone please describe how?

    I'm building up rows of data in the ListView and the last row is going to contain the totals for some of the columns. Where a column is not being summed I want to put a "-", aligned centrally, in the appropriate column in the 'Totals' row.

    Thanks

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: ListView SubItem Alignment

    You have to draw the listview yourself, here is an example, in that example, replace listView1_DrawSubItem with the following one
    vb Code:
    1. Private Sub listView1_DrawSubItem(ByVal sender As Object, _
    2.         ByVal e As DrawListViewSubItemEventArgs) _
    3.         Handles listView1.DrawSubItem
    4.  
    5.         Dim flags As TextFormatFlags = TextFormatFlags.Left
    6.  
    7.         Dim sf As New StringFormat()
    8.         Try
    9.  
    10.             ' Store the column text alignment, letting it default
    11.             ' to Left if it has not been set to Center or Right.
    12.             'Select Case e.Header.TextAlign
    13.             '    Case HorizontalAlignment.Center
    14.             '        sf.Alignment = StringAlignment.Center
    15.             '        flags = TextFormatFlags.HorizontalCenter
    16.             '    Case HorizontalAlignment.Right
    17.             '        sf.Alignment = StringAlignment.Far
    18.             '        flags = TextFormatFlags.Right
    19.             'End Select
    20.  
    21.  
    22.             ' This is 4x2y's code
    23.             '------------------------------------------------------------------------
    24.             If e.SubItem.Text = "-" Then
    25.                 sf.Alignment = StringAlignment.Center
    26.                 flags = TextFormatFlags.HorizontalCenter
    27.             Else
    28.                 sf.Alignment = StringAlignment.Near
    29.                 flags = TextFormatFlags.Left
    30.             End If
    31.             '-------------------------------------------------------------
    32.  
    33.  
    34.             ' Draw the text and background for a subitem with a
    35.             ' negative value.
    36.             Dim subItemValue As Double
    37.             If e.ColumnIndex > 0 AndAlso _
    38.                 Double.TryParse(e.SubItem.Text, NumberStyles.Currency, _
    39.                 NumberFormatInfo.CurrentInfo, subItemValue) AndAlso _
    40.                 subItemValue < 0 Then
    41.  
    42.                 ' Unless the item is selected, draw the standard
    43.                 ' background to make it stand out from the gradient.
    44.                 If (e.ItemState And ListViewItemStates.Selected) = 0 Then
    45.                     e.DrawBackground()
    46.                 End If
    47.  
    48.                 ' Draw the subitem text in red to highlight it.
    49.                 e.Graphics.DrawString(e.SubItem.Text, _
    50.                     Me.listView1.Font, Brushes.Red, e.Bounds, sf)
    51.  
    52.                 Return
    53.  
    54.             End If
    55.  
    56.             ' Draw normal text for a subitem with a nonnegative
    57.             ' or nonnumerical value.
    58.             e.DrawText(flags)
    59.  
    60.         Finally
    61.             sf.Dispose()
    62.         End Try
    63.  
    64.     End Sub

    That example uses heavy drawing, so you may remove all but keep the part that draw subitem text centered if it is "-"
    Last edited by 4x2y; Jul 10th, 2012 at 04:55 AM.



  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: ListView SubItem Alignment

    Thanks for the information. It seems like there's a lot of code required to perform a 'simple' function. I think I might look at a different control.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: ListView SubItem Alignment

    Does, doesn't it? Could you not simply add a few spaces to the "-" to centralise it with the present alignment?

  5. #5
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: ListView SubItem Alignment

    @Doogle
    Try the following custom listview
    vb Code:
    1. Public Class ListView
    2.     Inherits System.Windows.Forms.ListView
    3.  
    4.  
    5.     Public Sub New()
    6.         MyBase.New()
    7.         Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or _
    8.                     ControlStyles.AllPaintingInWmPaint Or _
    9.                     ControlStyles.EnableNotifyMessage, True)
    10.  
    11.         Me.OwnerDraw = True
    12.         Me.View = Windows.Forms.View.Details
    13.     End Sub
    14.  
    15.     Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
    16.         MyBase.OnMouseUp(e)
    17.         Dim lvItem As ListViewItem = Me.GetItemAt(8, e.Y)
    18.         If (lvItem IsNot Nothing) Then
    19.             lvItem.Selected = True
    20.             lvItem.Focused = True
    21.         End If
    22.     End Sub
    23.  
    24.     Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
    25.         MyBase.OnMouseMove(e)
    26.         Dim lvItem As ListViewItem = Me.GetItemAt(e.X, e.Y)
    27.         If lvItem IsNot Nothing Then
    28.             Me.Invalidate(lvItem.Bounds)
    29.         End If
    30.     End Sub
    31.  
    32.     Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawListViewItemEventArgs)
    33.         MyBase.OnDrawItem(e)
    34.  
    35.         If Not (e.State And ListViewItemStates.Selected) = 0 Then
    36.             e.Graphics.FillRectangle(Brushes.Beige, e.Bounds)
    37.             e.DrawFocusRectangle()
    38.         Else
    39.             e.Graphics.FillRectangle(Brushes.White, e.Bounds)
    40.         End If
    41.  
    42.     End Sub
    43.  
    44.     Protected Overrides Sub OnDrawSubItem(ByVal e As System.Windows.Forms.DrawListViewSubItemEventArgs)
    45.         MyBase.OnDrawSubItem(e)
    46.  
    47.         If e.SubItem.Text = "-" Then
    48.             e.DrawText(TextFormatFlags.HorizontalCenter)
    49.         Else
    50.             e.DrawText(TextFormatFlags.Left)
    51.         End If
    52.     End Sub
    53.  
    54.     Protected Overrides Sub OnDrawColumnHeader(ByVal e As System.Windows.Forms.DrawListViewColumnHeaderEventArgs)
    55.         MyBase.OnDrawColumnHeader(e)
    56.  
    57.         e.DrawBackground()
    58.         e.Graphics.DrawString(e.Header.Text, e.Font, Brushes.Black, e.Bounds, New Drawing.StringFormat(StringAlignment.Near))
    59.     End Sub
    60. End Class



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width