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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.