Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I have a requirement in my project related to a list box control. I have added file names to the list boxes. I handled "Delete" and "Enter" keys as follows.
Delete - Delete the entry with confirmation from user (with Yes and No buttons)
Enter - opens the file (selected entry on the list box)

Now if i select any item and press "Delete", a confirmation message pops up with "Yes" focused. And then i press "Enter". The selected item gets deleted but another item is getting opened. (this is because i have handled "Enter" key also). I need to avoid this (as this is distracting my basic workflow)

I browsed through various topics on internet but could not achieve this.
Can anyone help me out ?

Thanks and Regards,
YKK Reddy
Posted
Updated 5-Jan-12 19:38pm
v2
Comments
Sergey Alexandrovich Kryukov 6-Jan-12 3:14am    
(Sigh...) What do you mean by ListBox? Do you think there is only one type with this name?
Fully-qualified name, please. Always tag your UI library.
--SA
[no name] 6-Jan-12 3:41am    
ListBox here means of type System.Windows.Forms.ListBox().
Sergey Alexandrovich Kryukov 6-Jan-12 3:15am    
And make a code sample (a short one!) to show the problem. Normally, it never happens. You did something wrong.
--SA
[no name] 6-Jan-12 4:24am    
private void HandleKeyPressEvent(KeyEventArgs e)
{
if (ReportGenerator.m_bIsAdmin)
{
if (e.KeyCode == Keys.Delete)
{
// Delete the file with a user confirmation
}
else if (e.KeyCode == Keys.Enter)
{
// Open the file
}
}
}

1 solution

After many trials i solved it myself.
I handled both KeyDown and PreviewKeyDown Events.
in KeyDown Event, i am doing all the implementation and in PreviewKeyDown i am doing nothing.

The code in method OnPreviewKeyDown() event is blank.

This solves my problem.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900