WinForm ListBox 中的 MouseDown 杀死 SelectedIndexChanged
我正在编写一些代码来检测打开 MultiSelect 的 WindForms ListBox 中的选择切换。由于 SelectedIndexChanged 只能让我看到单击后选择的内容,因此我一直在寻找一种方法来检测单击 ListBox 之前选择的内容。我实现了 MouseDown 事件,并且可以得到我想要的结果,但不幸的副作用是我杀死了 SelectedIndexChanged 事件。它不会着火。
这是已知的行为吗?对于在点击之前进入选择列表有什么想法吗?
谢谢。
编辑以包含请求的代码片段。
设计器生成的事件:
this.lbPhysicianClinic.SelectedIndexChanged += new System.EventHandler( this.lbPhysicianClinic_SelectedIndexChanged );
this.lbPhysicianClinic.MouseDown += new System.Windows.Forms.MouseEventHandler( this.lbPhysicianClinic_MouseDown );
显示 MouseDown 事件的代码片段:
private void lbPhysicianClinic_MouseDown( object sender, MouseEventArgs e )
{
List<Clinic_List_ByPhysicianResult> Selected = this.PhysicianGetSelectedClinics( this.lbPhysicianClinic.SelectedIndices );
}
显示 SelectedIndexChanged 事件的代码片段:
private void lbPhysicianClinic_SelectedIndexChanged( object sender, EventArgs e )
{
try
{
if ( this.FormInitComplete && this.RefreshUIComplete )
{
List<Clinic_List_ByPhysicianResult> Selected = this.PhysicianGetSelectedClinics( this.lbPhysicianClinic.SelectedIndices );
Clinic_List_ByPhysicianResult DroppedClinic = new Clinic_List_ByPhysicianResult();
我在每个事件中设置了一个断点,如果存在 MouseDown 事件,则 SelectedIndexChanged 事件永远不会触发。它仅在 MouseDown 事件消失时触发。
希望这能澄清一些事情。
I'm writing some code to detect toggling of selections in a WindForms ListBox with MultiSelect turned on. Since SelectedIndexChanged only lets me see what is selected after the click, I was looking for a way to detect what was selected before the ListBox was clicked. I implemented the MouseDown event and I can get exactly what I want, but an unfortunate side effect is that I have killed the SelectedIndexChanged event. It will not fire.
Is this known behavior? Are there any thoughts about getting to the selection list before the click?
Thanks.
Edited to include code snippets as requested.
Designer generated events:
this.lbPhysicianClinic.SelectedIndexChanged += new System.EventHandler( this.lbPhysicianClinic_SelectedIndexChanged );
this.lbPhysicianClinic.MouseDown += new System.Windows.Forms.MouseEventHandler( this.lbPhysicianClinic_MouseDown );
Code snippet showing MouseDown event:
private void lbPhysicianClinic_MouseDown( object sender, MouseEventArgs e )
{
List<Clinic_List_ByPhysicianResult> Selected = this.PhysicianGetSelectedClinics( this.lbPhysicianClinic.SelectedIndices );
}
Code snippet showing SelectedIndexChanged event:
private void lbPhysicianClinic_SelectedIndexChanged( object sender, EventArgs e )
{
try
{
if ( this.FormInitComplete && this.RefreshUIComplete )
{
List<Clinic_List_ByPhysicianResult> Selected = this.PhysicianGetSelectedClinics( this.lbPhysicianClinic.SelectedIndices );
Clinic_List_ByPhysicianResult DroppedClinic = new Clinic_List_ByPhysicianResult();
I set a breakpoint in each event and if the MouseDown event is there, the SelectedIndexChanged event never fires. It only fires when the MouseDown event is gone.
Hopefully this clarifies things.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ListBox 在引发 MouseDown 或 SelectedIndexChanged 事件之前更改其选择。
您需要做的是捕获底层 Win32 消息并自行引发事件。您可以子类化 ListBox 来执行此操作。
您可以使用 MyListBox 类,并为 PreSelect 事件添加一个处理程序,如下所示:
在事件处理程序内,您可以在列表框更改所选索引之前访问它们。
The ListBox changes its selection before it raises the MouseDown or SelectedIndexChanged events.
What you need to do is capture the underlying Win32 message and raise an event yourself. You can subclass ListBox to do this.
You can use the MyListBox class, and add a handler for the PreSelect event like so:
Inside the event handler you can access the selected indices before the listbox has changed them.