Start New Discussion within our Software Development Community

Hello. I have a six locations checkboxlist and i'd like to create an array with the checked locations like this:

int[] v=new int[6];
private checkboxlist1_changed.....(...)
{
   for(int i=0;i<6;i++)
{
       if(checkboxlist1.getitemchecked(i))
          v[i]=1;
       else
          v[i]=0;
}
}
string w=v.Tostring();

and it seems it's always a step behind, cant figure it why it doesnt working;
i also tried with string, stringbuilder,etc. what should it be? the getitemchecked method?

I did an example code for you. I hope it helps:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string[] array = new string[] { "one", "two", "three", "four", "five", "six" };
            checkedListBox1.Items.AddRange(array);
            label1.Text = "No checkBoxes checked...";
        }

        private void ShowResults(int[] array)
        {
            label1.Text = "Results of the checkBoxes selection:\n\n";
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] == 1)
                    label1.Text += "checkBox " + (i + 1).ToString() + " is checked" + Environment.NewLine;
                else
                    label1.Text += "checkedBox " + (i + 1).ToString() + " is NOT checked" + Environment.NewLine;
            }
        }
        int[] ticks = new int[6]; 
        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
        {                   
            int index = e.Index;
            for (int i = 0; i < ticks.Length; i++)
            {
                if (i == index)
                {
                    if (e.NewValue == CheckState.Checked)
                        ticks[index] = 1;
                    else
                        ticks[index] = 0;
                }
            }
            ShowResults(ticks);
        }
    }
This article has been dead for over six months. Start a new discussion instead.