【问题标题】:SelectedIndexChanged event to occur in button click按钮单击时发生的 SelectedIndexChanged 事件
【发布时间】:2013-08-10 19:41:35
【问题描述】:

我有一个删除列表框中选定项目的命令。执行删除命令后如何更改列表框其他项的选择?

【问题讨论】:

  • 什么?不是一个明确的问题。
  • SelectedIndex 设置为您想要的任何索引,或者SelectedItemSelectedValue..

标签: c# winforms selectedindexchanged


【解决方案1】:

在按钮命令下,写这个,它会删除列表视图中的选中项。

listBox1.Items.Remove(listBox1.SelectedItem);

【讨论】:

    【解决方案2】:

    其实你想说的并不完整,但我得到的是你想删除ListBox中当前选中的项目;你可以这样做;

    int index=0; //Set its value corresponding to item you want to delete.
    listBox1.Items.RemoveAt(index); //This will remove selected item.
    

    现在,如果您希望在删除此项目后立即选择下一个项目,请添加此项目;

    if(index!=-1)
    {
       int nextindex=index+1; //Calculates the index of next item.
       listBox1.SelectedIndex=nextindex; //Selects next item.
    }
    

    【讨论】:

      【解决方案3】:
      private void button1_Click(object sender, EventArgs e) {
        int i = listBox1.SelectedIndex;
        if(i > -1) {
           listBox1.Items.RemoveAt(i);
           if(listBox1.Items.Count > 0)
             listBox1.SelectedIndex = i < listBox1.Items.Count ? i : listBox1.Items.Count - 1;
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-03-29
        • 2017-10-03
        • 2023-04-08
        • 2012-03-14
        • 1970-01-01
        • 2011-01-28
        • 2016-03-14
        • 2013-07-03
        • 1970-01-01
        相关资源
        最近更新 更多