Ask Your Question
1

Setting Focus on a button without a mouse, inside a macro

asked 2020-08-22 12:50:16 +0200

UncleOilybag gravatar image

updated 2020-08-22 16:01:46 +0200

ebot gravatar image

My aim is to produce a CALC spreadsheet that can be used WITHOUT a mouse (or pointer or trackpad) My sheets contain a few macros which I know I can 'fire' from the top menu but most of the macros are triggered by buttons

So when writing a macro - how do I set the focus on to a particular button from the macro itself. ( Yes I know that the keyboard command CTL+W sets the focus onto the button - but how do I set the focus onto a particular button from within the macro? )

Thank you for your interest in this problem

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2020-08-22 17:02:35 +0200

Ratslinger gravatar image

Hello,

You can set focus through the controls View. Here is the code:

Sub SetFocus
    Dim oSheet             As Object
    Dim oController      As Object
    Dim oForm              As Object
    Dim oControl          As Object
    Dim oControlView  As Object
Rem Get sheet - Active or by index or by name
    oSheet=ThisComponent.CurrentController.ActiveSheet
Rem    oSheet = ThisComponent.getSheets().getByIndex(0)
Rem    oSheet = ThisComponent.getSheets().getByName("YOUR_SHEET_NAME")
Rem Get Controller
    oController = ThisComponent.getCurrentController()
Rem Get the Form - Use internal form name
    oForm = oSheet.getDrawpage().getForms().getByName("YOUR_INTERNAL_FORM_NAME")
Rem Get the control - Use your control name
    oControl = oForm.getByName("YOUR_CONTROL_NAME")
Rem Get the view of the control
    oControlView = oController.getControl(oControl)
Rem Set focus to the specified control
    oControlView.setFocus()
End Sub

There are different ways to access your sheet. Left all in (two Rem'ed) for your choice. Remember to change code to reflect your internal form name and control name also.

edit flag offensive delete link more

Comments

This is a perfect answer - thank you so much.

UncleOilybag gravatar imageUncleOilybag ( 2020-08-23 17:23:48 +0200 )edit

Hi. How can this be modified to work in any general sheet? removing the specific name into the Sub?

LibreOffice_Mike gravatar imageLibreOffice_Mike ( 2021-04-01 19:58:30 +0200 )edit

@LibreOffice_Mike,

Don't understand what "specific name into the Sub" means. Are you talking of the Sub name, sheet name, form name control name? Maybe a little more info on what you actually are attempting.

Ratslinger gravatar imageRatslinger ( 2021-04-01 20:42:46 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2020-08-22 12:50:16 +0200

Seen: 121 times

Last updated: Aug 22 '20