【问题标题】:having problems with checklistbox click events复选框点击事件有问题
【发布时间】:2014-07-18 07:59:56
【问题描述】:

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

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

                }
            }
        }

}

那么现在第二个问题是为什么第二个 else if 语句会产生问题并使程序没有响应?而且我们仍然想知道我们如何能够转移字符串数组中的项目而不需要检查和取消检查,而是在检查时直接转移?

【问题讨论】:

  • 退后一步。从 1 个可选复选框、1 个列表和没有检查开始,并使其工作。然后添加另一个复选框和另一个列表,并确保其工作正常。开发的一个传统问题是开发人员咬得比他能咀嚼的多。记住:开发一个软件产品就像吃一头大象:你不能把它整个吞下去,所以你需要把它切成小块,一次一个地吃。

标签: c# checklistbox


【解决方案1】:

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

编辑

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

您需要为每个复选框分配一个值,其中包含您要传输的列表的 ID。然后,您可以签入您的代码(可能通过发件人)检查了哪个值,并使用FindControl(checkboxvalue) 查找您要传输的列表。然后,您首先转移所有项目,然后检查它们。正如我上面提到的,如果复选框当前被选中或未选中,您还需要在您的 checkedeventargs (或任何选中事件的 eventargs 参数)中验证。

【讨论】:

  • 我使用了 ItemCheck 事件,但我遇到了同样的问题:\
  • 我重读了您的问题。告诉我这是否正确:您想知道第一个组合框中的 3 个项目中的哪一个被选中,并根据您在第一个组合框中的选择从其他 3 个复选框中的一个中移动所有项目?
  • 我朋友用字符串数组代替了额外的选中列表框,我现在就写她的代码
猜你喜欢
  • 1970-01-01
  • 2021-08-08
  • 1970-01-01
  • 1970-01-01
  • 2014-10-10
  • 1970-01-01
  • 1970-01-01
  • 2014-01-16
  • 1970-01-01
相关资源
最近更新 更多