[英]C# Disable Ctrl key on a ListBox
我想禁用ListBox上的Ctrl鍵。 我按照D&D中的禁用列表框CTRL + C&CTRL + X的示例進行操作。 因為我正在using System.Windows.Input
和using System.Windows.Forms
,所以花了一些時間。 我能夠解析引用,除了e
對象。 它告訴我System.Windows.Forms.PreviewKeyDownEventArgs
沒有Handled
定義。 我在這里想念什么? 謝謝!
private void lbSigns_PreviewKeyDown(object sender,
System.Windows.Forms.PreviewKeyDownEventArgs e)
{
//
// This if statement detects if the Control Key is pressed.
//
if ((System.Windows.Input.Keyboard.Modifiers &
System.Windows.Input.ModifierKeys.Control) ==
System.Windows.Input.ModifierKeys.Control)
{
e.Handled = true;
}
}
好的,我與@Tsukasa的代碼越來越接近。 這就是我現在所擁有的。
List<int> alreadySelectedIndexes = new List<int>();
private void lbSigns_SelectedIndexChanged(object sender, EventArgs e)
{
TrackSelection((ListBox)sender, alreadySelectedIndexes);
bool allowSelection = false;
int currentSelectedIndex = -1;
//make sure we have an item selected
if (!lbSigns.SelectedIndex.Equals(-1))
{
//if first selection we allow it
if (alreadySelectedIndexes.Count.Equals(1))
{
allowSelection = true;
}
else
{
//get the last item index that was selected from our list
currentSelectedIndex = alreadySelectedIndexes[alreadySelectedIndexes.Count - 1];
//make sure we have a previous index item
if ((currentSelectedIndex - 1) >= 0)
{
//check if previous item before currently selected is checked
if (lbSigns.GetSelected(currentSelectedIndex - 1))
{
allowSelection = true;
}
}
//make sure we have a next index item
if ((currentSelectedIndex + 1) <= lbSigns.Items.Count - 1)
{
//check if next item after currently selected is checked
if (lbSigns.GetSelected(currentSelectedIndex + 1))
{
allowSelection = true;
}
}
//make sure we have both a next and a previous item
if (((currentSelectedIndex - 1) >= 0) && ((currentSelectedIndex + 1) <= lbSigns.Items.Count - 1))
{
//if both are selected, deny the selection
if (lbSigns.GetSelected(currentSelectedIndex - 1) && lbSigns.GetSelected(currentSelectedIndex + 1))
{
allowSelection = false;
}
}
}
}
//unselect item because it wasn't before or after an already selected item
if (!allowSelection && !currentSelectedIndex.Equals(-1))
{
lbSigns.SetSelected(currentSelectedIndex, false);
}
}
private void TrackSelection(ListBox listBox, List<int> alreadySelectedList)
{
ListBox.SelectedIndexCollection indexCollection = listBox.SelectedIndices;
foreach (int index in indexCollection)
{
if (!alreadySelectedList.Contains(index))
{
alreadySelectedList.Add(index);
}
}
foreach (int index in new List<int>(alreadySelectedList))
{
if (!indexCollection.Contains(index))
{
alreadySelectedList.Remove(index);
}
}
}
但是,這讓我取消選擇了2個選定項目周圍的項目。 取消選擇后,即使它已在任一側選擇了項目,也不允許我重新選擇該項目。 我在注釋后面的部分中添加了: //make sure we have both a next and a previous item
更新2
好的,這不允許在選定的項目之間取消選擇,盡管還有另一個問題。
問題:您可以按住鼠標按鈕,向左按住Ctrl +鼠標並上下拖動。 直到完成,才調用該事件。 我不確定要嘗試糾正哪個事件。
不過,我確實有更好的方式可以稍后發布。 我將在左右控制鍵上都創建一個全局鍵盤鈎。 如果檢測到該鍵時觸發。 如果ListBox具有輸入焦點,則我們將處理該鍵,從而僅使Shift按鈕能夠用於選擇多個項目。 您要確保僅當輸入焦點位於ListBox上時,否則它將成為所有其他應用程序的鍵,因為它是全局鈎子。
//hold already selected items. Last item will be last selected
List<int> alreadySelectedIndexes = new List<int>();
//used to skip listBox1_SelectedIndexChanged on TrackSelection
bool ignoreSelectedChanged = false;
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//prevent overflow of caused by TrackSelection
if (!ignoreSelectedChanged)
{
TrackSelection((ListBox)sender, alreadySelectedIndexes);
bool allowSelection = false;
int currentSelectedIndex = -1;
//make sure we have an item selected
if (!listBox1.SelectedIndex.Equals(-1))
{
//if first selection we allow it
if (alreadySelectedIndexes.Count.Equals(1))
{
allowSelection = true;
}
else
{
//get the last item index that was selected from our list
if ((alreadySelectedIndexes.Count - 1) >= 0)
{
currentSelectedIndex = alreadySelectedIndexes[alreadySelectedIndexes.Count - 1];
//make sure we stay in array range
if ((currentSelectedIndex - 1) >= 0)
{
//check if previous item before currently selected is checked
if (listBox1.GetSelected(currentSelectedIndex - 1))
{
allowSelection = true;
}
}
//make sure we stay in array range
if ((currentSelectedIndex + 1) <= listBox1.Items.Count - 1)
{
//check if next item after currently selected is checked
if (listBox1.GetSelected(currentSelectedIndex + 1))
{
allowSelection = true;
}
}
}
}
bool isSelected = false;
if (currentSelectedIndex >= 0)
{
isSelected = listBox1.GetSelected(currentSelectedIndex);
if (!isSelected)
{
//we can remove it from the list now
alreadySelectedIndexes.Remove(currentSelectedIndex);
//reselect it if in the middle of already selected items
if (alreadySelectedIndexes.Contains(currentSelectedIndex + 1) && alreadySelectedIndexes.Contains(currentSelectedIndex - 1))
{
ignoreSelectedChanged = true;
allowSelection = true;
listBox1.SetSelected(currentSelectedIndex, true);
}
}
}
}
if (!currentSelectedIndex.Equals(-1) && !allowSelection)
{
ignoreSelectedChanged = true;
listBox1.SetSelected(currentSelectedIndex, false);
}
//unselect item because it wasn't before or after the last selected item
}
ignoreSelectedChanged = false;
}
private void TrackSelection(ListBox listBox, List<int> alreadySelectedList)
{
ListBox.SelectedIndexCollection indexCollection = listBox.SelectedIndices;
foreach (int index in indexCollection)
{
if (!alreadySelectedList.Contains(index))
{
alreadySelectedList.Add(index);
}
}
foreach (int index in new List<int>(alreadySelectedList))
{
if (!indexCollection.Contains(index))
{
//remove first index in list
alreadySelectedList.Remove(index);
//add index back to end of list so we know what was deselected
alreadySelectedList.Add(index);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace listBoxWinforms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.Control)
{
return;
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.