472,133 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,133 software developers and data experts.

TreeView: How to capture Enter?

I've got KeyDown coding and it fires when other keys are pressed, but when Enter
is pressed, nothing.

I'd like to support the (standard?) Windows behavior of executing DblClick
processing when an item is selected and the user presses Enter

I got focus to stop moving to the next control via Tools|Options|Keyboard|Move
after enter....but now it just sits there and neither KeyDown nor KeyPress seems
to catch it.

Is this the end of the road?
--
PeteCresswell
Nov 13 '05 #1
7 8218
On Sat, 08 Jan 2005 20:10:17 GMT, "(Pete Cresswell)" <x@y.z> wrote:
I've got KeyDown coding and it fires when other keys are pressed, but when Enter
is pressed, nothing.

I'd like to support the (standard?) Windows behavior of executing DblClick
processing when an item is selected and the user presses Enter

I got focus to stop moving to the next control via Tools|Options|Keyboard|Move
after enter....but now it just sits there and neither KeyDown nor KeyPress seems
to catch it.

Is this the end of the road?
--
PeteCresswell


Hi
The KeyDown and KeyUp events don't occur when you press the ENTER key
if the form has a command button for which the Default property is set
to Yes.

So maybe if you make sure it hasn't they will. You will have to to
trap and act on the enter key for command buttons yourself.
HTH
David

Nov 13 '05 #2
Per David Schofield:
Hi
The KeyDown and KeyUp events don't occur when you press the ENTER key
if the form has a command button for which the Default property is set
to Yes.

So maybe if you make sure it hasn't they will. You will have to to
trap and act on the enter key for command buttons yourself.
HTH


No Command buttons w/.Default=True - and I tested that by putting a regular
ListBox on the same form. The regular ListBox's KeyDown event fires when
Return/Enter are pressed (KeyCode=13).

It's starting to sound like the TreeView's behaviour is somewhat different.
--
PeteCresswell
Nov 13 '05 #3
On Sun, 09 Jan 2005 22:21:21 GMT, "(Pete Cresswell)" <x@y.z> wrote:
Per David Schofield:
Hi
The KeyDown and KeyUp events don't occur when you press the ENTER key
if the form has a command button for which the Default property is set
to Yes.

So maybe if you make sure it hasn't they will. You will have to to
trap and act on the enter key for command buttons yourself.
HTH


No Command buttons w/.Default=True - and I tested that by putting a regular
ListBox on the same form. The regular ListBox's KeyDown event fires when
Return/Enter are pressed (KeyCode=13).

It's starting to sound like the TreeView's behaviour is somewhat different.
--
PeteCresswell

Hi
Um ...
You can set the form.KeyPreview = true and catch ENTER with KeyDown at
the form level, check that the treeview is active and in a suitable
state (use the treeview events to set up this) and then call your
doubleclick event. But the ENTER will still be passed to the treeview
control as KeyDown doesn't have a cancel argument.

Straightforward subclassing the treeview doesn't catch Enter, but if
you are up for heavy coding see
How To Prevent the ENTER Key From Firing in TreeView Control
http://support.microsoft.com/default...b;en-us;216664

This is written for VB.

It is a bold developer who deploys any Access app with treeview in it,
let alone one with subclassing!

David
HTH
David

Nov 13 '05 #4
Per David Schofield:
It is a bold developer who deploys any Access app with treeview in it,
let alone one with subclassing!


I've got a couple out there with no reported problems.

("Reported" being the operative word....)
Anything special I should be on the lookout for?
--
PeteCresswell
Nov 13 '05 #5
Per David Schofield:
ou can set the form.KeyPreview = true and catch ENTER with KeyDown at
the form level, check that the treeview is active and in a suitable
state (use the treeview events to set up this) and then call your
doubleclick event. But the ENTER will still be passed to the treeview
control as KeyDown doesn't have a cancel argument.


Still no-go.

I checked it with ?frmWhatever.Keypreview in the immediate window....and,
indeed, it still fires KeyDown and KeyPress for the space bar and other
keys...but not Enter.

I think I see why, though. The list is already using Enter to toggle nodes'
..Expanded. Hit Enter on a collapsed node and it expands...do the same thing
on an expanded node and it collapses.

I guess that about wraps it.... I guess I'll just trap SpaceBar and tell the
users to learn to love it.
--
PeteCresswell
Nov 13 '05 #6
On Tue, 11 Jan 2005 00:44:22 GMT, "(Pete Cresswell)" <x@y.z> wrote:
Per David Schofield:
It is a bold developer who deploys any Access app with treeview in it,
let alone one with subclassing!


I've got a couple out there with no reported problems.

("Reported" being the operative word....)
Anything special I should be on the lookout for?
--
PeteCresswell

Hi
Problems with common controls arise mainly in MDE/run time systems
when older or newer versions of the DLL are on the user's PC
David

Nov 13 '05 #7
TheSmileyCoder
2,322 Expert Mod 2GB
I was just myself searching for a solution to the above problem without any luck, but as I did find (partly based on the above) a solution I will post it here, even if it is a quite old thread.


First you must ensure that the Form previews the key pressed before the control. Enter your form properties and find the property called "Key Preview" and set it to TRUE.

This part is optional, but I decided to add a public enum to make it easier for myself to see the keycodes, so this goes into a public module:
Expand|Select|Wrap|Line Numbers
  1. Public Enum Keys
  2.    Enter = 13
  3.    Spacebar = 32
  4.    NoKey = 0
  5. End Enum
Now write code for the Key_Down event of the form, and notice I use it for both my treeview and my listview:
Expand|Select|Wrap|Line Numbers
  1.    'Only react to keyStroke=enter(13)
  2.    If KeyCode <> Keys.Enter AND Keycode<>Keys.Spacebar then Exit Sub
  3.  
  4.    'Respond accordingly to which control is the active control
  5.       Select Case Me.ActiveControl.Name
  6.          Case Me.TreeProjects.Name
  7.             'Treeview is active, determine active node (if any)
  8.                Dim tv As TreeView
  9.                Set tv = Me.TreeProjects.Object
  10.                If Not tv.SelectedItem Is Nothing Then
  11.                   Custom_TreeProjects_NodeSelect tv.SelectedItem
  12.                End If
  13.  
  14.             'Cancel enter keystroke
  15.                KeyCode = Keys.NoKey
  16.  
  17.          Case Me.listReports.Name
  18.             Select Case KeyCode
  19.                Case Keys.Enter
  20.  
  21.                Case Keys.Spacebar
  22.                   Dim lv As MSComctlLib.ListView
  23.                   Set lv = Me.listReports.Object
  24.                   If Not lv.SelectedItem Is Nothing Then
  25.                      OpenReport getID(lv.SelectedItem)
  26.                   End If
  27.             End Select
  28.             'Cancel enter keystroke
  29.                KeyCode = Keys.NoKey
  30.  
  31.       End Select
  32.  
  33. exitSub:
  34.    Set tv = Nothing
  35.    Set lv = Nothing
  36.  
  37.  
  38. End Sub
The Original poster doesn't say so, but I think when he tried this, he kept using the Treeviews KeyDown event, after turning on Key Preview, and thus failed to move the tracking into the forms Key_down (Which occurs any control reacts to Key_Down, thus why it is called Preview)
Aug 22 '12 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Hrvoje Voda | last post: by
1 post views Thread by Matthew Wieder | last post: by
2 posts views Thread by clsmith66 | last post: by
5 posts views Thread by ewillyb | last post: by
2 posts views Thread by Bernie Yaeger | last post: by
reply views Thread by ali element via .NET 247 | last post: by
2 posts views Thread by rolf.oltmans | last post: by
reply views Thread by krissco | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.