如何只检查c#中checkboxlist中的一个复选框? [英] how to check only one checkbox from checkboxlist in c#?

查看:52
本文介绍了如何只检查c#中checkboxlist中的一个复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3个项目的复选框列表...但我想一次只检查一个项目...如何在c#中执行此操作?

解决方案

参考: -

http://stackoverflow.com/questions/11687370/select-a-single-item-in-checkbox-list-using-c-sharp [ ^ ]

http://stackoverflow.com/questions/5256967/how-to- check-only-item-in-checkedlistbox [ ^ ]


使用checkedListBox1的SelectedIndexChanged事件。

请参阅以下代码。



  private   void  checkedListBox1_SelectedIndexChanged(  object  sender,EventArgs e)
{
int iSelectedIndex = checkedListBox1.SelectedIndex;
if (iSelectedIndex == -1)
return ;
for int iIndex = 0 ; iIndex < checkedListBox1.Items.Count; iIndex ++)
checkedListBox1.SetItemCheckState(iIndex,CheckState.Unchecked);
checkedListBox1.SetItemCheckState(iSelectedIndex,CheckState.Checked);
}


像这样使用循环





 for(int i = 0; i< chklist.items.count; i ++)> 
{
if(chkList.GetItemChecked(i)== true)
{
// true
}
}


i have a checkboxlist with 3 items...but i want to check only one item at a time...how to do this in c#?

解决方案

refer this:-
http://stackoverflow.com/questions/11687370/select-a-single-item-in-checkbox-list-using-c-sharp[^]
http://stackoverflow.com/questions/5256967/how-to-check-only-one-item-in-checkedlistbox[^]


Use SelectedIndexChanged event of checkedListBox1 .
Refer following code.

private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
       {
           int iSelectedIndex = checkedListBox1.SelectedIndex;
           if (iSelectedIndex == -1)
               return;
           for (int iIndex = 0; iIndex < checkedListBox1.Items.Count; iIndex++)
               checkedListBox1.SetItemCheckState(iIndex, CheckState.Unchecked);
           checkedListBox1.SetItemCheckState(iSelectedIndex, CheckState.Checked);
       }


use for loop like this


for(int i=0;i<chklist.items.count;i++)>
{
    if(chkList.GetItemChecked(i) == true)
    {
        //true
    }
}


这篇关于如何只检查c#中checkboxlist中的一个复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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