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
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:
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()
Try
' Store the column text alignment, letting it default
' to Left if it has not been set to Center or Right.
'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
' This is 4x2y's code
'------------------------------------------------------------------------
If e.SubItem.Text = "-" Then
sf.Alignment = StringAlignment.Center
flags = TextFormatFlags.HorizontalCenter
Else
sf.Alignment = StringAlignment.Near
flags = TextFormatFlags.Left
End If
'-------------------------------------------------------------
' Draw the text and background for a subitem with a
' negative value.
Dim subItemValue As Double
If e.ColumnIndex > 0 AndAlso _
Double.TryParse(e.SubItem.Text, NumberStyles.Currency, _
NumberFormatInfo.CurrentInfo, subItemValue) AndAlso _
subItemValue < 0 Then
' Unless the item is selected, draw the standard
' background to make it stand out from the gradient.
If (e.ItemState And ListViewItemStates.Selected) = 0 Then
e.DrawBackground()
End If
' Draw the subitem text in red to highlight it.
e.Graphics.DrawString(e.SubItem.Text, _
Me.listView1.Font, Brushes.Red, e.Bounds, sf)
Return
End If
' Draw normal text for a subitem with a nonnegative
' or nonnumerical value.
e.DrawText(flags)
Finally
sf.Dispose()
End Try
End Sub
That example uses heavy drawing, so you may remove all but keep the part that draw subitem text centered if it is "-"
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.
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?
Re: ListView SubItem Alignment
@Doogle
Try the following custom listview
vb Code:
Public Class ListView
Inherits System.Windows.Forms.ListView
Public Sub New()
MyBase.New()
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or _
ControlStyles.AllPaintingInWmPaint Or _
ControlStyles.EnableNotifyMessage, True)
Me.OwnerDraw = True
Me.View = Windows.Forms.View.Details
End Sub
Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseUp(e)
Dim lvItem As ListViewItem = Me.GetItemAt(8, e.Y)
If (lvItem IsNot Nothing) Then
lvItem.Selected = True
lvItem.Focused = True
End If
End Sub
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseMove(e)
Dim lvItem As ListViewItem = Me.GetItemAt(e.X, e.Y)
If lvItem IsNot Nothing Then
Me.Invalidate(lvItem.Bounds)
End If
End Sub
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawListViewItemEventArgs)
MyBase.OnDrawItem(e)
If Not (e.State And ListViewItemStates.Selected) = 0 Then
e.Graphics.FillRectangle(Brushes.Beige, e.Bounds)
e.DrawFocusRectangle()
Else
e.Graphics.FillRectangle(Brushes.White, e.Bounds)
End If
End Sub
Protected Overrides Sub OnDrawSubItem(ByVal e As System.Windows.Forms.DrawListViewSubItemEventArgs)
MyBase.OnDrawSubItem(e)
If e.SubItem.Text = "-" Then
e.DrawText(TextFormatFlags.HorizontalCenter)
Else
e.DrawText(TextFormatFlags.Left)
End If
End Sub
Protected Overrides Sub OnDrawColumnHeader(ByVal e As System.Windows.Forms.DrawListViewColumnHeaderEventArgs)
MyBase.OnDrawColumnHeader(e)
e.DrawBackground()
e.Graphics.DrawString(e.Header.Text, e.Font, Brushes.Black, e.Bounds, New Drawing.StringFormat(StringAlignment.Near))
End Sub
End Class