Ivan I
Ivan I

Reputation: 9988

Avoid selecting new node on key press in TreeView

When TreeView (WinForms) has focus pressing key selects the node whose text begins with a key character.

Is there a way to avoid this?

First thing that came to my mind was to create Control that inherits from TreeView, and override IsInputKey so that it returns false in all undesirable cases.

But it doesn't work. Is there any way to override this behavior?

Upvotes: 1

Views: 2227

Answers (3)

shimon
shimon

Reputation: 36

Set the e.Handled = true; in both keyup & keypress events.

Upvotes: 2

26071986
26071986

Reputation: 2330

For both KeyPress and KeyDown events of the TreeView (not the form) implementing

e.Handled = false;

will block the selection of the node. Moreover, these events will be fired only when the TreeView is focused already. And it will not block the whitespace key.

Upvotes: 2

Ivan I
Ivan I

Reputation: 9988

I think I found one possible work around (though not elegant and not ideal)

So, I check KeyDown event, and if it happens I record selected node in one variable.

After that in AfterSelect event I actually Select that node again.

If someone has more elegant solution, it would be welcome.

Upvotes: 0

Related Questions