标记复选框列表中的一个复选框问题 [英] mark one checkbox from checklistbox problem

查看:231
本文介绍了标记复选框列表中的一个复选框问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我正在使用清单框.
我只希望一次从复选框中标记一个项目,如果用户选择另一个复选框,则前一个复选框将被取消标记.

hello to all,
i m working on checklistbox.
i want only to mark one item at a time from checklistbox and if user select another checkbox ,the previous checkbox get unmarked.
how could i do that.

推荐答案

您可以使用RadioButtonList.

但是,如果您仍要继续使用复选框,请参见
You can use a RadioButtonList.

However, if you still want to continue with a check box, see http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/5333cdf2-a669-467c-99ae-1530e91da43a[^].


在变量中保留当前选中项目的痕迹.

在SelectedIndexChanged处理程序中,重置当前选中的项目,然后设置新的项目.

Keep a trace of the currently checked item in a variable.

In the SelectedIndexChanged handler, reset the currently checked item, and set the new one.

if (SelectedIndex >= 0)
{
    checkedListBox1.SetItemChecked(SelectedIndex, false);
}
SelectedIndex = checkedListBox1.SelectedIndex;
checkedListBox1.SetItemChecked(SelectedIndex, true);


在表单加载时将复选框的CheckOnClick属性设置为True.
使用SelectedIndexChanged事件,如下所示:

Set the checklistbox''s CheckOnClick attribute as True on form load.
Use the SelectedIndexChanged event like so:

private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
   if (checkedListBox1.GetItemChecked(checkedListBox1.SelectedIndex))
   {
      foreach (int item in checkedListBox1.CheckedIndices)
      {
         if(item != checkedListBox1.SelectedIndex)
            checkedListBox1.SetItemChecked(item, false);
      }
   }
}


这篇关于标记复选框列表中的一个复选框问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆