Welcome to the SRP Forum! Please refer to the SRP Forum FAQ post if you have any questions regarding how the forum works.

How to unfocus an item

My interface looks like this:

The behaviour is a follows:

when you press the Add button and no item in the tree has focus, the program will add a new node at the root level. When you press the Add button and an item has focus, then the program will add a new node below the item with focus.

I want the user to be able to do either of these things repeatedly.

The problem is, once an item gets focus, I can't programmatically unfocus it. Which means that the user cannot add a new node at the root level, since there is no way for the program to unfocus an item.

I want the user to be able to unfocus the item when they click on the white area. When this happens, i call these 2 lines of code, which should unfocus the item:

Set_Property(tree_ctrl$, "OLE.SelectedItems", "")
Set_Property(tree_ctrl$, "OLE.FocusItem", "")

But the item with focus retains focus. Surely it's possible, but I am doing something wrong?


  • I'll have our engineer for this control take a deeper look to see if any of the properties (or methods) can do exactly what you want. In the meantime, what about just setting the FocusItem to the root item?
  • I think the problem is your reliance upon focus when using a button to add items. The point of focus is to mark the keyboard's location. This is critical for people using the keyboard to navigate the tree. But your button uses the mouse to add items, so there is a conflict of interest here.

    Give your UI design, I recommend using selection instead of focus. That way, the user clicks an item to select it, then clicks add to add an item. Adding items won't mess with selection, so you won't encounter the issue you are having now. Moreover, selection is much easier to see than focus.
  • @KevinFournier

    Hi, the problem with using selection is that, as soon as I press the Add button, the selection goes away.
  • Are you adding items by replacing the ItemList property or by using the AddItems method. The latter should not affect selection.
  • Hi, I am using the AddItems method. But the unselection is happening as soon as I click the button, not after. Anyway, it's ok. I have decided to do this in another way which avoids this issue.

    thanks for your help.
  • Even if you have another solution, I feel like there is an important miscommunication happening here. Selection and focus are two different things. The dotted line is focus, and it will always go away when you click on the button because focus leaves the control. Selection does not go away, though it does change colors to indicate the tree doesn't have focus, unless you made significant changes to the Colors property.
  • edited June 2019
    "Selection does not go away"

    Visually it does, but you're saying that get_Property(tree_ctrl$, "OLE.SelectedItems") shoudl still contain what was selected before the button was clicked?

    I did not notice this. For me, that code returns an empty string.

    Anyway, all of this can be avoided by using context menus...rather than pressing a button outside of the control. Context menus are the way to go imo.
  • Context menus work, and I'm glad you have a work around. I'm still stymied as to how your selection is going away. I'd have to see your Add logic to diagnose further, but if you're happy, I'm happy. :)
Sign In or Register to comment.