Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

Adding a Property to the ListView's ColumnHeader

Hy all!

I need make a control that will have a custom text property on the columnheader collection. I was searching over the internet for one week, i don't found a way of doing this. Can anybody shows me an example (or where i can get one) for VB.Net to help me implementing this?

Thanks all!
0
regisdaniel
Asked:
regisdaniel
  • 3
  • 2
1 Solution
 
RonaldBiemansCommented:
Hi regisdaniel,

something like this ?

Public Class Mycolumnheader
    Inherits ColumnHeader
    Private _extra As String

    Public Property extra() As String
        Get
            Return _extra
        End Get
        Set(ByVal Value As String)
            _extra = Value
        End Set
    End Property
End Class

implement like this

Dim h As New Mycolumnheader
        h.extra = "test"
        h.Text = "yourcolumnname"
        ListView1.Columns.Add(h)

to get the extra information from the column header use the columnclick event of the listview

Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick
        MsgBox(CType(ListView1.Columns.Item(e.Column), Mycolumnheader).extra())
    End Sub
0
 
regisdanielAuthor Commented:
Ok. But i want be able change these properties on the form editor.
How  can i do this?
0
 
RonaldBiemansCommented:
Well, luckily you don't want much ;-)

try this

Public Class mylistview2
    Inherits ListView

    Private WithEvents mColumns As New ColumnCollection

    Shadows ReadOnly Property Columns() As ColumnCollection
        Get
            Return mColumns
        End Get
    End Property

End Class

Public Class ColumnCollection
    Inherits CollectionBase
   
    Default Public Property Item(ByVal index As Integer) As Column
        Get
            Return CType(List(index), Column)
        End Get
        Set(ByVal Value As Column)
            List(index) = Value
        End Set
    End Property


    Public Function Add(ByVal value As Column) As Integer
        Dim result As Integer
        result = List.Add(value)
        AddHandler value.Changed, AddressOf OnChanged
        OnChanged(Me, New EventArgs)
        Return result
    End Function


    Public Function Add(ByVal text As String) As Column
        Dim result As New Column
        result.Text = text
        Add(result)
        Return result
    End Function


    Public Function Add(ByVal text As String, ByVal width As Integer) As Column
        Dim result As New Column
        result.Text = text
        result.Width = width
        Add(result)
        Return result
    End Function


    Public Function IndexOf(ByVal value As Column) As Integer
        Return List.IndexOf(value)
    End Function


    Public Sub Insert(ByVal index As Integer, ByVal value As Column)
        List.Insert(index, value)
        OnChanged(Me, New EventArgs)
    End Sub


    Public Sub Remove(ByVal value As Column)
        List.Remove(value)
        OnChanged(Me, New EventArgs)
    End Sub


    Public Function Contains(ByVal value As Column) As Boolean
        Return List.Contains(value)
    End Function


    Public Event Changed(ByVal sender As Object, ByVal e As EventArgs)
    Protected Friend Sub OnChanged(ByVal sender As Object, ByVal e As EventArgs)
        RaiseEvent Changed(sender, e)
    End Sub

End Class


Public Class Column

    Private mText As String = "Column"
    Private mExtraText As String = "Extratext"
    Property Text() As String
        Get
            Return mText
        End Get
        Set(ByVal Value As String)
            If mText <> Value Then
                mText = Value
                OnChanged(Me, New EventArgs)
            End If
        End Set
    End Property

    Property ExtraText() As String
        Get
            Return mExtraText
        End Get
        Set(ByVal Value As String)
            If mExtraText <> Value Then
                mExtraText = Value
                OnChanged(Me, New EventArgs)
            End If
        End Set
    End Property

    Private mWidth As Integer = 100
    Property Width() As Integer
        Get
            Return mWidth
        End Get
        Set(ByVal Value As Integer)
            If mWidth <> Value Then
                mWidth = Value
                OnChanged(Me, New EventArgs)
            End If
        End Set
    End Property


    Public Event Changed(ByVal sender As Object, ByVal e As EventArgs)
    Protected Friend Sub OnChanged(ByVal sender As Object, ByVal e As EventArgs)
        RaiseEvent Changed(sender, e)
    End Sub

End Class
0
 
regisdanielAuthor Commented:
Ok Ronald! Thanks very much!!!

But, when I insert the control on a Form, everything works fine, but, when i reload the form, all colums have gone. What's wrong?

Thanks!!!
0
 
RonaldBiemansCommented:
Strange, I'll have a look ?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now