Handling the Enter Key On a Node in a TreeView in C#

Album Cover: Into the Wild

"I knew all the rules, but the rules did not know me...guaranteed."
Eddie Vedder / Guaranteed

Posted on May 08, 2011 11:51 AM in Programming
Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

I was surprised to find that hitting the Enter key on a node in a TreeView control in C# isn't treated like either a single- or double-click event. I suppose it's a good thing, from a developer's perspective, since it gives you maximum flexibility on handling the different types of input. However, it does require more work (and code) to make sure your keyboard- and mouse-driven input is treated equally, if that's what you desire.

I wanted my C# application to mimick behavior I'm used to in other applications, where single-clicking the mouse is the same as "browsing" the node with the arrow keys. You can go from one node to the next without firing off any selections. On a double-click, you are explicitly choosing that node, either to expand its children, see its associated contents (e.g. in another pane), or both. I implemented that behavior by handling the typical NodeMouseClick and NodeMouseDoubleClick events.

However, also mimicking behavior I'm used to in other applications, I wished to have the Enter (or Return) key behave the same as a double-click. To do so, I had to handle KeyPress events on the TreeView control itself, making sure to check that a node actually was selected prior to triggering my subsequent behavior. The code ended up looking something like the following:

// handling for when a key is pressed when the treeview is active
private void treeView_KeyPress(object sender, KeyPressEventArgs e)
{
  // if there is a currently selected node
  if (null != treeView.SelectedNode)
  {
    // if the Enter key was pressed
    if ((char)Keys.Return == e.KeyChar)
    {
      // do stuff
    }
  }
}

Since presumably one could hit Enter before any node was selected in the TreeView, it's a good idea to make sure there is a selected node before doing anything else. Checking for the Enter key input is as simple as converting the Keys.Return constant to a char and then comparing it to the key the user pressed, e.KeyChar.

This is pretty straightforward stuff once you figure it out, but I didn't see many existing examples online and kinda had to figure it out on my own (often the best way to learn), so I figured I'd post it here in case it saves someone a few minutes of their time.

Comments

Jan Eduardsen on August 11, 2011 at 10:54 PM:

Thanks Bernie,
Saved me some time, your example works well.

Keep up the good work, we will be checking your site again :-) Have a nice weekend.

Best Jan

Permalink

Peter Brightman on September 23, 2015 at 6:00 AM:

Thanks for this code. It helped me to get along.

Permalink

100% Niche Relevant blog comments services on September 05, 2016 at 2:51 AM:

This is an extraordinary article much obliged for sharing this enlightening data. I will visit your website consistently for some most recent post. I will visit your web journal consistently for Some most recent post.

Permalink

lyblxvxy on May 15, 2017 at 5:23 AM:

Post Comments

If you feel like commenting on the above item, use the form below. Your email address will be used for personal contact reasons only, and will not be shown on this website.

Name:

Email Address:

Website:

Comments:

Check this box if you hate spam.