melek_3
melek_3

Reputation: 127

having problems with checklistbox click events

enter image description here

This code works but the results show up when I check and then uncheck again in the first checkbox. I want to check only once.Does anyone have an opinion? Thanks!

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());
            }


        }

NEW CODE

My friend used string arrays instead of the combo boxes.She is also having the same problem as me,and she also tried to write a code that would prevent dublicate items being showed up in the list box.She has one checked list box the same as me,her second checked list box is empty,and one list box.Heres the code.

private void chklstbx_bolum_ItemCheck(object sender, ItemCheckEventArgs e) { //event for the first check list box

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

                }
            }
        }

}

So now the second question is why does the second else if statement create a problem and makes the program not respond? And we still want to know how are we able to transfer the items in the string array without having to check and uncheck but directly transfer when its checked?

Upvotes: 1

Views: 143

Answers (1)

Nzall
Nzall

Reputation: 3555

The click event is not the right one. you should use the check event and then verify in the event arguments if the checkbox has been checked or unchecked.

EDIT

I've read your other problem. There is an easier solution than manually checking which has been checked.

You need to assign a value to each checkbox with the ID of the list you want to transfer. You can then check in your code which value has been checked (likely through the sender) and use FindControl(checkboxvalue) to find the list that you want to transfer. Then you first transfer all the items, and then you check them. As I mentioned above, you also need to verify in your checkedeventargs (or whatever the eventargs parameter of the checked event is) if the checkbox is currently checked or unchecked.

Upvotes: 2

Related Questions