如何隐藏listview中的复选框

时间:2009-08-10 14:29:34

标签: c# winforms user-interface

我只需要隐藏指定行的复选框。这可能吗?如何?

2 个答案:

答案 0 :(得分:1)

那些行标题?如果是这样,请考虑在ListView中使用Groups

编辑:

我不知道有任何方法只显示listview项的子集上的复选框。您可以选择设置要灰色的项目的ForeColor,在ItemCheck事件中,如果选中这些项目的检查状态,则将其取消选中。

答案 1 :(得分:0)

您需要实现自己绘制的ListBox。我有一个显示所有项目的图标和复选框,但您可以轻松修改它以仅显示特定项目的复选框。

public class IconizedCheckedListBox : ListBox
{
    public IconizedCheckedListBox()
        : base()
    {
        DrawMode = DrawMode.OwnerDrawVariable;
        DoubleBuffered = true;
    }

    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        Graphics g = e.Graphics;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;


        using (SolidBrush b = new SolidBrush(BackColor))
            g.FillRectangle(b, e.Bounds);

        //e.DrawBackground();
        //e.DrawFocusRectangle();

        if (e.Index < Items.Count && e.Index != -1)
        {
            IconizedCheckedListBoxItem item = (IconizedCheckedListBoxItem)Items[e.Index];

            Rectangle checkBounds = e.Bounds;
            checkBounds.X += kCheckboxPadding;
            checkBounds.Y += (checkBounds.Height - kCheckboxSize) / 2;
            checkBounds.Width = kCheckboxSize;
            checkBounds.Height = kCheckboxSize;
            CheckBoxRenderer.DrawCheckBox(g, checkBounds.Location, 
                item.Checked?CheckBoxState.CheckedNormal:CheckBoxState.UncheckedNormal);

            using (SolidBrush b = new SolidBrush(ForeColor))
            {
                StringFormat format = new StringFormat();
                format.LineAlignment = StringAlignment.Center;
                format.Alignment = StringAlignment.Near;
                Rectangle textBounds = e.Bounds;
                textBounds.X += item.Icon.Width + 2*kCheckboxPadding + kCheckboxSize;
                textBounds.Y += 1;
                textBounds.Width -= item.Icon.Width;
                textBounds.Height -= 1;
                Font f;
                if (item.Checked)
                    f = new Font(Font, FontStyle.Bold);
                else
                    f = Font;
                g.DrawString(item.Data.ToString(), f, b, textBounds, format);
            }

            Image icon;
            if (!item.Checked)
                icon = Utilities.Utilities.WashOutImage(item.Icon);
            else
                icon = item.Icon;
            g.DrawImage(icon, e.Bounds.X + 2 * kCheckboxPadding + kCheckboxSize, e.Bounds.Y);
        }
    }

    protected override void OnMeasureItem(MeasureItemEventArgs e)
    {
        e.ItemHeight = kItemHeight;
    }

    protected override void  OnMouseClick(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            int idx = IndexFromPoint(e.Location);
            if (idx != -1)
            {
                IconizedCheckedListBoxItem item = (IconizedCheckedListBoxItem)Items[idx];
                item.Checked = !item.Checked;

                if (ItemCheck != null)
                {
                    ItemCheckEventArgs args = new ItemCheckEventArgs(
                        idx,
                        item.Checked ? CheckState.Checked : CheckState.Unchecked,
                        CheckState.Indeterminate);
                    ItemCheck(this, args);
                }
                Invalidate();
            }
        }
        base.OnMouseClick(e);
    }

    /// <summary>
    /// This is called AFTER the check state has been updated
    /// </summary>
    public event ItemCheckEventHandler ItemCheck;

    private const int kItemHeight = 32;
    private const int kCheckboxSize = 13;
    private const int kCheckboxPadding = 4;
}

public class IconizedCheckedListBoxItem
{
    public IconizedCheckedListBoxItem(Image inIcon, object inData)
    {
        Icon = inIcon;
        Data = inData;
        Checked = false;
    }

    public override string ToString()
    {
        return Data.ToString();
    }

    public Image Icon;
    public object Data;
    public bool Checked;
}