简体   繁体   English

取消 ListBox SelectedIndexChange 事件

[英]Cancelling ListBox SelectedIndexChange Event

Is it possible to cancel the SelectedIndexChange event for a listbox on a winforms application?是否可以取消 winforms 应用程序上列表框的 SelectedIndexChange 事件? This seems like such a logical thing to have that I must be overlooking some easy feature.这似乎是一件合乎逻辑的事情,以至于我必须忽略一些简单的功能。 Basically, I have been popping up a message box asking if the user really wants to move to another item, as this will change the UI and I don't want their changes to be lost.基本上,我一直在弹出一个消息框,询问用户是否真的想要移动到另一个项目,因为这会改变 UI,我不希望他们的更改丢失。 I'd like to be able to cancel the event in case the user has not saved what they are working on.如果用户没有保存他们正在处理的内容,我希望能够取消该事件。 Is there a better way of doing this?有没有更好的方法来做到这一点?

You cannot cancel it.你不能取消它。

What I did just a couple of days ago was to have a variable with the latest selected index.几天前我所做的是使用最新选择的索引创建一个变量。 Then when the event fires, you ask the user if he wants to save, this is done in the eventhandler.然后当事件触发时,您询问用户是否要保存,这是在事件处理程序中完成的。 If the user selected "Cancel" you change the id again.如果用户选择了“取消”,则您再次更改 ID。

The problem is that this will make the event fire once again.问题是这会使事件再次触发。 So what i've used is a bool just saying "Inhibit".所以我使用的是一个布尔值,只是说“禁止”。 And at the top of the eventhandler I have:在事件处理程序的顶部,我有:

if(Inhibit)
   return;

Then below this where you ask the question you do something like this:然后在下面你提出问题的地方你做这样的事情:

DialogResult result = MessageBox.Show("yadadadad", yadada cancel etc);
if(result == DialogResult.Cancel){
   Inhibit = true; //Make sure that the event does not fire again
   list.SelectedIndex = LastSelectedIndex; //your variable
   Inhibit = false; //Enable the event again
}
LastSelectedIndex = list.SelectedIndex; // Save latest index.

This is exactly @Oskar Kjellin 's method, but with a twist.这正是@Oskar Kjellin 的方法,但有所不同。 That is, one variable less and with a selected index changed event that really behaves like a selected index changed event.也就是说,一个变量少了一个选定的索引更改事件,它的行为实际上就像一个选定的索引更改事件。 I often wonder why is selected index changed event getting fired even if I click on the exact same selected item.我经常想知道为什么即使我单击完全相同的选定项目,选择的索引更改事件也会被触发。 Here it doesn't.这里没有。 Yes it's a deviation, so be doubly sure if you want this to be there.是的,这是一个偏差,所以要加倍确定你是否希望它在那里。

    int _selIndex = -1;
    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listBox1.SelectedIndex == _selIndex)
            return;

        if (MessageBox.Show("") == DialogResult.Cancel)
        {
            listBox1.SelectedIndex = _selIndex;
            return;
        }

        _selIndex = listBox1.SelectedIndex;
        // and the remaining part of the code, what needs to happen when selected index changed happens
    }

I just ran into this exact problem.我刚刚遇到了这个确切的问题。 What I did is when the user makes changes, I set ListBox.Enabled = false;我所做的是当用户进行更改时,我设置 ListBox.Enabled = false; This disallows them to select a different index.这不允许他们选择不同的索引。 Once they either save or discard their changes, I set ListBox.Enabled = true;一旦他们保存或放弃他们的更改,我设置 ListBox.Enabled = true; Probably not as slick as a prompt, but it gets the job done.可能不像提示那样圆滑,但它可以完成工作。

More elegant, use the Tag property:更优雅,使用 Tag 属性:

        if ((int)comboBox.Tag == comboBox.SelectedIndex)
        {
            // Do Nothing
        }
        else
        {
            if (MessageBox.Show("") == DialogResult.Cancel)
            {
                comboBox.SelectedIndex = (int)comboBox.Tag;
            }
            else
            {
                // Reset the Tag
                comboBox.Tag = comboBox.SelectedIndex;

                // Do what you have to
            }
        }

The SelectedIndexChanged cannot be cancelled. SelectedIndexChanged 无法取消。 So you only have one real option:所以你只有一个真正的选择:

private int? currentIndex;
public void ListBox_SelectedIndexChanged(sender, EventArgs args) {
    if (currentIndex.HasValue && currentIndex.Value != listBox1.SelectedIndex) {
        var res = MessageBox.Show("Do you want to cancel edits?", "Cancel Edits", MessageBoxButtons.YesNo);
        if (res == DialogResult.Yes) {
            currentIndex = (listBox1.SelectedIndex == -1 ? null : (int?) listBox1.SelectedIndex);
        } else {
            listBox1.SelectedIndex = currentIndex.Value;
        }
    }
}

This is my way to cancel SelectionChange for ComboBox.这是我为 ComboBox 取消 SelectionChange 的方法。 I think it could also fit to ListBox.我认为它也可以适合 ListBox。

private bool comboBox_CancelSelection = false;
private int comboBox_LastSelectedIndex = -1;

private void comboBox_SelectedIndexChanged(object sender, EventArgs e) {
    if (comboBox_CancelSelection) {
        comboBox_CancelSelection = false;
        return ;
    }

    // Handle Event

    if (!comoBox_CancelSelection) {
        comboBox_LastSelectedIndex = comboBox.SelectedIndex; 
    } else {
        comboBox.SelectedIndex = comboBox_LastSelectedIndex;
    }
}

暂无
暂无

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

相关问题 DataList SelectedIndexChange事件未触发 - DataList SelectedIndexChange event not firing SelectedIndexChange事件处理程序代码 - SelectedIndexChange event handler code 刷新列表中的数据时,是否必须从listBox中删除selectedIndexChange事件? - Do I have to remove a selectedIndexChange event from a listBox when refreshing the data in it? 如何检查ComboBox.selectedIndexChange事件是否为null - How to check ComboBox.selectedIndexChange event is null or not 在选定的indexchange事件触发后填充控件 - Populate control after selectedindexchange event has fired 通过反射使用时,不会触发SelectedIndexChange事件 - SelectedIndexChange event not firing when using through reflection Dropdown selectedIndexChange Event正在调用Radiobutton selectedIndexChange事件。两个控件都在转发器内 - Dropdown selectedIndexChange Event is calling Radiobutton selectedIndexChange event.Both controls are inside repeater C#:在SelectedIndexChange事件处理程序中设置ComboBox文本值? - C#: Setting ComboBox text value in SelectedIndexChange event handler? GridView无法从UpdatePanel中的DropDownList的SelectedIndexChange事件更新 - GridView not updating from SelectedIndexChange event of a DropDownList within an UpdatePanel 无法将下拉列表 selectedindexchange 中的列表附加到以后在 buttonclick 事件中使用 - Cannot append the list in dropdown selectedindexchange to later use in buttonclick event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM