tags:

views:

223

answers:

2

Hi ,

I'm having a windows form which contains listview control , where listView1.View = View.Details; and listView1.CheckBoxes = true;

then added a column with HeaderName as "FileName".

listView1.Columns.Add("File Name", 200, HorizontalAlignment.Left);

Here I would like to have check box in the Header of listview , ie FileName.

Can anyone help me with this.

Thanks in advance. andy

+1  A: 

A ListView header with a checkbox is not part of the standard ListView functionality. You'll need to customise the renderering to do this:

    listview.OwnerDraw = true


    private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
    {
        // Draw your custom checkbox control here
    }

    private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
    {
        e.DrawDefault = true;
    }

    private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
    {
        e.DrawDefault = true;
    }

You'll also have to add some click handlers for the header and manage the state of your checkboxes yourself.

Matt Breckon
Thanks a Tom Matt
Andy
@Andy: If this was the answer to your question, would you consider marking as the answer?
Zach Johnson
A: 

Hey I did it! Thanks Matt!

'Sorry guys I still love Visual Basic... assuming LV here is a List View control

Private Sub LV_DrawColumnHeader(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewColumnHeaderEventArgs) Handles LV.DrawColumnHeader
    If e.ColumnIndex = 0 Then
        Dim cck As New CheckBox With {.Text = "", .Visible = True}
        LV.SuspendLayout() : e.DrawBackground()
        cck.BackColor = Color.Transparent
        cck.UseVisualStyleBackColor = True
        cck.BackgroundImage = My.Resources.CheckboxBackground
        cck.SetBounds(e.Bounds.X, e.Bounds.Y, _
          cck.GetPreferredSize(New Size(e.Bounds.Width, e.Bounds.Height)).Width, _
          cck.GetPreferredSize(New Size(e.Bounds.Width, e.Bounds.Height)).Width)
        cck.Size = New Size(cck.GetPreferredSize(New Size(e.Bounds.Width - 1, e.Bounds.Height)).Width + 1, e.Bounds.Height)
        cck.Location = New Point(3, 0)
        LV.Controls.Add(cck)
        cck.Show()
        cck.BringToFront()
        e.DrawText(TextFormatFlags.VerticalCenter Or TextFormatFlags.Left)
        LV.ResumeLayout(True)
    Else
        e.DrawDefault = True
    End If
End Sub

Private Sub LV_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewItemEventArgs) Handles LV.DrawItem
    e.DrawDefault = True
End Sub

Private Sub LV_DrawSubItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewSubItemEventArgs) Handles LV.DrawSubItem
    e.DrawDefault = True
End Sub

In my code above, I just want to draw a checkbox at first column header... That is, e.ColumnIndex = 0. In order to make the checkbox respond properly to user's click, we need to add handler with this piece of code.

'this line should be added before LV.ResumeLayout()
AddHandler cck.CheckedChanged, AddressOf theCheckboxInHeader_CheckChanged

'then add this procedure elsewhere in the file.
Private Sub theCheckboxInHeader_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    'Enter code here
End Sub

Due to stackoverflow.com policy, new users (such as me) are forbidden to post images... so I cannot post my screenshot here. http://i.imgur.com/tfiFl.png

Btw... does stackoverflow.com code highlighting javascript only supports C#?

Ryan
Hi Ryan Im very keen to know how you had the listview group name on the same line of text , I mean "AlphaMart---------------" and "IndoMart--------------------", as in my case using visual studio 2008 it comes beneath the text not on the same line . could you please advise on this.
Andy