Solved

Adding a Property to the ListView's ColumnHeader

Posted on 2004-09-23
8
230 Views
Last Modified: 2010-04-23
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
Comment
Question by:regisdaniel
  • 3
  • 2
8 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12140854
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
 

Author Comment

by:regisdaniel
ID: 12142337
Ok. But i want be able change these properties on the form editor.
How  can i do this?
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 500 total points
ID: 12143928
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
 

Author Comment

by:regisdaniel
ID: 12157185
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12158283
Strange, I'll have a look ?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now