简体   繁体   中英

CheckedListBox.SelectedItems.Count = 1?

In C#, I have checked list boxes, that I need to store the data in arrays, but when I start the event that writes the objects to the array, I have to set the size of the array, which I naturally set to the amount of items checked. However, the items checked, for both checked list boxes I have is 1, no matter how many I check. Can someone help?

public partial class Form3 : Form
{
    public static object[] dtype;
    public static bool loaded = false;
    bool typeselecte = false;
    bool typeselectd = false;
    public Form3()
    {
        InitializeComponent();
    }

    private void Form3_Shown(object sender, EventArgs e)
    {
        if (loaded)
        {
            int counte = 0;
            int countd = 0;
            types1.Items.AddRange(dtype);
            types2.Items.AddRange(dtype);

            if (typeselecte)
            {
                for (int i = 0; i < types1.Items.Count; i++)
                {
                    if (i == Form1.enumber[counte])
                    {
                        types1.SelectedItems[i] = Form1.esearch[i];
                        counte++;
                    }
                }
            }

            if (typeselectd)
            {
                for (int j = 0; j < types2.Items.Count; j++)
                {
                    if (j == Form1.dnumber[countd])
                    {
                        types2.SelectedItems[j] = Form1.dsearch[j];
                        countd++;
                    }
                }
            }
        }
    }

    public void dtypes()
    {
        dtype = new object[types1.Items.Count];
        for (int i = 0; i < types1.Items.Count; i++)
        {
            dtype[i] = types1.Items[i];
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (types1.SelectedItems.Count > 0)
            typeselecte = true;

        if (types2.SelectedItems.Count > 0)
            typeselectd = true;

        Form1.esearch = new object[types1.SelectedItems.Count];
        Form1.dsearch = new object[types2.SelectedItems.Count];
        Form1.enumber = new int[types1.SelectedItems.Count];
        Form1.dnumber = new int[types2.SelectedItems.Count];

        int counte = 0;
        int countd = 0;

        if (typeselecte)
        {
            for (int i = 0; i < types1.SelectedItems.Count; i++)
                Form1.esearch[i] = types1.SelectedItems[i];
        }

        if (typeselectd)
        {
            for (int j = 0; j < types2.SelectedItems.Count; j++)
                Form1.dsearch[j] = types2.SelectedItems[j];
        }

        if (typeselecte)
        {
            for (int k = 0; k < types1.Items.Count; k++)
            {
                if (Form1.esearch[k] == types1.Items[k])
                {
                    Form1.enumber[counte] = k;
                    counte++;
                }
                else
                {
                    k--;
                }
            }
        }

        if (typeselectd)
        {
            for (int l = 0; l < types2.Items.Count; l++)
            {
                if (Form1.dsearch[l] == types2.Items[l])
                {
                    Form1.dnumber[countd] = l;
                    countd++;
                }
                else
                {
                    l--;
                }
            }
        }
        this.Close();
    }
}

Form1.esearch and dsearch are object arrays, which the size hasn't been picked yet, and e and dnumber are int arrays that have unknown size as well, I just didn't feel the need to put in that code.

我相信您需要使用CheckedItems属性而不是SelectedItems属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM