# How to remove focus from a Calc ListBox Control

I have placed a ListBox Control on a Calc Sheet and assigned a Basic Macro to the control's 'Changed' event. The last thing the macro does is place the cursor on a desired cell on the sheet using: dispatcher.executeDispatch(ThisComponent.CurrentController.Frame, ".uno:GoToCell", "", 0, args()) Everything works as desired except: After the macro completes execution, the ListBox sometimes retains focus, but sometimes it does not. By 'retains focus' I mean that the ListBox displays the dotted-line around the box (indicating focus) and the selected item shown in the box is highlighted with a blue background. Since the Control still has focus, pressing the up/down arrow keys causes the previous/next item in the list to be selected and triggers the 'Changed' event again, and the ListBox retains focus. The Control's TabStop property is set to 'No'. So when the ListBox has focus, pressing the Tab key does not remove focus from the Control.

After macro completion, if I manually use the mouse to select any cell on the sheet, the ListBox immediately loses focus, the dotted lines in the Control disappear, the background of the selected item reverts to the white background, and pressing the arrow keys moves the 'cursor' to the next cell above or below the selected cell.

Behavior is the same regardless of if I select a ListBox item using the mouse to pull-down and click a selection, or if I use the arrow keys to simply move up or down in the selection list. I have tried using ThisComponent.CurrentController.Select(cell) in combintion with the above 'dispatch' command and separately with no change in behavior: Sometimes the ListBox retains focus when the macro completes, and sometimes it does not.

Can anyone suggest a way for me to dependably remove focus from the ListBox Control after it is used to select an item? I have been unable to identify a specific sequence of actions or macro code that will dependably predict whether the control will retain focus or not.

edit retag close merge delete

Sort by » oldest newest most voted

The following discussion replaces my original answer to this question.

My first ‘solution’ was to preceded the UNO:GoToCell command mentioned in the OP with an UNO:ToggleControlFocus command. But extensive testing showed that command did not operate entirely as assumed/desired.

For clarity, what I want my macro to do following a ListBox selection is to remove focus from the ListBox, place the cell cursor and focus on a single ‘GoTo’ cell, with no selection highlighting, and allow the arrow, tab, and enter keys to operate as normal/expected within the sheet.

It turns out that although the ToggleControlFocus command does remove focus from the ListBox, it does NOT toggle focus as its name implies. Rather it operates as Calc normally does when a Control has focus and the Tab key is pressed – simply transferring focus from the in-focus Control to the next Control in the Tab sequence. This Tab-like behavior of the ToggleControlFocus command is true even though all Controls on the sheet I am using are configured with their TabStop and Take Focus on Click properties set to ‘No’.

Let me explain: In my sheet, the ‘next’ Control is a Push-Button Control, so ToggleControlFocus transfers focus from the ListBox Control to that Push-Button Control. Since all of the Controls are configured with no TabStop the dotted-lines that would normally indicate which Control has focus are not displayed. But a Control does, in fact, have focus. And when a Push-Button Control has focus, pressing the Enter key causes that Push-Button to trigger its Execute Action event as if it had been clicked. The Control retains focus even after Enter or an arrow key is pressed and even though its Take Focus on Click property is set to ‘No’.

I tried the UNO ToggleControlFocus and GoToCell commands with all sorts of permutations, combinations, and sequences of other UNO dispatch commands, including FocusCellAddress, FocusInputLine, InputLineEnter, GoUp/DownBlock, Escape, SetInputMode, and ToggleInputMode – all without success. No matter what I tried, either the ListBox retained focus, or focus was merely transferred to the next Control as noted above, or the final cell cursor state was something other than desired and arrow and other keys had no effect.

Of course, the lack of documentation regarding proper use of any of those commands means I was guessing which required use of an argument array and what the proper argument values should be. But that’s another story.

The solution I have settled on for now (for lack of a better one) is to abandon use of ToggleControlFocus entirely, use GoToCell to place the cursor on the desired cell, momentarily select another sheet within my workbook, and then re-select the ListBox’s sheet:

args(0).Value = oSheet.getCellByPosition(columnNbr, rowNbr).AbsoluteName
args(0).Name = "ToPoint"
dispatcher.executeDispatch(ThisComponent.CurrentController.Frame, ".uno:GoToCell", "", 0, args())
ThisComponent.CurrentController.setActiveSheet(ThisComponent.Sheets(0))
ThisComponent.CurrentController.setActiveSheet(ThisComponent.Sheets(1))


The above removes focus from the ListBox Control and simply ‘refreshes’ display of the desired sheet with the cursor ...

more