Home > Blockchain >  Get and concatenate all the selected item in CheckedBoxComboBox
Get and concatenate all the selected item in CheckedBoxComboBox

Time:11-16

I'm having a problem when I select more than 1 in my CheckedBoxComboBox.

I can do this using a CheckListBox using this code.

objNPTBEL.workdayidlist = null;
for (int x = 0; x < checkedListBox1.CheckedItems.Count; x  )
{
     if (x < checkedListBox1.CheckedItems.Count - 1)
         objNPTBEL.workdayidlist  = checkedListBox1.GetItemText(checkedListBox1.CheckedItems[x])   ",";
     else
         objNPTBEL.workdayidlist  = checkedListBox1.GetItemText(checkedListBox1.CheckedItems[x]);
}

Ex. Output, "415073,415072"

But I want a ComboBox UI. I found this custom tool CheckBoxComboBox.

CheckBox ComboBox Extending the ComboBox Class and Its Items

And this is my current code to get all the selected items in CheckedBoxComboBox.

int x = 0;
objNPTBEL.workdayidlist = null;
for (int i = 0; i < checkBoxComboBox1.CheckBoxItems.Count; i  )
{
     if (checkBoxComboBox1.CheckBoxItems[i].CheckState == CheckState.Checked)
     {
         x = x   1;
         for (int j = 0; j < x; j  )
         {
              if (j < x - 1)
              {
                  objNPTBEL.workdayidlist  = checkBoxComboBox1.GetItemText(checkBoxComboBox1.CheckBoxItems[i].Text)   ",";
              }
              else
              {
                  objNPTBEL.workdayidlist  = checkBoxComboBox1.GetItemText(checkBoxComboBox1.CheckBoxItems[i].Text);
              }
          }
      }
}

When I select more than 1 item my output looks like this.

ex. Output 415073415072,415072

CodePudding user response:

Don't get too complex, just add the comma every time then trim it off at the end..

I can't work out why there is an inner for loop, but I've left it alone because you know your project better than me. It will cause duplicate items in the output but you haven't indicated that that is a problem - I'm only solving the problem that the numbers are run together without a comma. If you don't want duplicate items too, you can remove the inner for loop also - basically just do it like you had first time but with an if that looks to see if the item is checked

     for (int j = 0; j < x; j  )
     {
         objNPTBEL.workdayidlist  = checkBoxComboBox1.GetItemText(checkBoxComboBox1.CheckBoxItems[i].Text)   ","; 
     }

...

objNPTBEL.workdayidlist = objNPTBEL.workdayidlist.TrimEnd(',');

Probably worth pointing out that it may be more useful to make objNPTBEL.workdayidlist something like a List<int> rather than a csv

  • Related