listview标题复选框

时间:2009-11-22 12:39:18

标签: c# listview

我有一个包含listview控件的窗体, 其中listView1.View = View.Details;listView1.CheckBoxes = true;

然后添加一个HeaderName为“FileName”的列。

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

这里我想在列表视图的标题中有复选框,即FileName。

任何人都可以帮助我。

提前致谢。 安迪

4 个答案:

答案 0 :(得分:9)

带复选框的ListView标头不是标准ListView功能的一部分。您需要自定义渲染以执行此操作:

    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;
    }

您还必须为标题添加一些点击处理程序,并自行管理复选框的状态。

答案 1 :(得分:6)

这是我在C#中的答案,它也解释了点击事件以及来自VB.NET的C#事件处理

private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) 
{
    if ((e.ColumnIndex == 0))
    {
        CheckBox cck = new CheckBox();
        // With...
        Text = "";
        Visible = true;
        listView1.SuspendLayout();
        e.DrawBackground();
        cck.BackColor = Color.Transparent;
        cck.UseVisualStyleBackColor = true;

        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);
        listView1.Controls.Add(cck);
        cck.Show();
        cck.BringToFront();
        e.DrawText((TextFormatFlags.VerticalCenter | TextFormatFlags.Left));
        cck.Click += new EventHandler(Bink);
        listView1.ResumeLayout(true);
    }
    else
    {
        e.DrawDefault = true;
    }
}

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

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

private void Bink(object sender, System.EventArgs e)
{
    CheckBox test = sender as CheckBox;

    for (int i=0;i < listView1.Items.Count; i++)
    {     
        listView1.Items[i].Checked = test.Checked;
    }
}

答案 2 :(得分:2)

嘿,我做到了!谢谢马特!

'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

在上面的代码中,我只想在第一列标题处绘制一个复选框...即,e.ColumnIndex = 0。 为了使复选框能够正确响应用户的点击,我们需要使用这段代码添加处理程序。

'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

由于stackoverflow.com政策,新用户(例如我)被禁止发布图片...因此我无法在此处发布我的屏幕截图。 http://i.stack.imgur.com/tfiFl.png

顺便说一下... stackoverflow.com代码突出显示javascript只支持C#?

答案 3 :(得分:1)

这比上述书面解决方案更有效率,

public partial class Form1 : Form
{
bool clicked = false;
CheckBoxState state;
public Form1()
{
        InitializeComponent();
        listView1.View = View.Details;
        listView1.Columns.Add("Col1", 150);
        listView1.Columns.Add("Col2", 150);
        listView1.Columns.Add("Col3", 150);
        listView1.Columns.Add("Col4", 150);
        listView1.HeaderStyle = ColumnHeaderStyle.Clickable;
        listView1.CheckBoxes = true;
        listView1.OwnerDraw = true;

        for(int i = 0; i < 10; i++)
            listView1.Items.Add("Value " + i);
    }

    private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
    {
        if (!clicked)
        {
            clicked = true;
            state    = CheckBoxState.CheckedPressed;

            foreach (ListViewItem item in listView1.Items)
            {
                item.Checked = true;
            }

            Invalidate();
        }
        else
        {
            clicked = false;
            state = CheckBoxState.UncheckedNormal;
            Invalidate();

            foreach (ListViewItem item in listView1.Items)
            {
                item.Checked = false;
            }
        }           
    }

    private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
    {
        TextFormatFlags flags = TextFormatFlags.LeftAndRightPadding;
        e.DrawBackground();
        CheckBoxRenderer.DrawCheckBox(e.Graphics, ClientRectangle.Location, state);
        e.DrawText(flags);
    }

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

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