[Solved] Setting the focus or cursor in a Dialog

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Alex6361
Posts: 29
Joined: Fri Jun 05, 2015 2:15 am

[Solved] Setting the focus or cursor in a Dialog

Post by Alex6361 »

I have a dialog in a spreadsheet document with various Check Boxes, Radio and Control Buttons, and Text Boxes. Based on which button gets pushed or which checkbox gets checked I'd like the focus / cursor (I'm not sure what it's called) to be placed on / in one of the Text Boxes. As a user, after clicking a button or checkbox, I can hit the Tab key until the appropriate Text Box gets focus, but I'd like to do that programatically through On-action macros assigned to the buttons and checkboxes. How do I do this?
Last edited by Alex6361 on Tue Jun 16, 2015 9:06 am, edited 3 times in total.
LibreOffice Version: 7.4.7.2
Debian Linux 12 & Windows 11
User avatar
Zizi64
Volunteer
Posts: 11364
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Setting the focus or cursor in a Dialog

Post by Zizi64 »

Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Alex6361
Posts: 29
Joined: Fri Jun 05, 2015 2:15 am

Re: Setting the focus or cursor in a Dialog

Post by Alex6361 »

Thanks again, Tibor, for trying to help me over another impasse! As it turns out, the links you suggested were for cases when the control was on a DrawPage or database form, and the case I had is for controls on a Dialog accessed by a macro from a spreadsheet document. I finally stumbled onto the solution, and it is just what I would have wanted it to be, but not documented in a straightforward way. In fact, the API documentation I'm using gave me the impression that the method was only valid for windows, and not things on them. Anyway, the following code shows how it's done.

Code: Select all

Sub SetTheFocus()

    Dim oMyDialog as Object
    Dim sDialogName as String
    Dim sControlName as String
	
    sDialogName = "<name of your dialog goes here>"
    sControlName = "<name of the control on your dialog goes here>"

    'the following has probably already been done before you need to set focus on anything:
    DialogLibraries.LoadLibrary("Standard")
    if IsNull(oMyDialog) then 
		oMyDialog = CreateUnoDialog (DialogLibraries.getByName("Standard").getByName(sDialogName))	
    end if

    'the following line is all it takes once you have an object handle for the dialog
    oMyDialog.GetControl(sControlName).setFocus()

    'the next time the dialog is presented the focus / cursor should be on / in sControlName
    oMyDialog.execute   
 
End Sub
LibreOffice Version: 7.4.7.2
Debian Linux 12 & Windows 11
Post Reply