How to bind an event handler to a button on a dialog

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
lelynx
Posts: 6
Joined: Sat May 17, 2008 3:06 pm

How to bind an event handler to a button on a dialog

Post by lelynx »

Hi all,

I can't manage to have the following code snippet run ok.
Could you please help me?

Purpose: to insert by code a command button onto a dialog
and attach an onclick event handler to this button.

Result: the dialog is dispayed with the button.
A click on the button will give an error message: property or method will not exist.

TIA,

Philippe

Code: Select all

dim mdlg as object
' ======================================================
Sub Main
 ' to get a reference to Dialog1
 biblio=DialogLibraries.GetByName("Standard")
 oFrm=biblio.getByName("Dialog1")
 ' instanciate the dialog
 mdlg=createUNODialog(oFrm)
 ' to add a button and attach an event handler
 call AjoutBouton( mdlg ,"monBtn" ,3 , 3  ,1)
 ' show the dialog
 mdlg.execute
 ' close the dialog
 'mdlg.endExecute
End Sub

Sub AjoutBouton( dialogue as object,nomBouton as string, positionX as integer,positionY as integer)

  Dim oDialogModel As Object , oButtonModel As Object 
  NomObj = nomBouton  
  oDialogModel = dialogue.Model

  oButtonModel = oDialogModel.createInstance("com.sun.star.awt.UnoControlButtonModel" )

  	With oButtonModel
	    .Name = NomObj
	    .Label = "affecter"
	    .PushButtonType="STANDARD"
	    .Tabstop = True
	    .PositionX = positionX 'position horizontale
	    .PositionY = positionY 'position verticale
	    .Width = 27 'largeur
	    .Height = 14 'hauteur
  	End With
  	' to insert the button into the dialog
	oDialogModel.insertByName( NomObj , oButtonModel )

       ' create a listener

	oButtonControl = dialogue.getControl(NomObj)
	cEventListenerName =NomObj
	' oActionListener = CreateUnoListener( cEventListenerName + "_", "com.sun.star.awt.XActionListener" ) 
	oActionListener = CreateUnoListener( "NomBouton_","com.sun.star.awt.XActionListener" ) 
	oButtonControl.addActionListener( oActionListener )
End Sub

' ===============Event handler==========================
public sub NomBouton_dispose(e as object) ' I tried _click, _onclick but unsuccessfully!
'on error resume next
	msgbox "Salut :" & e.source.model.name
end sub
ms777
Volunteer
Posts: 177
Joined: Mon Oct 08, 2007 1:33 am

Re: How to bind an event handler to a button on a dialog

Post by ms777 »

Hi,

you need to implement all functions of the XActionListenr, not only dispose. You are missing the actionPerformed function.

For an example, see http://www.oooforum.org/forum/viewtopic ... 497#205497

Good luck,

ms777
lelynx
Posts: 6
Joined: Sat May 17, 2008 3:06 pm

Re: How to bind an event handler to a button on a dialog

Post by lelynx »

Thank you, ms77, it greatly help me!
batistaj
Posts: 3
Joined: Sat May 18, 2019 12:52 pm

Re: How to bind an event handler to a button on a dialog

Post by batistaj »

Hi lelynx
Your question is still very pertinent nowadays cause I'm finding the same trouble being novice to Libreoffice and I can't find the link posted by ms777.
Would you please give me a tip how did you solve de problem?
Thanks
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to bind an event handler to a button on a dialog

Post by JeJe »

Look at OpenOffice.org Macros Explained Third Edition, Listing 551

http://www.pitonyak.org/book/

Its a free download
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
batistaj
Posts: 3
Joined: Sat May 18, 2019 12:52 pm

Re: How to bind an event handler to a button on a dialog

Post by batistaj »

Thank you very much for the posted link.
After a weak of searching you put me in the right direction.
I'm setting hands on it.
Post Reply