[Solved] One Event for many buttons

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
orangebokov
Posts: 2
Joined: Wed Sep 04, 2013 6:49 pm

[Solved] One Event for many buttons

Post by orangebokov »

I have a lot of buttons for which i want to create an event handler.
How can i learn the name of button that has been clicked?

Code: Select all

Sub butPlus_click
	Dim field as Object

        theNameOfButton = ?????

	field = dialog.getControl("num_"+theNameOfButton)
	field.Value = field.Value + 1
End Sub
Last edited by Hagar Delest on Wed Sep 04, 2013 9:48 pm, edited 2 times in total.
Reason: tagged [Solved].
OpenOffice 3.4
User avatar
Zizi64
Volunteer
Posts: 11359
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: One Event for many buttons

Post by Zizi64 »

Here are some threads with similar problem:
http://forum.openoffice.org/en/forum/vi ... =5&t=44260
http://forum.openoffice.org/en/forum/vi ... 39&t=38725
http://ooo-forums.apache.org/en/forum/v ... 78&p=80541


Try to assign to some buttons the subroutine below:

Code: Select all

Sub Any_Button(oEvent)

Dim str as string
Dim oCaller as object

   oCaller=oEvent.Source.Model
   str=oCaller.Name
   Print "You pushed the button named: ", str
end sub
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.
orangebokov
Posts: 2
Joined: Wed Sep 04, 2013 6:49 pm

Re: One Event for many buttons

Post by orangebokov »

A lot of thanks! That works!
OpenOffice 3.4
kraybone
Posts: 4
Joined: Mon Jan 23, 2017 11:41 am

Re: [Solved] One Event for many buttons

Post by kraybone »

OK, thanks.

sub WhoCalledMe()
Dim str as string
Dim oCaller as object

oCaller=oEvent.Source.Model
str=oCaller.Name
Print "You pushed the button named: ", str

end sub

I get the message "BASIC runtime error. Variable not defined" on the line : oCaller=oEvent.Source.Mode
Any suggestions?
Thanks
Version: 5.2.4.2 on Windows 10 Professional
kraybone
Posts: 4
Joined: Mon Jan 23, 2017 11:41 am

Re: [Solved] One Event for many buttons

Post by kraybone »

OK, just spotted my error!!
I forgot toe argument "oEvent"
Works fine
Thank you very much :)
Cheers
Version: 5.2.4.2 on Windows 10 Professional
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] One Event for many buttons

Post by JeJe »

If you have a lot of controls, you might find this function I wrote to get the number at the end of a control useful. You can use the number as an index, which is the nearest you can get to having a control array in OOBasic.

Select case endval(oEvent.source.model.name)
case 1 'for "CommandButton1"
case 2 to 6 'for "CommandButton2" to "CommandButton6"
end select

Function EndVal(st As String) As Long

Dim i As Long, a As Integer, s As String, res As String
res = ""
s = ""
For i = Len(st) To 1 Step -1
s = Mid(st, i, 1)
a = Asc(s)
If a >= 48 And a <= 57 Then
res = s & res
Else
Exit For
End If
Next
EndVal = Val(res)
End Function

(Edit:
You can create an array of objects as well and set each to a control - but you can't easily get where the control is in the array from an event. If you use the number at the end of the control as an index its easy.)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply