ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   TreeView node is Selected but grayed out--how set focus? (https://www.excelbanter.com/excel-programming/411086-treeview-node-selected-but-grayed-out-how-set-focus.html)

[email protected]

TreeView node is Selected but grayed out--how set focus?
 
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.

***

RB Smissaert

TreeView node is Selected but grayed out--how set focus?
 
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


wrote in message
...
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.

***



[email protected]

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

Thanks VERY much. Will try.

***


All times are GMT +1. The time now is 09:40 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com