Listview

  • Thread starter Thread starter Ferdinand Zaubzer
  • Start date Start date
F

Ferdinand Zaubzer

Hello,
is there any possibility to prevent a ListView from selecting an item
starting with a certain letter when pressing the corresponding key?
Thanks, Ferdinand
 
Hi Ferdinand,

I'm sure there are other ways, but the first thing that entered my mind is
ownerdrawing the text and hiding the real text by putting some character
as a prefix, thereby preventing a match on a key press. It may affect
sorting, and it is probably far more elaborate to achieve than worthwhile.

The code below is written for a ListView with OwnerDraw set to true, and
with the items "One", "Two", "_Three", "Four".
The underscore indicates makes sure Three won't be selected if you type T,
even though it looks like you should be able to as the DrawItem event
removes the _ before drawing the text.

private void listView1_DrawItem(object sender,
DrawListViewItemEventArgs e)
{
string text = e.Item.Text;
if(text.StartsWith("_"))
text = text.Substring(1);

if ((e.State & ListViewItemStates.Focused) > 0)
{
e.Graphics.FillRectangle(SystemBrushes.Highlight,
e.Bounds);
e.Graphics.DrawString(text, listView1.Font,
SystemBrushes.HighlightText, e.Bounds.Left, e.Bounds.Top);
}
else
{
e.Graphics.DrawString(text, listView1.Font,
SystemBrushes.ControlText, e.Bounds.Left, e.Bounds.Top);
}
}
 
Back
Top