{"id":514,"date":"2011-09-09T09:16:27","date_gmt":"2011-09-09T07:16:27","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=514"},"modified":"2011-09-09T09:17:10","modified_gmt":"2011-09-09T07:17:10","slug":"treeviewex-a-few-enhancements","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2011\/09\/treeviewex-a-few-enhancements\/","title":{"rendered":"TreeViewEx &#8211; a few enhancements"},"content":{"rendered":"<p>Sometimes I wonder why some functionality that should be common is simply left out of general controls.. like the WinForms <em>TreeView<\/em> (and <em>ListView<\/em>) controls that don&#8217;t have specific events for the enter key pressed. I&#8217;ve been taught long ago to always provide functionality so keyboard events also can be used to navigate around applications &#8211; like when the mouse is unavailable, broken or not your first choice (like for disabled people). Yes, I know the controls have general events for key pressing but it requires some hectic coding each time to do something that should be simple.<\/p>\n<p>Anyway, adding it isn&#8217;t too difficult (but I still wished I didn&#8217;t have to do it each time I need to use it).<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">public class<\/span> TreeViewEx : <span style=\"color: #0000ff;\">TreeView<\/span><br \/>\n{<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">public<\/span> TreeViewEx() : <span style=\"color: #0000ff;\">base<\/span>()<br \/>\n{<\/p>\n<p style=\"padding-left: 60px;\">DoubleBuffered = <span style=\"color: #0000ff;\">true<\/span>;<\/p>\n<p style=\"padding-left: 30px;\">}<br \/>\n<span style=\"color: #0000ff;\"> public event MethodInvoker<\/span> EnterKeyPressed;<br \/>\n<span style=\"color: #0000ff;\"> public event MethodInvoker<\/span> DeleteKeyPressed;<br \/>\n<span style=\"color: #0000ff;\"> protected override void<\/span> OnKeyPress(<span style=\"color: #0000ff;\">KeyPressEventArgs<\/span> e)<br \/>\n{<\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">if<\/span> (e.KeyChar == &#8216;\\r&#8217;)<\/p>\n<p style=\"padding-left: 90px;\">if (EnterKeyPressed != <span style=\"color: #0000ff;\">null<\/span>)<br \/>\n{<\/p>\n<p style=\"padding-left: 120px;\">EnterKeyPressed();<br \/>\ne.Handled = <span style=\"color: #0000ff;\">true<\/span>;<\/p>\n<p style=\"padding-left: 90px;\">}<\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">base<\/span>.OnKeyPress(e);<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">protected override void<\/span> OnKeyDown(<span style=\"color: #0000ff;\">KeyEventArgs<\/span> e)<br \/>\n{<\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">if<\/span> (e.KeyCode == <span style=\"color: #0000ff;\">Keys<\/span>.Delete)<\/p>\n<p style=\"padding-left: 90px;\"><span style=\"color: #0000ff;\">if<\/span> (DeleteKeyPressed != <span style=\"color: #0000ff;\">null<\/span>)<br \/>\n{<\/p>\n<p style=\"padding-left: 120px;\">DeleteKeyPressed();<br \/>\ne.Handled = <span style=\"color: #0000ff;\">true<\/span>;<\/p>\n<p style=\"padding-left: 90px;\">}<\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">base<\/span>.OnKeyDown(e);<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p>}<\/p><\/blockquote>\n<p>The code for the <em>ListView<\/em> control is basically (exactly) the same.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes I wonder why some functionality that should be common is simply left out of general controls.. like the WinForms TreeView (and ListView) controls that don&#8217;t have specific events for the enter key pressed. I&#8217;ve been taught long ago to &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2011\/09\/treeviewex-a-few-enhancements\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[7,8,9,12,303,11,75,10],"class_list":["post-514","post","type-post","status-publish","format-standard","hentry","category-development","tag-net","tag-c","tag-code","tag-control","tag-development","tag-listview","tag-treeview","tag-winforms"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/514","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=514"}],"version-history":[{"count":3,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/514\/revisions"}],"predecessor-version":[{"id":517,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/514\/revisions\/517"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}