简体   繁体   English

如何从C#中的checkBoxList访问项目文本?

[英]How to access items text from a checkBoxList in C#?

I have the following code: 我有以下代码:

    string checkedItemListBoxPName;
    string checkedItemListBoxPID;
    int selectedProcessIndex;
    private void processes_checkedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        if (e.NewValue == CheckState.Checked)
        {
            for (int j = 0; j < processes_checkedListBox.Items.Count; ++j)
            {
                if (e.Index != j)
                {
                    processes_checkedListBox.SetItemChecked(j, false);
                }
            }

            selectedProcessIndex = processes_checkedListBox.SelectedIndex;
            MessageBox.Show(selectedProcessIndex.ToString());

            string checkedProcess = processes_checkedListBox.Items[selectedProcessIndex].ToString();
            string[] checkedProcessElements = checkedProcess.Split(new string[] { ".exe" }, StringSplitOptions.None);
            checkedItemListBoxPName = checkedProcessElements[0];
            checkedItemListBoxPID = checkedProcessElements[1].Trim();

            MessageBox.Show(checkedProcessElements[0].ToString());
            MessageBox.Show(checkedItemListBoxPID.ToString());
        }
    }

This method is called when I check a checkbox from my checkBoxList. 当我从我的checkBoxList中选中一个复选框时,将调用此方法。 In the beginning I am unchecking all the other previous checked boxes so that I will always have just one checked. 一开始,我将取消选中所有其他先前的复选框,以便始终只选中一个。

In selectedProcessIndex I store the index of the checked box so that I can access its text a little while after in string checkedProcess = processes_checkedListBox.Items[selectedProcessIndex].ToString(); selectedProcessIndex我存储了复选框的索引,以便可以在string checkedProcess = processes_checkedListBox.Items[selectedProcessIndex].ToString();之后string checkedProcess = processes_checkedListBox.Items[selectedProcessIndex].ToString();访问它的文本string checkedProcess = processes_checkedListBox.Items[selectedProcessIndex].ToString(); .

Everything works fine except the fact that when I check the very first box (index = 0) or the 33rd box, I receive System.IndexOutOfRangeException: Index was outside the bounds of the array. 一切正常,除了以下事实:当我选中第一个框(索引= 0)或第33个框时,我收到System.IndexOutOfRangeException: Index was outside the bounds of the array.

I don't know what is going on with those specific positions. 我不知道这些特定职位的情况。 Why 0 and 33 can't be handled. 为什么0和33无法处理。 I have 35 checkboxes total, from 0 to 34, as I said. 正如我所说的,我总共有35个复选框,从0到34。 The data from those positions is just like the other, it is visible, and has nothing special. 这些位置的数据就像其他位置的数据一样,是可见的,没有什么特别的。 Any suggestion is appreciated! 任何建议表示赞赏!

I think you get this error because when you check your elements on position 0 and 33 the method checkedProcess don't return a string containing the chars ".exe" . 我认为您收到此错误的原因是,当您检查位置0和33上的元素时,方法checkedProcess不会返回包含chars ".exe"的字符串。 The algorithm tries to split your string. 该算法尝试拆分您的字符串。 If there isn't found any ".exe" in it .split() will return an array with only one element on position zero. 如果未在其中找到任何".exe" ,则.split()将返回一个数组,该数组在位置0处仅包含一个元素。 In this case the new splitted array is the old unsplitted string). 在这种情况下,新的拆分数组是旧的未拆分字符串)。

checkedProcessElements[1].Trim(); tries to read a value outside the arrays range because its length is only 1. 尝试读取数组范围之外的值,因为其长度仅为1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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