简体   繁体   English

MultiSelect ListBox选择和取消选择事件

[英]MultiSelect ListBox Select And DeSelect Event

I have a ListBox in winforms Application, now the business logic demands me to fire one function if an item in the List Box is Selected and fire another if an item in the List Box is Deselected. 我在winforms应用程序中有一个ListBox,现在的业务逻辑要求我如果选择了列表框中的一项,则触发一个函数,如果取消选择列表框中的项,则触发另一个函数。

But the way I see it none of the Events Listed in VS is giving that power of Execution. 但是从我的角度来看,VS中列出的所有事件都没有赋予执行权。 I do see Events like 我确实看到类似的活动

SelectedIndexChanged(object sender, EventArgs e)

and

SelectedValueChanged(object sender, EventArgs e)

But both these event fires if there is a change in the selection of the ListBox. 但是,如果列表框的选择发生更改,则会触发这两个事件。 But it doesn't specify if an item was selected or deselected which raised the event. 但是它没有指定引发事件的项目是选中还是取消选中。

Any suggestion on this would be very helpful. 关于此的任何建议将非常有帮助。

I even found the following link on MSDN 我什至在MSDN上找到了以下链接

https://msdn.microsoft.com/en-us/library/system.windows.controls.listboxitem.unselected%28v=vs.110%29.aspx https://msdn.microsoft.com/en-us/library/system.windows.controls.listboxitem.unselected%28v=vs.110%29.aspx

But I am not sure how to apply the same in this situation. 但是我不确定在这种情况下如何应用相同的方法。

Posting the Tedious Solution. 发布繁琐的解决方案。 Might help some1 in the future to Copy Paste. 将来可能会帮助some1复制粘贴。

public static int ListCount;

        private void listBoxPackService_SelectedIndexChanged(object sender, EventArgs e)
        {
            int CurrCount;

            ListBox.SelectedObjectCollection col = listBoxPackService.SelectedItems;
            CurrCount = col.Count;

            if (CurrCount > ListCount)
            {
                //Item Selected

            }
            else
            {
                //Item DeSelected

               if(CurrCount == 0)
               {
                  //All Items Were Deselected
               }

            }

            ListCount = CurrCount;

        }

On load of the ListBox 加载ListBox时

ListCount = 0

rename the Controls as per your requirement. 根据您的要求重命名控件。

I am still open for a better solution :) 我仍然愿意寻求更好的解决方案:)

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

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