简体   繁体   English

在ListView上启用/禁用上移/下移按钮

[英]Enabling/Disabling Moveup/MoveDown buttons on a ListView

with a form like this: 具有这样的形式:

I wrote this piece of code to take care of enable/disable logic for moveup/down buttons when they click on at item ( we don't care about Avaiable list on the left, we just care about Selected list on the right) 我编写这段代码是为了在单击项目时照顾上移/下移按钮的启用/禁用逻辑(我们不在乎左侧的“可用列表”,我们只在意右侧的“选定”列表)

在此处输入图片说明

   private void SelectedLV_SelectedIndexChanged(object sender, EventArgs e)
    {
        // what to do wth move up button
        if (SelectedLV.SelectedIndices.Count == 1 && SelectedLV.SelectedItems[0].Index > 0)
        {
            MoveUpBtn.Enabled = true;
        }
        else
        {
            MoveUpBtn.Enabled = false;
        }

        //what to do with move down button
        if (SelectedLV.SelectedIndices.Count == 1 && SelectedLV.SelectedItems[0].Index < SelectedLV.Items.Count - 1)
        {
            MoveDownBtn.Enabled = true;
        }
        else
        {
            MoveDownBtn.Enabled = false;
        }
    }

I think it works fine for that scenario but my question is what about when we click off of Selected Listview, What is good logic to handle that and Disable Both Moveup/Down buttons? 我认为在这种情况下可以正常工作, 但是我的问题是,当我们单击“选定的列表视图”时,如何处理该问题以及“禁用两个上移/下移”按钮是什么? I don't want them be enabled when we are not inside SelectedListView... Also if you notice any issue with the code I pasted please let me know. 当我们不在SelectedListView中时,我不希望启用它们。另外,如果您发现我粘贴的代码有任何问题,请告诉我。 Thanks 谢谢

The problem is once you click on the Move buttons, then you are outside of the SelectedListView control, so the logic should really be based on if you have a correct index value or not: 问题是,一旦单击“移动”按钮,那么您就位于SelectedListView控件之外,因此逻辑应该实际上基于是否具有正确的索引值:

private void SelectedLV_SelectedIndexChanged(object sender, EventArgs e)
  if (SelectedLV.SelectedIndicies.Count == 0) {
    MoveUpBtn.Enabled = false;
    MoveDownBtn.Enabled = false;
  } else {
    // normal processing
  }

You are about to shoot your foot with the focus requirement. 您将着眼于聚焦要求。 These kind of UI updates are best done with the Application.Idle event, it only runs when nothing important is happening. 此类UI更新最好通过Application.Idle事件完成,它仅在没有重要事件发生时运行。 And can help to eliminate a lot of event handlers. 并且可以帮助消除很多事件处理程序。 Like this: 像这样:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        Application.Idle += Application_Idle;
        this.FormClosed += delegate { Application.Idle -= Application_Idle; };
    }

    void Application_Idle(object sender, EventArgs e) {
        bool focusOk = this.ActiveControl == SelectedLV;
        bool selectOk = SelectedLV.SelectedIndices.Count == 1;
        int index = selectOk ? SelectedLV.SelectedIndices[0] : -1;
        MoveUpBtn.Enabled = focusOk && selectOk && index > 0;
        MoveDownBtn.Enabled = focusOk && selectOk && index < SelectedLV.Items.Count-1;
    }
}

Don't forget to set the focus back in the buttons' Click event handler. 不要忘记将焦点重新设置在按钮的Click事件处理程序中。 And don't forget about the ListView.HideSelection property. 并且不要忘记ListView.HideSelection属性。 Set it to False so that focus doesn't matter anymore. 将其设置为False,这样焦点就不再重要了。

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

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