' /* *\
' |#* ListView Elektro *#|
' \* */
'
' // By Elektro H@cker
'
' Properties:
' ...........
' · Disable_Flickering
' · Double_Buffer
' · GridLineColor
' · ItemHighlightColor
' · ItemNotFocusedHighlighColor
' · DrawCustomGridLines
' · UseDefaultGridLines
' · Enable_ProgressBar
' · Progressbar_Column
' · Percent
' · Percent_Decimal
' · Percent_Font
' · Percent_Text
' · Percent_Forecolor
' · Percent_Text_Allignment
' · ProgressBar_BackColor
' · ProgressBar_BorderColor
' · ProgressBar_FillColor1
' · ProgressBar_FillColor2
'
' Events:
' .......
' · ItemAdded
' · ItemRemoved
'
' Methods:
' .......
' · AddItem
' · RemoveItem
Public Class ListView_Elektro : Inherits ListView
Public Event ItemAdded()
Public Event ItemRemoved()
Private _Disable_Flickering As Boolean = True
Private _gridLines As Boolean = False
Private _useDefaultGridLines As Boolean = False
Private _gridLineColor As Color = Color.Black
Private _itemHighlightColor As Color = Color.FromKnownColor(KnownColor.Highlight)
Private _itemNotFocusedHighlighColor As Color = Color.FromKnownColor(KnownColor.MenuBar)
Private _enable_progressbar As Boolean = False
Private _progressbar_column As Integer = Nothing
Private _percent As Double = 0
Private _percent_decimal As Short = 2
Private _percent_text As String = "%"
Private _percent_text_allignment As StringAlignment = StringAlignment.Center
Private _percent_stringformat As StringFormat = New StringFormat With {.Alignment = _percent_text_allignment}
Private _percent_font As Font = Me.Font
Private _percent_forecolor As SolidBrush = New SolidBrush(Color.Black)
Private _progressBar_backcolor As SolidBrush = New SolidBrush(Color.Red)
Private _progressBar_bordercolor As Pen = New Pen(Color.LightGray)
Private _progressBar_fillcolor1 As Color = Color.YellowGreen
Private _progressBar_fillcolor2 As Color = Color.White
Public Sub New()
Me.Name = "ListView_Elektro"
Me.DoubleBuffered = True
Me.UseDefaultGridLines = True
' Set Listview OwnerDraw to True, so we can draw the progressbar inside.
If Me.Enable_ProgressBar Then Me.OwnerDraw = True
' Me.GridLines = True
' Me.MultiSelect = True
' Me.FullRowSelect = True
' Me.View = View.Details
End Sub
#Region " Properties "
''' <summary>
''' Enable/Disable any flickering effect on the ListView.
''' </summary>
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
If _Disable_Flickering Then
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or &H2000000
Return cp
Else
Return MyBase.CreateParams
End If
End Get
End Property
''' <summary>
''' Set the Double Buffer.
''' </summary>
Public Property Double_Buffer() As Boolean
Get
Return Me.DoubleBuffered
End Get
Set(ByVal Value As Boolean)
Me.DoubleBuffered = Value
End Set
End Property
''' <summary>
''' Enable/Disable the flickering effects on this ListView.
'''
''' This property turns off any Flicker effect on the ListView
''' ...but also reduces the performance (speed) of the ListView about 30% slower.
''' This don't affect to the performance of the application itself, only to the performance of this control.
''' </summary>
Public Property Disable_Flickering() As Boolean
Get
Return _Disable_Flickering
End Get
Set(ByVal Value As Boolean)
Me._Disable_Flickering = Value
End Set
End Property
''' <summary>
''' Changes the gridline color.
''' </summary>
Public Property GridLineColor() As Color
Get
Return _gridLineColor
End Get
Set(ByVal value As Color)
If value <> _gridLineColor Then
_gridLineColor = value
If _gridLines Then
Me.Invalidate()
End If
End If
End Set
End Property
''' <summary>
''' Changes the color when item is highlighted.
''' </summary>
Public Property ItemHighlightColor() As Color
Get
Return _itemHighlightColor
End Get
Set(ByVal value As Color)
If value <> _itemHighlightColor Then
_itemHighlightColor = value
Me.Invalidate()
End If
End Set
End Property
''' <summary>
''' Changes the color when the item is not focused.
''' </summary>
Public Property ItemNotFocusedHighlighColor() As Color
Get
Return _itemNotFocusedHighlighColor
End Get
Set(ByVal value As Color)
If value <> _itemNotFocusedHighlighColor Then
_itemNotFocusedHighlighColor = value
Me.Invalidate()
End If
End Set
End Property
Private ReadOnly Property DrawCustomGridLines() As Boolean
Get
Return (_gridLines And Not _useDefaultGridLines)
End Get
End Property
Public Shadows Property GridLines() As Boolean
Get
Return _gridLines
End Get
Set(ByVal value As Boolean)
_gridLines = value
End Set
End Property
''' <summary>
''' use the default gridlines.
''' </summary>
Public Property UseDefaultGridLines() As Boolean
Get
Return _useDefaultGridLines
End Get
Set(ByVal value As Boolean)
If _useDefaultGridLines <> value Then
_useDefaultGridLines = value
End If
MyBase.GridLines = value
MyBase.OwnerDraw = Not value
End Set
End Property
#End Region
#Region " Procedures "
''' <summary>
''' Monitors when an Item is added to the ListView.
''' </summary>
Public Function AddItem(ByVal Text As String) As ListViewItem
RaiseEvent ItemAdded()
Return MyBase.Items.Add(Text)
End Function
''' <summary>
''' Monitors when an Item is removed from the ListView.
''' </summary>
Public Sub RemoveItem(ByVal Item As ListViewItem)
RaiseEvent ItemRemoved()
MyBase.Items.Remove(Item)
End Sub
Protected Overrides Sub OnDrawColumnHeader(ByVal e As DrawListViewColumnHeaderEventArgs)
e.DrawDefault = True
MyBase.OnDrawColumnHeader(e)
End Sub
Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
For Each selectedIndex As Integer In MyBase.SelectedIndices
MyBase.RedrawItems(selectedIndex, selectedIndex, False)
Next
MyBase.OnLostFocus(e)
End Sub
Protected Overrides Sub OnDrawSubItem(ByVal e As DrawListViewSubItemEventArgs)
Dim drawAsDefault As Boolean = False
Dim highlightBounds As Rectangle = Nothing
Dim highlightBrush As SolidBrush = Nothing
'FIRST DETERMINE THE COLOR
If e.Item.Selected Then
If MyBase.Focused Then
highlightBrush = New SolidBrush(_itemHighlightColor)
ElseIf HideSelection Then
drawAsDefault = True
Else
highlightBrush = New SolidBrush(_itemNotFocusedHighlighColor)
End If
Else
drawAsDefault = True
End If
If drawAsDefault Then
e.DrawBackground()
Else
'NEXT DETERMINE THE BOUNDS IN WHICH TO DRAW THE BACKGROUND
If FullRowSelect Then
highlightBounds = e.Bounds
Else
highlightBounds = e.Item.GetBounds(ItemBoundsPortion.Label)
End If
'ONLY DRAW HIGHLIGHT IN 1 OF 2 CASES
'CASE 1 - FULL ROW SELECT (AND DRAWING ANY ITEM)
'CASE 2 - NOT FULL ROW SELECT (AND DRAWING 1ST ITEM)
If FullRowSelect Then
e.Graphics.FillRectangle(highlightBrush, highlightBounds)
ElseIf e.ColumnIndex = 0 Then
e.Graphics.FillRectangle(highlightBrush, highlightBounds)
Else
e.DrawBackground()
End If
End If
e.DrawText()
If _gridLines Then
e.Graphics.DrawRectangle(New Pen(_gridLineColor), e.Bounds)
End If
If FullRowSelect Then
e.DrawFocusRectangle(e.Item.GetBounds(ItemBoundsPortion.Entire))
Else
e.DrawFocusRectangle(e.Item.GetBounds(ItemBoundsPortion.Label))
End If
MyBase.OnDrawSubItem(e)
End Sub
#End Region
#Region " ProgressBar Properties "
''' <summary>
''' Enables the drawing of a ProgressBar
''' This property should be "True" to use any of the ProgressBar properties.
''' </summary>
Public Property Enable_ProgressBar As Boolean
Get
Return _enable_progressbar
End Get
Set(ByVal value As Boolean)
Me.OwnerDraw = value
_enable_progressbar = value
End Set
End Property
''' <summary>
''' The column index to draw the ProgressBar
''' </summary>
Public Property Progressbar_Column As Integer
Get
Return _progressbar_column
End Get
Set(ByVal value As Integer)
_progressbar_column = value
End Set
End Property
''' <summary>
''' The ProgressBar progress percentage
''' </summary>
Public Property Percent As Double
Get
Return _percent
End Get
Set(ByVal value As Double)
_percent = value
End Set
End Property
''' <summary>
''' The decimal factor which should be displayed for the ProgressBar progress percentage
''' </summary>
Public Property Percent_Decimal As Short
Get
Return _percent_decimal
End Get
Set(ByVal value As Short)
_percent_decimal = value
End Set
End Property
''' <summary>
''' The Font to be used as the ProgressBar Percent text
''' </summary>
Public Property Percent_Font As Font
Get
Return _percent_font
End Get
Set(ByVal value As Font)
_percent_font = value
End Set
End Property
''' <summary>
''' The additional text to add to the ProgressBar Percent value
''' </summary>
Public Property Percent_Text As String
Get
Return _percent_text
End Get
Set(ByVal value As String)
_percent_text = value
End Set
End Property
''' <summary>
''' The ForeColor of the ProgressBar Percent Text
''' </summary>
Public Property Percent_Forecolor As Color
Get
Return _percent_forecolor.Color
End Get
Set(ByVal value As Color)
_percent_forecolor = New SolidBrush(value)
End Set
End Property
''' <summary>
''' The text allignment to use for the ProgressBar
''' </summary>
Public Property Percent_Text_Allignment As StringAlignment
Get
Return _percent_stringformat.Alignment
End Get
Set(ByVal value As StringAlignment)
_percent_stringformat.Alignment = value
End Set
End Property
''' <summary>
''' The ProgressBar BackColor
''' </summary>
Public Property ProgressBar_BackColor As Color
Get
Return _progressBar_backcolor.Color
End Get
Set(ByVal value As Color)
_progressBar_backcolor = New SolidBrush(value)
End Set
End Property
''' <summary>
''' The ProgressBar BorderColor
''' </summary>
Public Property ProgressBar_BorderColor As Color
Get
Return _progressBar_bordercolor.Color
End Get
Set(ByVal value As Color)
_progressBar_bordercolor = New Pen(value)
End Set
End Property
''' <summary>
''' The First ProgressBar Gradient color
''' </summary>
Public Property ProgressBar_FillColor1 As Color
Get
Return _progressBar_fillcolor1
End Get
Set(ByVal value As Color)
_progressBar_fillcolor1 = value
End Set
End Property
''' <summary>
''' The Last ProgressBar Gradient color
''' </summary>
Public Property ProgressBar_FillColor2 As Color
Get
Return _progressBar_fillcolor2
End Get
Set(ByVal value As Color)
_progressBar_fillcolor2 = value
End Set
End Property
#End Region
#Region " ProgressBar EventHandlers "
' ListView [DrawColumnHeader]
Public Sub Me_DrawColumnHeader(ByVal sender As Object, ByVal e As DrawListViewColumnHeaderEventArgs) Handles Me.DrawColumnHeader
e.DrawDefault = True ' Draw default ColumnHeader.
End Sub
' ListView [DrawItem]
Public Sub Me_DrawItem(ByVal sender As Object, ByVal e As DrawListViewItemEventArgs) 'Handles Me.DrawItem
e.DrawDefault = False ' Draw default main item.
End Sub
' ListView [DrawSubItem]
Public Sub Me_DrawSubItem(ByVal sender As Object, ByVal e As DrawListViewSubItemEventArgs) Handles Me.DrawSubItem
If (e.ItemState And ListViewItemStates.Selected) <> 0 Then
' Item is highlighted.
e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds)
End If
' Draw the progressbar.
If e.ColumnIndex = Me.Progressbar_Column Then
If (Not Me.Enable_ProgressBar OrElse Me.Progressbar_Column = Nothing) Then Exit Sub
' Background color of the progressbar is white.
e.Graphics.FillRectangle(Me._progressBar_backcolor, e.Bounds)
' This creates a nice color gradient to fill.
Dim brGradient As Brush = _
New System.Drawing.Drawing2D.LinearGradientBrush(New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), _
Me.ProgressBar_FillColor1, Me.ProgressBar_FillColor2, 270, True)
' Draw the actual progressbar.
e.Graphics.FillRectangle(brGradient, _
e.Bounds.X + 1, e.Bounds.Y + 2, _
CInt(((Me.Percent) / 100) * (e.Bounds.Width - 2)), e.Bounds.Height - 3)
' Draw the percentage number and percent sign.
e.Graphics.DrawString(Me.Percent.ToString("n" & Me.Percent_Decimal) & Me.Percent_Text, _
Me.Percent_Font, Me._percent_forecolor, _
CSng(e.Bounds.X + (e.Bounds.Width / 2)), e.Bounds.Y + 3, _
_percent_stringformat)
' Draw a light gray rectangle/border around the progressbar.
e.Graphics.DrawRectangle(Me._progressBar_bordercolor, _
e.Bounds.X, e.Bounds.Y + 1, _
e.Bounds.Width - 1, e.Bounds.Height - 2)
Else
e.DrawDefault = True
End If
End Sub
#End Region
End Class