Imports System.ComponentModel
Imports System.Drawing
Public Class MKPPListView
Inherits System.Windows.Forms.ListView
#Region "<< Variables >>"
Private _columns As ArrayList
Private _captions As ArrayList
Private _widths As ArrayList
Private _dataSource As Object
Private _start As Int32
Private _finish As Int32
Private _contextMenu As ContextMenu
Private _helpText As String
Private _sorting As Boolean
#End Region
#Region "<< Properties >>"
<Browsable(True), DefaultValue(True)> _
Public Property AllowSorting() As Boolean
Get
Return _sorting
End Get
Set(ByVal Value As Boolean)
_sorting = Value
End Set
End Property
<Browsable(True)> _
Public Property HelpText() As String
Get
Return _helpText
End Get
Set(ByVal Value As String)
_helpText = Value
End Set
End Property
<Browsable(False)> _
Public ReadOnly Property SelectedIndex() As Int32
Get
For i As Int32 = 0 To Me.Items.Count - 1
Dim row As ListViewItem = Me.Items(i)
If row.Selected = True Then
Return i
End If
Next
Return -1
End Get
End Property
Public Property RowFilter() As String
Get
If TypeOf _dataSource Is DataView Then
Return CType(_dataSource, DataView).RowFilter
End If
End Get
Set(ByVal Value As String)
If TypeOf _dataSource Is DataView Then
CType(_dataSource, DataView).RowFilter = Value
End If
End Set
End Property
Public Property SortData() As String
Get
If TypeOf _dataSource Is DataView Then
Return CType(_dataSource, DataView).Sort
End If
End Get
Set(ByVal Value As String)
If TypeOf _dataSource Is DataView Then
CType(_dataSource, DataView).Sort = Value
MyBase.Items.Clear()
SetupListView()
End If
End Set
End Property
Public ReadOnly Property Count() As Int32
Get
Return MyBase.Items.Count
End Get
End Property
#End Region
#Region "<< Startup >>"
Public Sub New()
MyBase.New()
_columns = New ArrayList
_widths = New ArrayList
_captions = New ArrayList
MyBase.View = View.Details
MyBase.FullRowSelect = True
MyBase.MultiSelect = False
MyBase.ForeColor = Color.Yellow
MyBase.BackColor = Color.Black
MyBase.AllowColumnReorder = True
MyBase.Scrollable = True
_sorting = True
AddColumn("", "", 0, HorizontalAlignment.Center)
_contextMenu = New ContextMenu
Me.ContextMenu = _contextMenu
Me.ContextMenu.MenuItems.Add(New MenuItem("What's This?", AddressOf WhatsThis))
End Sub
#End Region
#Region "<< Helper Methods >>"
Private Sub WhatsThis(ByVal sender As Object, ByVal e As EventArgs)
Help.ShowPopup(Me, HelpText, System.Windows.Forms.Form.MousePosition)
End Sub
#End Region
#Region "<< Public Methods >>"
Public Sub SetCaption(ByVal index As Int32, ByVal newCaption As String)
_captions(index) = newCaption
Me.Columns(index).Text = newCaption
End Sub
Public Function SelectedItem(ByVal Column As String) As String
Dim i As Int32
For i = 0 To _columns.Count - 1
If CType(_columns(i), String) = Column Then
Return SelectedItem(i)
End If
Next
Throw New DataException(Column & " does not exist!")
End Function
Public Function SelectedItem(ByVal Column As Int32) As String
Dim selected As New ListView.SelectedListViewItemCollection(Me)
If selected.Count = 0 Then
Return Nothing
Else
Return selected.Item(0).SubItems(Column).Text
End If
End Function
Public Function SelectedItem() As ListViewItem
Dim selected As New ListView.SelectedListViewItemCollection(Me)
If selected.Count = 0 Then
Return Nothing
Else
Return selected.Item(0)
End If
End Function
Public Sub TypeFind(ByVal item As String, ByVal Column As String)
Dim i As Int32
For i = 0 To _columns.Count - 1
If CType(_columns(i), String) = Column Then
TypeFind(item, i)
Exit For
End If
Next
End Sub
Public Sub TypeFind(ByVal item As String, ByVal Column As Int32)
Dim i As Int32
For i = 0 To MyBase.Items.Count - 1
If MyBase.Items(i).SubItems(Column).Text.ToLower.StartsWith(item.ToLower) Then
MyBase.Items(i).EnsureVisible()
MyBase.Items(i).Selected = True
Exit For
End If
Next
End Sub
Public Sub SetAlternateRowColor(ByVal ForeColor As Color, ByVal BackColor As Color)
Dim i As Int32
For i = 0 To MyBase.Items.Count - 1
If i Mod 2 = 1 Then
MyBase.Items(i).BackColor = BackColor
MyBase.Items(i).ForeColor = ForeColor
End If
Next
End Sub
#End Region
#Region "<< Add Column >>"
Public Sub AddColumn(ByVal Column As String, ByVal Caption As String)
AddColumn(Column, Caption, 0)
End Sub
Public Sub AddColumn(ByVal Column As String, ByVal Caption As String, ByVal Width As Int32)
AddColumn(Column, Caption, Width, HorizontalAlignment.Left)
End Sub
Public Sub AddColumn(ByVal Column As String, ByVal Caption As String, ByVal Alignment As HorizontalAlignment)
AddColumn(Column, Caption, 0, Alignment)
End Sub
Public Sub AddColumn(ByVal Column As String, ByVal Caption As String, ByVal Width As Int32, ByVal Alignment As HorizontalAlignment)
_columns.Add(Column)
_captions.Add(Caption)
_widths.Add(Width)
MyBase.Columns.Add(Caption, Width, Alignment)
End Sub
#End Region
#Region "<< Load Data >>"
Public Sub LoadData(ByRef Data As DataTable)
LoadData(Data, 0, Data.Rows.Count - 1)
End Sub
Public Sub LoadData(ByRef Data As DataView)
LoadData(Data, 0, Data.Count - 1)
End Sub
Public Sub LoadData(ByRef Data As DataTable, ByVal Start As Int32, ByVal Finish As Int32)
LoadData(New DataView(Data), Start, Finish)
End Sub
Public Sub LoadData(ByRef Data As DataView, ByVal Start As Int32, ByVal Finish As Int32)
MyBase.Items.Clear()
_dataSource = Data
_start = Start
_finish = Finish
SetupListView()
End Sub
#End Region
#Region "<< SetupListView >>"
Private Sub SetupListView()
Dim i As Int32, j As Int32
If TypeOf _dataSource Is DataTable Then
With CType(_dataSource, DataTable)
For i = _start To _finish
Dim item As New ListViewItem(.Rows(i)(CType(_columns(1), Int32)).ToString)
For j = 1 To _columns.Count - 1
item.SubItems.Add(.Rows(i)(CType(_columns(j), Int32)).ToString)
Next
MyBase.Items.Add(item)
Next
End With
ElseIf TypeOf _dataSource Is DataView Then
With CType(_dataSource, DataView)
For i = _start To _finish
Dim item As New ListViewItem(.Item(i)(CType(_columns(1), String)).ToString)
For j = 1 To _columns.Count - 1
item.SubItems.Add(.Item(i)(CType(_columns(j), String)).ToString)
Next
MyBase.Items.Add(item)
Next
End With
End If
If MyBase.Columns(1).Width = 0 Then
Dim gr As Graphics = MyBase.CreateGraphics
For i = 1 To _columns.Count - 1
Dim width As Int32 = CType(gr.MeasureString(MyBase.Columns(i).Text, Me.Font).Width, Int32)
width += 5
For j = 0 To MyBase.Items.Count - 1
Dim myWidth As Int32 = CType(gr.MeasureString(MyBase.Items(j).SubItems(i).Text, MyBase.Font).Width, Int32)
If i > 1 Then
myWidth += 5
End If
If myWidth > width Then
width = myWidth
End If
Next
MyBase.Columns(i).Width = width
width = 0
Next
End If
End Sub
#End Region
#Region "<< Column Sorting >>"
Private Sub MKPPListView_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles MyBase.ColumnClick
If AllowSorting Then
If TypeOf _dataSource Is DataView Then
Dim col As String = MyBase.Columns(e.Column).Text
Dim i As Int32
For i = 0 To _captions.Count - 1
If CType(_captions(i), String) = col Then
col = CType(_columns(i), String)
Exit For
End If
Next
If CType(_dataSource, DataView).Sort = col Then
CType(_dataSource, DataView).Sort = col & " DESC"
Else
CType(_dataSource, DataView).Sort = col
End If
MyBase.Items.Clear()
SetupListView()
End If
End If
End Sub
#End Region
End Class