在C#Windforms中的清单框控件中切换复选框 [英] Toggle Check boxes in checkedlistbox control in c# windforms

查看:47
本文介绍了在C#Windforms中的清单框控件中切换复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个清单框控件
1. chklst_type
2.chklst_test


chklst_type我通过数据库填充.
在检查来自chklst_type的项目时,我填写了第二个清单框控件,即(chklst_test)

我想取消选中chklst_type中最后一个选中的项目,还希望从第二个CheckedListBox控件(chklst_test)中清除相关项目.
我要切换它.

意味着一次只应从第一个控件中选中一个复选框,其相关项目显示在第二个控件中.

这是我的代码...




I have two checkedlistbox controls
1. chklst_type
2.chklst_test


chklst_type I fill through database.
On Checking of an item from chklst_type, I fill second checkedlistbox control i.e.(chklst_test)

I want to uncheck the last checked item from chklst_type and also clear the related items from the second checkedlistbox control (chklst_test).
I want to toggle it.

Means at a time only one checkbox should be checked from first control and its related items are shown in the second control.

Here is my code...




private void chklst_type_ItemCheck(object sender, ItemCheckEventArgs e)
{
            try
            {
                int j;
                for (j = 0; j <= (chklst_type.Items.Count - 1); j++)
                {
                    if (chklst_type.GetItemCheckState(j)==CheckState.Unchecked)
<code><pre>

{
id = Convert.ToInt32(chklst_type.SelectedIndex);
id = id + 1;
sqlQry =从LabTest中选择Lab_Test,其中Type_Id =""+ id +"'';
adapter = new SqlDataAdapter(sqlQry,Connection.sqlconn);
ds = new DataSet();
adapter.Fill(ds);
for(int i = 0; i< ds.Tables [0] .Rows.Count; i ++)
{
chklst_test.Items.Add(ds.Tables [0] .Rows [i] ["Lab_Test"].ToString());
}
}
}
}
catch(ex ex例外)
{
Connection.m_MessageLog.Log("frmRecLabTest.cs",57,02636,``U'',``E'',0,"chklst_type_ItemCheck:" + ex.ToString());
}
}

{
id = Convert.ToInt32(chklst_type.SelectedIndex);
id = id + 1;
sqlQry = "select Lab_Test from LabTest where Type_Id=''" + id + "''";
adapter = new SqlDataAdapter(sqlQry, Connection.sqlconn);
ds = new DataSet();
adapter.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
chklst_test.Items.Add(ds.Tables[0].Rows[i]["Lab_Test"].ToString());
}
}
}
}
catch (Exception ex)
{
Connection.m_MessageLog.Log("frmRecLabTest.cs", 57, 02636, ''U'', ''E'', 0, "chklst_type_ItemCheck:" + ex.ToString());
}
}

推荐答案

假定chklst_type_ItemCheck是复选框列表的处理程序,为什么要尝试重新加载列表框?

Assuming that chklst_type_ItemCheck is a handler for the checklistbox, why are you trying to reload the listbox?

sqlQry = "select Lab_Test from LabTest where Type_Id='" + id + "'";
                        adapter = new SqlDataAdapter(sqlQry, Connection.sqlconn);
                        ds = new DataSet();
                        chklst_test.Items.Clear();
                        adapter.Fill(ds);
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            chklst_test.Items.Add(ds.Tables[0].Rows[i]["Lab_Test"].ToString());
                        }



您需要做的就是取消选中除已选中项目以外的所有项目.您不需要重新加载任何内容.



All you need to is uncheck all items except the one that was checked. You don''t need to reload anything.


这篇关于在C#Windforms中的清单框控件中切换复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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