선택한 항목의 수를 MaxSelection으로 제한하려는 텍스트 상자가 있습니다. 원하는 동작은 MaxSelection 항목을 선택하면 더 큰 선택 항목이 무시된다는 것입니다. (따라서이 질문은 "limit selections in a listbox in vb.net"과 다릅니다).ListBox에서 X 개 이상의 항목을 선택한 경우 이전 선택으로 되돌립니다.
이 작업을 시도하는 목록 상자의 SelectedIndexChanged 이벤트에 대한 이벤트 처리기가 있습니다. 사용자가 Ctrl + 클릭을 사용하여 (MaxSelection + 1) 번째 항목을 선택하면 이전 선택 항목으로 되돌아갑니다.
사용자가 항목을 선택한 다음 목록에서 MaxSelection + 1 항목 아래의 항목을 Shift 키를 누른 채로 클릭 할 때 문제가 발생합니다. 이 경우 하나 이상의 SelectedIndexChanged 이벤트가 발생합니다. 하나는 Shift 키를 누른 상태에서 클릭 한 항목을 선택하는 항목이고, 다른 하나는 원래 항목과 Shift 키를 누른 상태로 선택한 항목 사이에있는 항목을 모두 선택하는 항목입니다. 이러한 이벤트 중 첫 번째 이벤트는 사용자가 Shift 키를 누른 항목 (기술적으로는 정확함)을 선택할 수있게 한 다음 두 번째 이벤트는 첫 번째 이벤트 이후의 선택 항목으로 되돌립니다 (원래 선택한 항목과 Shift 클릭 된 항목). 원하는 것은 코드가 첫 번째 이벤트 (원래 선택한 항목 만) 이전에 선택 항목을 선택 항목으로 되돌릴 수 있다는 것입니다.
Shift 키를 누르기 전에 선택 사항을 유지할 수있는 방법이 있습니까? 일부 제 3 자 구성 요소는 BeforeSelectedIndexChanged 같은 취소 할 이벤트가
void ChildSelectionChanged(object sender, EventArgs e)
{
ListBox listBox = sender as ListBox;
//If the number of selected items is greater than the number the user is allowed to select
if ((this.MaxSelection != null) && (listBox.SelectedItems.Count > this.MaxSelection))
{
//Prevent this method from running while reverting the selection
listBox.SelectedIndexChanged -= ChildSelectionChanged;
//Revert the selection to the previous selection
try
{
for (int index = 0; index < listBox.Items.Count; index++)
{
if (listBox.SelectedIndices.Contains(index) && !this.previousSelection.Contains(index))
{
listBox.SetSelected(index, false);
}
}
}
finally
{
//Re-enable this method as an event handler for the selection change event
listBox.SelectedIndexChanged += ChildSelectionChanged;
}
}
else
{
//Store the current selection
this.previousSelection.Clear();
foreach (int selectedIndex in listBox.SelectedIndices)
{
this.previousSelection.Add(selectedIndex);
}
//Let any interested code know the selection has changed.
//(We do not do this in the case where the selection would put
//the selected count above max since we revert the selection;
//there is no net effect in that case.)
RaiseSelectionChangedEvent();
}
}
불행히도 SelectedValueChanged 이벤트 후에 MouseDown 및 KeyDown이 실행되고 있음을 발견했습니다. 그러나 곧 내가 게시 할 MouseUp을 사용하는 솔루션에 영감을 얻었습니다. 정말 고마워. –
나는 그것을 테스트하지는 않았지만 전에 해고해야한다고 생각했다. 죄송합니다. 해결 방법을 찾았다는 소식을 듣게되어 기쁩니다. 그러나 "최악"의 경우에는 ListBox에서 상속 된 자체 컨트롤을 만들고 WndProc 메서드를 재정 의하여 Windows 메시지 처리에 연결하십시오. – Lucero