How do I include an event handler to allow only one checkbox in a listview?

What event handler code would I include with the below C# code that would only allow one check box at a time to be checked in the listview?  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace ToDoListWM5_Live
{
    public partial class DeleteToDoItemsView : UserControl
    {
        private ToDoListDS.ToDoListDataTable viewData;

        private ToDoListDS.ToDoListDataTable viewItemsToDelete;

        //public event ItemCheckEventHandler ItemCheck;
       
        public DeleteToDoItemsView()
        {
            InitializeComponent();
            viewItemsToDelete = new ToDoListDS.ToDoListDataTable();
        }

        public ToDoListDS.ToDoListDataTable ViewItemsToDelete
        {
            get
            {
                viewItemsToDelete.Clear();
                foreach (ListViewItem item in this.listView1.Items)
                {
                    if (item.Checked)
                        viewItemsToDelete.AddToDoListRow(item.Text);
                }
                return viewItemsToDelete;
            }
        }



        public ToDoListDS.ToDoListDataTable ViewData
        {
            set
            {
                viewData = value;
                this.listView1.Items.Clear();
                foreach (ToDoListDS.ToDoListRow row in viewData.Rows)
                {
                    this.listView1.Items.Add(new ListViewItem(row.ToDoListItem));
                }
            }
        }
 
    }
}
NSingAsked:
Who is Participating?
 
lazyberezovskyCommented:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace ToDoListWM5_Live
{
    public partial class DeleteToDoItemsView : UserControl
    {
        private ToDoListDS.ToDoListDataTable viewData;
        private ToDoListDS.ToDoListDataTable viewItemsToDelete;
        private bool isItemChecking;

        //public event ItemCheckEventHandler ItemCheck;
        
        public DeleteToDoItemsView()
        {
            InitializeComponent();
            viewItemsToDelete = new ToDoListDS.ToDoListDataTable();
            listView1.ItemCheck += listView1_ItemCheck;
        }


        private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (isItemChecking)
                return;

            isItemChecking = true;

            if (e.NewValue == CheckState.Checked)
            {
                foreach (ListViewItem item in listView1.Items)
                {
                    if (item.Index == e.Index)
                        continue;

                    item.Checked = false;
                }

                listView1.Items[e.Index].Checked = true;                
            }

            isItemChecking = false;
        }

        public ToDoListDS.ToDoListDataTable ViewItemsToDelete
        {
            get
            {
                viewItemsToDelete.Clear();
                foreach (ListViewItem item in this.listView1.Items)
                {
                    if (item.Checked)
                        viewItemsToDelete.AddToDoListRow(item.Text);
                }
                return viewItemsToDelete;
            }
        }

        public ToDoListDS.ToDoListDataTable ViewData
        {
            set
            {
                viewData = value;
                this.listView1.Items.Clear();
                foreach (ToDoListDS.ToDoListRow row in viewData.Rows)
                {
                    this.listView1.Items.Add(new ListViewItem(row.ToDoListItem));
                }
            }
        }  
    }
}

Open in new window

0
 
NSingAuthor Commented:
Thanks!!!  Everything is working perfectly now!
0
 
NSingAuthor Commented:
The solution was very accurate and it was received in a very timely manner.  Excellent job!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.