How can I make only one checkbox checked in a listview without allowing the others to be checked

I am very new to VS and have never programmed in C#.  Here is the code that I am using and need to edit it so that only one checkbox at a time can be selected so that only the  selected item will be deleted from my list.  I cannot figure out how to include this in the event handler.   I know it is simple.  Please help!

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.

käµfm³d 👽Commented:
Handle the ItemChecked event of the ListView. Inside the handler, keep track of the item that was just checked in a class-level variable. We do this because when we programmatically check a ListViewItem, the event fires again, until a stack overflow occurs. Once we set this variable, we can check inside the handler whether or not the original event is being handled or one of the events that is fired because of the loop. See notes below:
private ListViewItem checkingItem;

private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
{
    // the first time the event fires, checkingItem should be null;
    //   If so, we set it to the item being checked
    if (this.checkingItem == null)
    {
        this.checkingItem = e.Item;
    }
    // If the event was fired by some ListViewItem from the loop below
    //   we need to just return control to the original handler call
    else
    {
        return;
    }

    // Set each item in the ListView to unchecked
    foreach (ListViewItem item in this.listView1.Items)
    {
        item.Checked = false;
    }

    // Set the item we want checked to checked
    e.Item.Checked = true;

    // Reset the checkingItem to null for a future event firing
    this.checkingItem = null;
}

Open in new window

NSingAuthor Commented:
Thanks for the suggestion.  I think there is still something that I am missing.  Is there some other type of declaration that should be made within the code?  When I have tried to build my solutions using the following, I get an error message:  

private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)

I get the following error message:

Error      1      The type or namespace name 'ItemCheckedEventArgs' could not be found (are you missing a using directive or an assembly reference?)
käµfm³d 👽Commented:
As long as you have a using statement for System.Windows.Forms, then you should be good. You can try prefixing the type like

    private void listView1_ItemChecked(object sender, System.Windows.Forms.ItemCheckedEventArgs e)

but I don't expect that message to go away even if you do. If it does, fantastic. If it does not, make sure you don't have other errors near that line. Sometimes VS is finicky and generates incorrect errors--but not often.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

käµfm³d 👽Commented:
If the above does not work, would it be possible to post the entire class definition?
NSingAuthor Commented:
Thank you kaufmed.  I tried that approach as well, but still get the same error.  The only way that it will get past that everror is if that line is if ItemCheckedEventArgs is changed to ItemCheckEventArgs, which is not what I want.  If is done this way then the error is that there is "no definition for Item".  Below is the entire code for this class.  I'm sure that I am missing something.  Thanks for your help.

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));
                }
            }
        }

        private ListViewItem checkingItem;

        private void listView1_ItemChecked(object sender, System.Windows.Forms.ItemCheckedEventArgs e)
        {
            // the first time the event fires, checkingItem should be null;
            //   If so, we set it to the item being checked
            if (this.checkingItem == null)
            {
                this.checkingItem = e.Item;
            }
            // If the event was fired by some ListViewItem from the loop below
            //   we need to just return control to the original handler call
            else
            {
                return;
            }

            // Set each item in the ListView to unchecked
            foreach (ListViewItem item in this.listView1.Items)
            {
                item.Checked = false;
            }

            // Set the item we want checked to checked
            e.Item.Checked = true;

            // Reset the checkingItem to null for a future event firing
            this.checkingItem = null;
        }

    }
}
käµfm³d 👽Commented:
What version of .NET and/or Visual Studio are you using?
NSingAuthor Commented:
I'm using VS 2005 and .NET 2.0
NSingAuthor Commented:
I had to take a different approach which is actually working now.  Thanks again for your help.

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;
        }

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
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.