I'm looking to disable the Ctrl key on a ListBox.
I followed the example at Disable list box CTRL+C & CTRL+X in D&D .
It took some playing, since I'm using System.Windows.Input
and using System.Windows.Forms
.
I was able to resolve the references, except for the e
object.
It tells me there is no Handled
definition within System.Windows.Forms.PreviewKeyDownEventArgs
.
What am I missing here? Thanks!
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;
}
}
OK, I'm getting closer with @Tsukasa 's code. Here's what I have now.
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);
}
}
}
But, this is letting me de-select an item surrounded by 2 selected items. Once de-selected, it won't let me re-select that item, even though it has selected items on either side of it. I added in the section that follows the comment: //make sure we have both a next and a previous item