C# winform项目中ListView控件使用CheckBoxes属性实现单选功能

C# winform项目中ListView控件使用CheckBoxes属性实现单选功能

在做项目时需要使用ListView控件的CheckBoxes属性显示,还要在点击行时自动选中CheckBoxes和点击选中CheckBoxes时自动显示正行选中状态的单选功能。

效果图:


主要利用两个事件:listView1_ItemCheck和listView1_SelectedIndexChanged事件。

上代码:

        private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (!listView1.Items[e.Index].Checked)//如果点击的CheckBoxes没有选中
            {
                foreach (ListViewItem lv in listView1.Items)
                {
                    if (lv.Checked)//取消所有已选中的CheckBoxes
                    {
                        lv.Checked = false;
                        lv.Selected = false;
                       // lv.BackColor = Color.White;
                    }                    
                }
                listView1.Items[e.Index].Selected = true;
                // lv.Checked = false;
            }
            
            //int count = listView1.Items.Count;
            //ListViewItem item = listView1.Items[e.Index];
            //if (item.Checked)
            //{
            //    for (int i = 0; i < count; i++)
            //    {
            //        if (i != e.Index)
            //        {
            //            ListViewItem item1 = listView1.Items[i];
            //            item1.Checked = false;
            //        }
            //    }
            //}
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

            foreach (ListViewItem lv in listView1.Items)
            {

                if (lv.Selected)
                {
                    //if (lv.Checked)
                    //{
                    //    //lv.Checked = false;
                    //}
                    //else
                    //{
                        lv.Checked = true;
                    //}
                }
                else 
                {
                    if (listView1.SelectedIndices.Count>0)
                    {
                        if (lv.Checked)
                        {
                            lv.Checked = false;                           
                        }
                    }
                    
                }
            }

        }


另附源码Demo地址:http://download.csdn.net/detail/blacet/7342363


  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以通过以下步骤实现: 1. 添Listview控件WinForm界面。 2. 设置Listview控件的View属性为Details,并添所需的列。 3. 在代码,将需要上传的附件信息存储在一个List<Attachment>,其Attachment是一个自定义的类,包含附件的名称、路径等信息。 4. 将List<Attachment>的数据绑定到Listview控件上,每个附件对应一行数据。 5. 通过双击Listview控件的某一行,可以打开对应附件的查看界面。 6. 通过选Listview控件的某一行并点击删除按钮,可以删除对应附件。 以下是代码示例: ```csharp public partial class Form1 : Form { private List<Attachment> attachments = new List<Attachment>(); // 附件列表 public Form1() { InitializeComponent(); InitializeListView(); // 初始化Listview控件 LoadAttachments(); // 载附件列表 } private void InitializeListView() { listView1.View = View.Details; // 设置Listview控件的View属性为Details listView1.FullRowSelect = true; // 设置Listview控件全行选 listView1.Columns.Add("名称", 200); // 添名称列 listView1.Columns.Add("路径", 400); // 添路径列 } private void LoadAttachments() { // 从数据库或其他来源获取附件列表 // 并将每个附件添到attachments列表 // Attachment为自定义的类,包含附件的名称、路径等信息 // 将attachments列表的数据绑定到Listview控件上 foreach (Attachment attachment in attachments) { ListViewItem item = new ListViewItem(attachment.Name); item.SubItems.Add(attachment.Path); listView1.Items.Add(item); } } private void listView1_MouseDoubleClick(object sender, MouseEventArgs e) { if (listView1.SelectedItems.Count == 1) { // 获取选的附件的路径 string path = listView1.SelectedItems[0].SubItems[1].Text; // 打开附件的查看界面 Process.Start(path); } } private void btnDelete_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count == 1) { // 获取选的附件的名称和路径 string name = listView1.SelectedItems[0].SubItems[0].Text; string path = listView1.SelectedItems[0].SubItems[1].Text; // 从attachments列表删除选的附件 attachments.RemoveAll(a => a.Name == name && a.Path == path); // 从Listview控件删除选的附件 listView1.Items.Remove(listView1.SelectedItems[0]); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值