C# 如何从listview隐藏复选框

C# 如何从listview隐藏复选框,c#,winforms,user-interface,C#,Winforms,User Interface,我只需要隐藏指定行中的复选框。这有可能吗?这些行的标题是什么?如果是,请考虑在ListVIEW中使用。 编辑: 我不知道有什么方法可以只在listview项的子集上显示复选框。您的另一种选择是将您想要的项目的前景色设置为灰色,并且在ItemCheck事件中,如果选中这些项目,则将其checkstate重置为unchecked。您将需要实现一个所有者绘制的列表框。我有一个可以显示所有项目的图标和复选框,但是你可以很容易地修改它,只显示特定项目的复选框 public class IconizedCh

我只需要隐藏指定行中的复选框。这有可能吗?这些行的标题是什么?如果是,请考虑在ListVIEW中使用。

编辑:


我不知道有什么方法可以只在listview项的子集上显示复选框。您的另一种选择是将您想要的项目的前景色设置为灰色,并且在ItemCheck事件中,如果选中这些项目,则将其checkstate重置为unchecked。

您将需要实现一个所有者绘制的列表框。我有一个可以显示所有项目的图标和复选框,但是你可以很容易地修改它,只显示特定项目的复选框

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;
}
公共类图标化checkedlistbox:ListBox
{
公共图标化checkedlistbox()
:base()
{
DrawMode=DrawMode.OwnerDrawVariable;
双缓冲=真;
}
受保护的覆盖无效OnDrawItem(DrawItemEventArgs e)
{
图形g=e.图形;
g、 SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
使用(SolidBrush b=新的SolidBrush(背景色))
g、 圆角矩形(b,e.Bounds);
//e、 牵引杆接地();
//e、 DrawFocusRectangle();
如果(e.Index
没有行不是标题,它们只是不应选择的行。客户希望隐藏复选框OP要求的是“ListView”而不是“ListBox”。这不是一个有效的答案。