How can I prevent the firing of multiple events of the same kind triggered by a single action?
如何防止由单个操作触发的同一类型的多个事件的触发?
For example, I have a ListView
containing some items. When I select or deselect all items, the SelectedIndexChanged
event is fired once for each item. Rather, I would like to receive a single event indication the user's action (selection/deselection of items), regardless of the number of items.
例如,我有一个包含一些项目的ListView。当我选择或取消选择所有项目时,会为每个项目触发一次SelectedIndexChanged事件。相反,我想收到一个事件指示用户的动作(选择/取消选择项目),而不管项目的数量。
Is there any way to achieve this?
有没有办法实现这个目标?
3 个解决方案
#1
You can't change the ListView
code, and subclassing it doesn't provide many options.
您无法更改ListView代码,并且子类化它不提供许多选项。
I would suggest that you simply add a small delay (200ms or similar) to your code - i.e. you only do the calculation a little while after the last update. Something like:
我建议您只需在代码中添加一个小延迟(200ms或类似) - 即您只在上次更新后稍微进行计算。就像是:
using System;
using System.Windows.Forms;
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
ListView list;
TextBox txt;
Timer tmr = new Timer();
tmr.Interval = 200;
Form form = new Form {
Controls = {
(txt = new TextBox { Dock = DockStyle.Fill, Multiline = true}),
(list = new ListView { Dock = DockStyle.Right, View = View.List,
Items = { "abc", "def" , "ghi", "jkl", "mno" , "pqr"}})
}
};
list.SelectedIndexChanged += delegate {
tmr.Stop();
tmr.Start();
};
tmr.Tick += delegate {
tmr.Stop();
txt.Text += "do work on " + list.SelectedItems.Count + " items"
+ Environment.NewLine;
};
Application.Run(form);
}
}
#2
Only by by coming at the problem from a slightly different direction. E.g. subscribe loss of focus.
只是从一个稍微不同的方向来解决问题。例如。订阅失去焦点。
In the end, the application or runtime cannot raise an event on "all selection changes done" without actually using something else because there is no way for the application to predict whether the user will perform another click on the control while it retains focus.
最后,应用程序或运行时无法在“完成所有选择更改”的情况下引发事件而不实际使用其他内容,因为应用程序无法预测用户是否会在保留焦点时对控件执行另一次单击。
Even using focus, the user could switch back to that control.
即使使用焦点,用户也可以切换回该控件。
#3
If your ListView
is in virtual mode, you could use the VirtualItemsSelectionRangeChanged
event. This event will be fired only once for the user's action (selection/deseclection).
如果ListView处于虚拟模式,则可以使用VirtualItemsSelectionRangeChanged事件。对于用户的操作(选择/解除选择),此事件仅被触发一次。
#1
You can't change the ListView
code, and subclassing it doesn't provide many options.
您无法更改ListView代码,并且子类化它不提供许多选项。
I would suggest that you simply add a small delay (200ms or similar) to your code - i.e. you only do the calculation a little while after the last update. Something like:
我建议您只需在代码中添加一个小延迟(200ms或类似) - 即您只在上次更新后稍微进行计算。就像是:
using System;
using System.Windows.Forms;
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
ListView list;
TextBox txt;
Timer tmr = new Timer();
tmr.Interval = 200;
Form form = new Form {
Controls = {
(txt = new TextBox { Dock = DockStyle.Fill, Multiline = true}),
(list = new ListView { Dock = DockStyle.Right, View = View.List,
Items = { "abc", "def" , "ghi", "jkl", "mno" , "pqr"}})
}
};
list.SelectedIndexChanged += delegate {
tmr.Stop();
tmr.Start();
};
tmr.Tick += delegate {
tmr.Stop();
txt.Text += "do work on " + list.SelectedItems.Count + " items"
+ Environment.NewLine;
};
Application.Run(form);
}
}
#2
Only by by coming at the problem from a slightly different direction. E.g. subscribe loss of focus.
只是从一个稍微不同的方向来解决问题。例如。订阅失去焦点。
In the end, the application or runtime cannot raise an event on "all selection changes done" without actually using something else because there is no way for the application to predict whether the user will perform another click on the control while it retains focus.
最后,应用程序或运行时无法在“完成所有选择更改”的情况下引发事件而不实际使用其他内容,因为应用程序无法预测用户是否会在保留焦点时对控件执行另一次单击。
Even using focus, the user could switch back to that control.
即使使用焦点,用户也可以切换回该控件。
#3
If your ListView
is in virtual mode, you could use the VirtualItemsSelectionRangeChanged
event. This event will be fired only once for the user's action (selection/deseclection).
如果ListView处于虚拟模式,则可以使用VirtualItemsSelectionRangeChanged事件。对于用户的操作(选择/解除选择),此事件仅被触发一次。