TreeView node is Selected but grayed out--how set focus?

B

baobob

My UserForm has a TreeView. A node is selected, has blue background
color, and is fully alive.

But if I minimize the form (thanks to Bullen's FormFun) then restore
it, the node is still selected (.SelectedItem.Selected = True), but
it's grayed out and focus has gone somewhere else.

Pressing the Up/Down keys does nothing. But if I press TAB, focus then
jumps to the next control on the form. Pressing Shift-TAB then returns
me to the node AND its color and focus are happily restored.

So programmatically, how do I 1) force focus back on the node, or
better 2) prevent focus from going away UNLESS and until another node
is selected?

I've tried various things and they all fail, e.g.:

with TreeView
if .Enabled and .Visible and (.Nodes.Count > 0) then

1) TreeView.SetFocus

2) SelectedItem.Selected = True

3) SelectedItem.Visible = False followed by ditto = True. (Fails
because, at least for me, .Visible is read-only at run-time).

4) SelectedItem.Checked = True followed by = False.

5) Moving focus away & back again with:

Set S = .SelectedItem
..Nodes.Item(S.Index - 1).Selected = True
S.Selected = True

6) UserForm.Repaint

....quack quack.

Thanks much.

***
 
R

RB Smissaert

Try this. Not tested, but it should work.


In the Form module:
-----------------------------
Option Explicit
Private lFormHwnd As Long
Private Declare Function FindWindow _
Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Public Property Let propFormHwnd(lHwnd As Long)
lFormHwnd = lHwnd
End Property
Public Property Get propFormHwnd() As Long
propFormHwnd = lFormHwnd
End Property

Private Sub Userform_Initialize()
If Val(Application.Version) >= 9 Then
Me. propFormHwnd = FindWindow("ThunderDFrame", strCaption)
Else
Me. propFormHwnd = FindWindow("ThunderXFrame", strCaption)
End If
End Sub


In a normal module:
---------------------------
Option Explicit
Private Declare Function Putfocus Lib "user32" _
Alias "SetFocus" (ByVal hWnd As Long) As Long

Sub FocusTreeview()

Putfocus Form1.propFormHwnd

With Form1
.txtDummy.SetFocus
.TreeView1.SetFocus
End With

End Sub

Where txtDummy is an invisible dummy textbox.


RBS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top