Link to home
Start Free TrialLog in
Avatar of regisdaniel
regisdaniel

asked on

Inherited ListView Control with a Custom Text Field in the ColumnHeader Collection

Hi all!

I need to make a custom listview control that has a custom textfield in each column that i create. This field will be filled by the end programmer when he uses my control, that need this information (on each column) to make some calculations.

I tried to create a inherited ColumnHeaderObject, that has a new property that i use to set what i want. (See code below).
When I insert my NewControl on a form, and Add Columns to It, everthing shows to be fine, but, when i close the form and reopen it, the columns previosly declared was not showed. The listview looks like if was cleared, but the objects are declared on form.

Does anybody can help me solving this?
Thanks all!

'Inherited ColumnHeaderClass that declares my CustomField called mField.
Public Class ColumnHeaderXP
    Inherits ColumnHeader

    Private mField As String

    Public Property Field() As String
        Get
            Return mField
        End Get
        Set(ByVal Value As String)
            mField = Value
        End Set
    End Property

End Class

'My ColumnHeaderXPCollection that i use to work with my usercreated ColumnHeader
Public Class ColumnHeaderXPCollection
    Inherits CollectionBase

    Public Event Invalidate()

    Protected Sub OnInvalidate()
        RaiseEvent Invalidate()
    End Sub

    Default Public ReadOnly Property Item(ByVal Index As Integer) As ColumnHeaderXP
        Get
            Return CType(list.Item(Index), ColumnHeaderXP)
        End Get
    End Property

    Public Sub Add(ByVal Field As String)
        Dim col As New ColumnHeaderXP
        col.Text = Field
        col.Field = Field
        list.Add(col)
    End Sub

    Public Sub Add(ByVal Field As String, ByVal Width As Integer)
        Dim col As New ColumnHeaderXP
        col.Text = Field
        col.Field = Field
        col.Width = Width
        list.Add(col)
    End Sub

    Public Sub Add(ByVal Text As String, ByVal Field As String)
        Dim col As New ColumnHeaderXP
        col.Text = Text
        col.Field = Field
        list.Add(col)
    End Sub

    Public Sub Add(ByVal Text As String, ByVal Field As String, ByVal Width As Integer)
        Dim col As New ColumnHeaderXP
        col.Text = Text
        col.Field = Field
        col.Width = Width
        list.Add(col)
    End Sub

    Public Sub Add(ByVal Item As ColumnHeaderXP)
        list.Add(Item)
    End Sub

    Protected Overrides Sub OnRemoveComplete(ByVal index As Integer, ByVal value As Object)
        OnInvalidate()
    End Sub

    Protected Overrides Sub OnInsertComplete(ByVal index As Integer, ByVal value As Object)
        OnInvalidate()
    End Sub

    Protected Overrides Sub OnSetComplete(ByVal index As Integer, ByVal oldValue As Object, ByVal newValue As Object)
        OnInvalidate()
    End Sub

    Protected Overrides Sub OnClearComplete()
        OnInvalidate()
    End Sub
End Class

'My control itself
Option Strict On

Imports System.ComponentModel
Imports System.Reflection

Public Class ListViewXP
    Inherits System.Windows.Forms.ListView

    Private WithEvents mColumns As ColumnHeaderXPCollection

    'Constructor
    Public Sub New()
        mColumns = New ColumnHeaderXPCollection
        View = View.Details
        FullRowSelect = True
        MultiSelect = False
    End Sub

    'Load the columns
    Private Sub DataBind()

        Clear()

        Dim Field As Integer

        ' load the column headers
        For Field = 0 To mColumns.Count - 1
            MyBase.Columns.Add(mColumns(Field))
        Next

    End Sub

    'User defined Property Column
    Public Shadows ReadOnly Property Columns() As ColumnHeaderXPCollection
        Get
            Return mColumns
        End Get
    End Property

    'When a Add or Change a Column, This event is Fired
    Private Sub mItems_Invalidate() Handles mColumns.Invalidate
        DataBind()
    End Sub

End Class

Thaks all!
ASKER CERTIFIED SOLUTION
Avatar of jcesarf
jcesarf

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial