为什么我listbox在删除选中项的时候会是未选中任何项?

我在进行listbox删除选中项的时候,明明选择了列表中的项,但是为什么Listbox1.SelectedIndex是-1,

img

有没有具体代码和界面啊?

这个是界面

img


```  private void button11_Click(object sender, EventArgs e)
        {
            if (Listbox1.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择要删除的图片");
            }
            else
            {
                //删除listbox中的文件名
                //Listbox1.Items.Remove(Listbox1.SelectedItem);

                List<object> list = new List<object>();
                foreach (int i in Listbox1.SelectedIndices)
                {
                    list.Add(Listbox1.Items[i]);
                }

                foreach (object i in list)
                {
                    Listbox1.Items.Remove(i);
                }

```这个是执行检测的部分

```        //执行检测
        int kongzhi = 0;//判断是否执行检测的参数
        public void button2_Click(object sender, EventArgs e)
        {

            kongzhi = 1;

        bool i = Jiance();
            string str1;
            str1 = str;
            

            if (i)
            {
                if (jiancemoshi)
                {
                    label1.Text=label3.Text ="1";
                    HObject img = new HObject();
                   
                    HOperatorSet.ReadImage(out img, imagestr);
                    img = Changesize(model, img);//按照模型对图像的要求修改图像尺寸
                    HTuple Width = new HTuple(), Height = new HTuple();
                    HOperatorSet.GetImageSize(img, out Width, out Height);
                    HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, Height - 1, Width - 1);//按照图像大小定义显示窗口imagepart尺寸
                    List<Jiance.Xinxi> list = ceshi.RunHalconEx(hWindowControl1.HalconWindow, hWindowControl2.HalconWindow, model, img);//输入图像和模型进行检测,返回缺陷位置信息

                    int length = list.Count;
                    int numRow = dataGridView1.RowCount;
                    for (int j = 0; j < numRow - 1; j++)
                    {
                        dataGridView1.Rows.Remove(dataGridView1.Rows[0]);
                    }
                    for (int j = 0; j < length; j++)
                    {
                        dataGridView1.Rows.Add();
                        dataGridView1.Rows[j].Cells[0].Value = j + 1;
                        int id = list[j].class_id;
                        if (id == 1)
                        {
                            dataGridView1.Rows[j].Cells[1].Value = "孔洞";//TODO(依据返回数定义缺陷类型)
                        }
                        dataGridView1.Rows[j].Cells[2].Value = "(" + list[j].r_c1 + "," + list[j].r_r1 + ")";//在列表中显示起点坐标
                        dataGridView1.Rows[j].Cells[3].Value = "(" + list[j].r_c2 + "," + list[j].r_r2 + ")";//在列表中显示终点坐标


                        ////////////////////////
                        dataGridView1.Rows[j].Cells[4].Value = str1;//在列表中显示分区结果

                        dataGridView1.Rows[j].Cells[5].Value = list[j].MaxDistance.D.ToString("0.0");

                    }; 
                    label4.Text = Listbox1.Items[0].ToString();
                }
                else
                {
                    DirectoryInfo dir = new DirectoryInfo(str);
                    int num2 = GetFilesCount(dir);
                    label3.Text = num2.ToString();
                    int num1 = m + 1;
                    label1.Text = num1.ToString();
                    HObject img = new HObject();
                    HTuple Width = new HTuple(), Height = new HTuple();
                    HOperatorSet.ListFiles(str, "files", out hv_file);

                    HOperatorSet.ReadImage(out img, hv_file[0]);//读取图像
                                                                             
                    img = Changesize(model, img);//按照模型对图像的要求修改图像尺寸
                    HOperatorSet.GetImageSize(img, out Width, out Height);
                    HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, Height - 1, Width - 1);//按照图像大小定义显示窗口imagepart尺寸
                    List<Jiance.Xinxi> list = ceshi.RunHalcon(hWindowControl1.HalconWindow, model, img);//输入图像和模型进行检测,返回缺陷位置信息
                    CurtXiaciList[m] = list;

                    int length = list.Count;
                    for (int j = 0; j < length; j++)
                    {
                        dataGridView1.Rows.Add();
                        dataGridView1.Rows[j].Cells[0].Value = j + 1;
                        int id = list[j].class_id;
                        if (id == 1)
                        {
                            dataGridView1.Rows[j].Cells[1].Value = "孔洞";//TODO(依据返回数定义缺陷类型)
                        }
                        dataGridView1.Rows[j].Cells[2].Value = "(" + list[j].r_c1 + "," + list[j].r_r1 + ")";//在列表中显示起点坐标
                        dataGridView1.Rows[j].Cells[3].Value = "(" + list[j].r_c2 + "," + list[j].r_r2 + ")";//在列表中显示终点坐标
                    }
                    button5.Enabled = true;
                    button4.Enabled = true;//开放前后按钮,可以对批处理图像进行

                    label4.Text = Listbox1.Items[0].ToString();
                }
                

            }
            

        }



```这个是列表切换的部分

```       private void Listbox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            label1.Text = Listbox1.SelectedIndex.ToString();
            HObject img = new HObject();

            if (kongzhi == 0)
            {
                //显示原图
                HOperatorSet.ReadImage(out img, folderBrowserDialog1.SelectedPath + "\\" + Listbox1.SelectedIndex.ToString());
                //img = Changesize(model, img);
                label4.Text = Listbox1.SelectedIndex.ToString();
                //HTuple Width = new HTuple(), Height = new HTuple();
                //HOperatorSet.GetImageSize(img, out Width, out Height);
                //HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, Height - 1, Width - 1);
                //List<Jiance.Xinxi> list = ceshi.RunHalcon(hWindowControl1.HalconWindow, model, img);
                //CurtXiaciList[m] = list;
                img.Dispose();
            }

            else
            {
                //显示处理后的图像,hv_file为检测后的目录和文件
                HOperatorSet.ReadImage(out img, hv_file[Listbox1.SelectedIndex]);

                img = Changesize(model, img);
                label4.Text = Listbox1.SelectedIndex.ToString();
                HTuple Width = new HTuple(), Height = new HTuple();
                HOperatorSet.GetImageSize(img, out Width, out Height);
                HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, Height - 1, Width - 1);