By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,630 Members | 1,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,630 IT Pros & Developers. It's quick & easy.

TreeView: How to capture Enter?

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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
Expert Mod 100+
P: 2,321
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.