简体   繁体   中英

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. 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:

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. 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. And don't forget about the ListView.HideSelection property. Set it to False so that focus doesn't matter anymore.

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