开发者

Avoid selecting new node on key press in TreeView

开发者 https://www.devze.com 2023-01-10 09:33 出处:网络
When TreeView (WinForms) has focus pressing key selects the node whose text begins with a key character.

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 th开发者_开发百科ere any way to override this behavior?


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消