有关checklistbox点击事件的问题

时间:2014-07-18 07:59:56

标签: c# checklistbox

enter image description here

此代码有效但在我检查时会显示结果,然后在第一个复选框中再次取消选中。我只想检查一次。有人有意见吗?谢谢!

private void clb_grup_MouseClick(object sender, MouseEventArgs e)
        {

           Liste.Items.Clear();

            string s = "";


            foreach (var item in clb_kisiler.CheckedItems)
            {
                s = s + item.ToString() + " ";
            }
            string[] secilenler = s.Split(' ');


            if (clb_grup.GetItemChecked(0) == true)
            {
                for (int x = 0; x < clb_kisiler.Items.Count; x++)
                {
                    clb_kisiler.SetItemChecked(x, true);

                }

                for (int i = 0; i < clb_kisiler.Items.Count; i++)
                {
                    Liste.Items.Add(clb_kisiler.Items[i].ToString());
                }
            }
            else if (clb_grup.GetItemChecked(1) == true)
            {
                for (int x = 0; x < clb_idari.Items.Count; x++)
                {
                    clb_idari.SetItemChecked(x, true);
                }


                for (int i = 0; i < clb_idari.Items.Count; i++)
                {

                    Liste.Items.Add(clb_idari.Items[i].ToString());
                }
            }

            else if (clb_grup.GetItemChecked(2) == true)
            {
                for (int x = 0; x < clb_teknik.Items.Count; x++)
                {
                    clb_teknik.SetItemChecked(x, true);
                }

                for (int i = 0; i < clb_teknik.Items.Count; i++)
                {
                    Liste.Items.Add(clb_teknik.Items[i].ToString());
                }
            }



            foreach (object i in clb_kisiler.CheckedItems)
            {
                Liste.Items.Add(i.ToString());
            }


        }

新代码

我的朋友使用了字符串数组而不是组合框。她也遇到了和我一样的问题,她还尝试编写一个代码来阻止列表框中显示出公开项目。她有一个检查列表盒子和我一样,她的第二个清单是空的,还有一个列表框。这是代码。

private void chklstbx_bolum_ItemCheck(object sender,ItemCheckEventArgs e)         { //第一个检查列表框的事件

       string[] tumu = { "Jane", "Tammy", "John", "Emily", "Susan", "Julie", "Amelia",        "Katherine" };
        string[] idari = { "Julie", "Amelia", "Katherine" };
        string[] teknik = { "Jane", "Tammy", "John", "Emily", "Susan" };

       if (chklstbx_bolum.GetItemChecked(0) == true)
       {
       //if the first box in the first check list box is checked then do this

           //this part of the code works fine but it doesnt check if there are             //duplicates of the same name in the list box


            chklstbx_sonuc.Items.Clear();

                for (int i = 0; i < 5;i++ )
                {
                    chklstbx_sonuc.Items.Add(teknik[i]);
                    chklstbx_sonuc.SetItemChecked(i, true);
                    lstbx_sonuc.Items.Add(teknik[i]);
                }
        }
         else if (chklstbx_bolum.GetItemChecked(1) == true){
       //do this if the second box in the first check box list is checked

          //Here the program checks to see if there are duplicates when adding from the second check list box but the program just freezes.

            chklstbx_sonuc.Items.Clear();


            int x=0;

            do
            {
                for (int i = 0; i < 3; i++)
                {
                    chklstbx_sonuc.Items.Add(idari[i]);
                    chklstbx_sonuc.SetItemChecked(i, true);
                    lstbx_sonuc.Items.Add(idari[i]);
                }

            } while ( x== 0);
            x++;



            for (int t = 0; t < lstbx_sonuc.Items.Count; t++)
            {
                for (int s = 0; s < chklstbx_sonuc.Items.Count; s++)
                {
                    if (chklstbx_sonuc.Items[s].ToString() != lstbx_sonuc.Items[t].ToString())

                        lstbx_sonuc.Items.Add(chklstbx_sonuc.Items[s]);

                }
            }
        }

}

所以现在第二个问题是为什么第二个if语句会产生问题并使程序无法响应?我们仍然想知道如何在不必检查和取消选中的情况下传输字符串数组中的项目,但在检查时直接传输?

1 个答案:

答案 0 :(得分:2)

点击事件不正确。您应该使用check事件,然后在事件参数中验证是否已选中或取消选中该复选框。

修改

我已经读过你的其他问题了。有一个比手动检查已经检查过的更简单的解决方案。

您需要为每个复选框指定一个值,并使用您要传输的列表的ID。然后,您可以检查代码中已检查的值(可能通过发件人),并使用FindControl(checkboxvalue)查找要传输的列表。然后你首先转移所有项目,然后检查它们。正如我上面提到的,如果当前选中或取消选中该复选框,您还需要在checkedeventargs(或者所检查事件的eventargs参数)中进行验证。