Problem to change button background color in macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
serado69
Posts: 5
Joined: Mon May 26, 2008 3:28 pm

Problem to change button background color in macro

Post by serado69 »

Hello,

I'm currently creating a calc macro with several buttons. It sounds that I faced a very basic issue in changing the button background color. Indeed, I can change the button background color in the dialog editor (general tab of the button properties) but when I run the macro the background color of the button remain gray :-(. It sounds that the new color is applied to the button boudary only or that there's someting in the foreground hiding the change I've done.

Is anybody faced the same problem? is it a known issue ?

Thanks in advance for your help.
Safway
Volunteer
Posts: 347
Joined: Thu Apr 24, 2008 4:35 pm

Re: Problem to change button background color in macro

Post by Safway »

I am not able to duplicate your problem. Here are the steps I took in trying to:
* Open Calc
* Ensured View | Toolbars | Form Controls is checked
* Click the "push button" icon in the form controls toolbar
* Place the push button somewhere in the spreadsheet
* Right-click on the push button and select "Control"
* Click the "General" tab
* Select the background color to something other than "Default" (I chose Yellow)
* Close the button control window
* Toggle the design mode off by clicking "Design Mode" in the form controls toolbar
* Clicked the button I created and noticed the color didn't change
* Recorded a simple macro
* Tools | Macro | Record Macro . . .
* Assigned the recorded macro to the button
* Ran the macro and noticed the color still didn't change.

Have you run this on another computer with a different monitor/video card? Maybe your screen resolution is poor.

Have you looked carefully at all the settings in the "General" tab? Maybe one of your settings is causing this to occur (such as a background color in addition to a conflicting graphics image).
If someone posts a fix for your question, then please post a quick thank you and then go to your first post, use the edit button to add [Solved] as the first word of the title so other people can rely on the fix as well.
LibreOffice 3.3.3 on Fedora
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Problem to change button background color in macro

Post by Villeroy »

May be you are interested in more powerful buttons.
 Edit: I just notice that serado69 refers to buttons on a dialog whereas Safway refers to buttons on form and I refer to a replacement for buttons on forms. Sorry, I'll try another answer. 
Attachments
HelloWorldFromCell.ods
cell buttons with arguments
(19.1 KiB) Downloaded 761 times
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Problem to change button background color in macro

Post by Villeroy »

This toggles the button's color:

Code: Select all

Sub toggleColor(oEv)
REM get model of calling button:
oModel = oEv.Source.getModel()
nColor = oModel.BackgroundColor
if Red(nColor) = 0 then
	nNew = RGB(255,0,0)
else
	nNew = RGB(0,255,0)
endif
oModel.BackgroundColor = nNew
End Sub
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Lasciatemi
Posts: 3
Joined: Mon Mar 12, 2012 10:20 pm

Re: Problem to change button background color in macro

Post by Lasciatemi »

Hi!

I have the same problem as serado69 had in 2008. Almost 4 years have passed but this thread seems to remain unsolved.
I'm afraid those who posted remarks on it may have misunderstood the problem a bit, so let me try to make it more clear.
I have made a dialog in OpenOffice Calc (Tools > Macros > Organize Macros > OpenOffice.org BASIC, then I click on the "Organize" button and in the "Standard" Library I create a new dialog). In the dialog I put buttons, listboxes, numeric fields, textboxes etc. (by choosing their icons in the Toolbar). Then I can change their properties by right clicking on them and choosing "Properties..." In the "General" tab you can change the background colour, as well. The changes seem to take effect as the background colours do change in the dialog editor. However, if you switch off the 'Test mode' by clicking on the "Test mode on/off" button, then the changes disappear. I have the same result if I execute the dialog with a macro like the following:

Sub TestDialog()
Dim dlgTest As Object

DialogLibraries.LoadLibrary("Standard")
dlgTest = CreateUnoDialog(DialogLibraries.Standard.Dialog1)

dlgTest.Execute()
End Sub

If I try to change the background colour from the macro, inserting the following line into it, which should change the background colour of the textbox into red, then this is also useless:

dlgTest.getControl("TextField1").Model.BackgroundColor = RGB(255, 0, 0)

I guess it is a bug in OpenOffice Calc. But as I didn't find any other forum topic about it, there may be a solution. Does anyone know about it?
Thanks,
Gabor
Attachments
The window on the left is the editor window, the window on the right is obtained when I switch off the Test mode.
The window on the left is the editor window, the window on the right is obtained when I switch off the Test mode.
Dialog.png (5.88 KiB) Viewed 22439 times
OpenOffice version: 3.2.0
Operating System: Ubuntu 10.04.4 LTS
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Problem to change button background color in macro

Post by B Marcelly »

Hi,
Works for me (Windows XP).
ToggleBtnColor.odt
(12.06 KiB) Downloaded 733 times
Probably another bug of the "Ubuntu improved" version.
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Problem to change button background color in macro

Post by Villeroy »

I guess it is a bug in OpenOffice Calc. But as I didn't find any other forum topic about it, there may be a solution. Does anyone know about it?
But my code works with any button you assign to to that code.
Why do you ignore my code?
Why don't you post your code?
Why don't you debug your variables? In particular: What is variable dlgTest outside of sub TestDialog?
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
JohnSUN-Pensioner
Volunteer
Posts: 876
Joined: Fri Jan 14, 2011 1:21 pm
Location: Kyiv, Ukraine

Re: Problem to change button background color in macro

Post by JohnSUN-Pensioner »

It really does not work as expected. Tested on both versions of my signature.
Painted only a thin border around the button, but the background is light gray.
Attachments
ToggleBtnColor.PNG
ToggleBtnColor.PNG (8.34 KiB) Viewed 22415 times
I may not have a lot to give but what I got I'll give to you...
Apache OpenOffice 4.1.5, LibreOffice 6.4.4.2 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Problem to change button background color in macro

Post by Villeroy »

I can not even set the color manually. The model colored in design mode but in the loaded dialog the button is grey.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
JohnSUN-Pensioner
Volunteer
Posts: 876
Joined: Fri Jan 14, 2011 1:21 pm
Location: Kyiv, Ukraine

Re: Problem to change button background color in macro

Post by JohnSUN-Pensioner »

I may not have a lot to give but what I got I'll give to you...
Apache OpenOffice 4.1.5, LibreOffice 6.4.4.2 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
Lasciatemi
Posts: 3
Joined: Mon Mar 12, 2012 10:20 pm

Re: Problem to change button background color in macro

Post by Lasciatemi »

Thanks for the replies.
Here at work I have OpenSuse 11.4 and LibreOffice 3.3.1. I don't know if regarding this topic there is a difference between LibreOffice and OpenOffice but I think there isn't. In any case, in the evening I will check it at home, too (where I have Ubuntu and OpenOffice, see my signature).
I tried both Villeroy's code and B Marcelly's attached file but neither of them worked for me. I got no error message but the background colour did not change when I clicked on the button. I observed, however, that if I keep moving the mouse over the button (without clicking) then the expected background colour sometimes flashes for a very very short time but then it remains grey again. For me not even the border colour changed.
A simple code that shows my problem is the following:

Sub TestDialog()
Dim dlgTest As Object
DialogLibraries.LoadLibrary("Standard")
dlgTest = CreateUnoDialog(DialogLibraries.Standard.Dialog1)
dlgTest.getControl("TextField1").Model.BackgroundColor = RGB(255, 0, 0)
dlgTest.Execute()
End Sub

Of course, first I need to create a dialog called "Dialog1" in the "Standard" library, which has a textbox called "TextField1". If I run this macro (e.g. by hitting F5 in the macro editor) then the dialog appears, in which the textbox should have a red background but it is grey.
The bug report, shown by JohnSUN-Pensioner seems to be about this topic but I think the problem is not only with buttons but also with textboxes and numeric fields.
Gabor
OpenOffice version: 3.2.0
Operating System: Ubuntu 10.04.4 LTS
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Problem to change button background color in macro

Post by Villeroy »

The bug report, shown by JohnSUN-Pensioner seems to be about this topic but I think the problem is not only with buttons but also with textboxes and numeric fields.
It fails only if the controls belong to a Basic dialog. It works with form controls on a document's draw page.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Lasciatemi
Posts: 3
Joined: Mon Mar 12, 2012 10:20 pm

Re: Problem to change button background color in macro

Post by Lasciatemi »

Hi!

I found the solution!
In the bug report, shown by JohnSUN-Pensioner, there is another link to a former bug report (Bug 40890). Reading it I found that you can swith off the "native look" by the following line:

dlgTest.getPeer().setProperty( "NativeWidgetLook", False )

After this line, if you modify the background colour of a button, numeric field or whatever (e.g. dlgTest.getControl("NumericField1").Model.BackgroundColor = RGB(255, 0, 0)), then it will take effect in the dialog after execution. :)
Best wishes,
Gabor
OpenOffice version: 3.2.0
Operating System: Ubuntu 10.04.4 LTS
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Problem to change button background color in macro

Post by Villeroy »

Lasciatemi wrote:dlgTest.getPeer().setProperty( "NativeWidgetLook", False )
Good catch! Thank you for finding this.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Tom Sequitur
Posts: 14
Joined: Wed Feb 03, 2016 1:50 am

Re: Problem to change button background color in macro

Post by Tom Sequitur »

Lasciatemi wrote:Hi!

I found the solution!
In the bug report, shown by JohnSUN-Pensioner, there is another link to a former bug report (Bug 40890). Reading it I found that you can swith off the "native look" by the following line:

dlgTest.getPeer().setProperty( "NativeWidgetLook", False )

After this line, if you modify the background colour of a button, numeric field or whatever (e.g. dlgTest.getControl("NumericField1").Model.BackgroundColor = RGB(255, 0, 0)), then it will take effect in the dialog after execution. :)
Best wishes,
Gabor
This bug is evidently still in existence after 8 YEARS. I was having the same issue with dialog controls. But I did notice one additional thing not mentioned here. You can set the color without the bug fix above if the text field is multi line input is set to Yes in the form designer. But if it is set to No then the background color set either in the form designer or programmatically at runtime, it just won't work without adding the line of code above. I thought after four years since this bug temp fix was introduced that someone ought to speak up and say it's still a problem. Gosh just adding a line to the online docs about this bug would be nice.
Tom Sequitur
OpenOffice 4.1.1 on LinuxMint 17.2
musikai
Volunteer
Posts: 294
Joined: Wed Nov 11, 2015 12:19 am

Re: Problem to change button background color in macro

Post by musikai »

Lasciatemi wrote:Hi!

I found the solution!
In the bug report, shown by JohnSUN-Pensioner, there is another link to a former bug report (Bug 40890). Reading it I found that you can swith off the "native look" by the following line:

dlgTest.getPeer().setProperty( "NativeWidgetLook", False )

After this line, if you modify the background colour of a button, numeric field or whatever (e.g. dlgTest.getControl("NumericField1").Model.BackgroundColor = RGB(255, 0, 0)), then it will take effect in the dialog after execution. :)
Best wishes,
Gabor
Million Thanks! This is the solution for my Libre- and OpenOffice-Dialogs in Linux. On Windows the BackgroundColor works right out of the box but in Linux only with this simple line. Thanks!!
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/li ... itect.html
Post Reply