ListBox CTRL-C behavior (autofocus issue)

G

Guest

I have a ListBox control on a form, and I am capturing the CTRL-C with the
KeyDown event. All works well, but I have the unwanted side effect of the
ListBox moving the currently selected item to the first Item that start with
C as well.

Example:
If the ListBox has 4 items:
apple
banana
candy
door

Once 'apple' is selected and I press CTRL-C, I handle the event in the
KeyDown method I created (basically copying the value of the item to the
clipboard), but then the selected item becomes 'candy' (I guess the framework
"thinks" I am trying to focus on the first item that starts with 'c')

How can I disable the auto focusing on the first letter behavior when the
user clicks CTRL-C. (The auto focus is actually good when the user clicks 'c'
by itself, or any other letter and the selected item becomes the item that
starts with that letter).
 
C

Claes Bergefall

The KeyEventArgs passed to your KeyDown event handler has a Handled
property. Set that to true to prevent the default handling

/claes
 
G

Guest

I inserted:
e.Handled = true;
as the last line in my key down handler:
private void formCopy_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e){....}
But still I see no change, I still get a focus to the first item that start
with c when I press CTRL-C

Am I doing something wrong?

Roy.
 
G

Guest

It worked!
After a few more tests i found out that I should put the Handled=true in the
KeyPress event handler. So I use the KeyDown handler to check for CTRL-C
if (e.KeyData == (Keys.C | Keys.Control)) ...
and then I set a private variable flag to true listBox_CTRL_C_Event = true;
In the KeyPress handler (which is fired after the KeyDown event) I check for
the value of listBox_CTRL_C_Event and if true, I use the Handled=false; and
then listBox_CTRL_C_Event=flase; to reset the flag.

Works great.

Roy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top