Change color of button in toggle mode

Hello.

I use a SMALLINT type column at 1 or 0 to set the status of an invoice (valid or cancelled).

	INV_STATUS SMALLINT DEFAULT 1 NOT NULL,		-- valid (1) or cancelled (0)
	CONSTRAINT CC_INV_STATUS CHECK (VAC_STATUS BETWEEN 0 AND 1)

In the form, rather than a checkbox or listbox, I inserted a button with toggle mode.

How do I set 2 colors (eg green for 1 and red for 0), for the button based on its state?

Thanks.

or maybe something like this:

Sub ButtonColour(oEv as object)
	'toggle button background colour
	'fired by button 'Execute action' event
	oButton = oEv.source.model
	if oButton.state = 1 then
		oButton.backgroundcolor = 432686
	else
		oButton.backgroundcolor = 16711680
	end if
End Sub

This doesn’t work. The macro is not defined when the form is loaded and crashes if I do.

I still tried it on clicking the button with these colors.

Sub ButtonColour(oEv as object)

	Dim oButton As Oject	

	'toggle button background colour
	'fired by button 'Execute action' event

	oButton = oEv.source.model
	if oButton.state = 1 then
		oButton.backgroundcolor = "77BC65"
	else
		oButton.backgroundcolor = "FF6D6D"
	end if
End Sub

As soon as I click once, the button turns black and stays.

@Primus,

Version: 7.3.7.2 (x64) / LibreOffice Community
Build ID: e114eadc50a9ff8d8c8a0567d6da8f454beeb84f
CPU threads: 8; OS: Windows 10.0 Build 19045; UI render: Skia/Raster; VCL: win
Locale: en-GB (en_GB); UI: en-GB
Calc: CL
.
if you wish to use hex then please use it properly.
I have no idea what you are doing so here is a working example.
.
2 forms:
‘fButtonOnly’ uses the code which you said does not work.
‘fInvStatus’ does what you ask.
.
we do not require the constraint.
INV_STATUS has a default so should be nullable in order to avoid system errors and form seizure.
I used hex here to show how it’s done.
ButtonColour.odb (22.1 KB)

That’s ok. Your ToggleColorSimple macro does exactly what I expected.

I prefer to avoid Boolean columns. I don’t know why, but when the table is large (in columns and rows), the Boolean make the scrolling rampant and slow down the navigation in the forms. So I prefer a SMALLINT with 1 or 0 (the constraint) and NOT NULL.

Thanks. :slightly_smiling_face:

I never write code for Base, but the handling of FormControl objects should always be the same.
If I want to work with such objects, I often pass needed arguments via a query string assigned to the .Tag property (“Additional Information” in the UI). The evaluation can in this case be simplified, and done without helper functions. There may be much more elegant solutions I don’t know of.
See attached example residing in a spreadsheet:
disask86307toggleButtonColorLabel.ods (12.1 KB)

Yes, perfect, but unsuitable for Base with this code.

I feel sure you’re eager to tell me about the specific problem.
Do not hesitate to do so.

Where need the UserCode you are using with Base to be stored?
(@Villeroy may also intervene here out of compassion. After all he knows that I am not familiar with any usage of Base, and least with custom programming for it. I ask for his consideration because of my curiosity.)

The form controls on a Writer/Calc/Draw/Impress layer are exactly the same as the buttons on a Base form. A Base form is nothing but a Writer document embedded in the Base document. So the color change should work with Lupp’s code.
Regarding the value binding, I would abstain from toggle buttons. Buttons have not value binding, and it would be rather complicated to implement. We have check boxes, listboxes and option buttons at hand. They change their status by value binding without a single line of macro code.

1 Like

I know I’ve made it a little complicated with a toggle button for aesthetic goal.

I forgot to set the default color of the button. And in doing so, there is indeed no need to load the macro when opening the form.