Page 1 of 1

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

Posted: Sun May 18, 2008 11:00 am
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

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

Posted: Sun May 18, 2008 1:00 pm
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

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

Posted: Mon May 19, 2008 2:32 pm
by lelynx
Thank you, ms77, it greatly help me!

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

Posted: Fri Jun 07, 2019 7:26 pm
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

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

Posted: Sat Jun 08, 2019 2:33 pm
by JeJe
Look at OpenOffice.org Macros Explained Third Edition, Listing 551

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

Its a free download

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

Posted: Sat Jun 08, 2019 7:49 pm
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.