Change CommandButton Look, Label, Length At Runtime Using Macro in Calc

Advertisement

This tutorial will show you how to change command button properties at run time.

Pre-requisite

This tutorial assumes that you know how to create a simple macro and a simple dialog. If you are unaware of the same, read below two tutorials before you proceed.

Using form controls
Using dialog controls

If you are looking for something else, head over to below link which contains all the LibreOffice macro tutorials list:

Macro Tutorial Index

Changing Properties

The button control properties can be changes via Properties window during dialog design. However, it is sometimes needed to change them during runtime as well.

Properties window where you can change button properties during dialog design.

prop

In this tutorial, we will try to do something like this:

Dialog Macro - Change Control Properties

Background Color

The background color of command button can be changed using BackgroundColor property of its Model. The model for the button can be accessed via getModel method of the control. The color can be specified via RGB() function.

oB1 = oDialog1.GetControl("CommandButton1")
with oB1.getModel
  .BackgroundColor=RGB(255,0,0)
End With

However, this is not sufficient to change the color because by default the LibreOffice dialogs adopts the Ubuntu/Linux desktop themes. The button colors remains same even if you execute above codes. To solves this, you have to turn off the NativeWidgetLook property of the entire dialog. Once you do that, you can change the look of the controls inside the dialog as per your needs. For our example, using below code you can turn it off.

 oDialog1.getPeer().setProperty( "NativeWidgetLook", False )
Change Looks

Change Looks

Size of Control

To change the height and width of button at runtime, use Height and Width property of the control. This can be accessed via Model of the control.

	oB1 = oDialog1.GetControl("CommandButton4")
	oB1.Model.Width = 63
	oB1.Model.Height = 27

Change Length

Change the Label

To change the label text of the button use Label property. The property can be accessed without the Model of the control.

oB1 = oDialog1.GetControl("CommandButton2")
oB1.Label ="Hello!!"

Change Label

Enable/Disable the control

To enable/disable the control use the Enable property.

	oB1 = oDialog1.GetControl("CommandButton3")
	oB1.Enable = False

Disable

You can put together all these functions in separate functions and assign them for each of the commandbuttons.

 

Complete Macro

REM  *****  BASIC  *****
' 
' LibreOffice Macro Tutorials 
' Change Button Properties at Runtime
' 
' www.debugpoint.com
' Author: Arindam
'

Dim oDialog1 As Object
 
Sub StartDialog1()
    oDialog1 = CreateUnoDialog(DialogLibraries.Standard.Dialog1)
    oDialog1.Execute()
End Sub
 
Sub Change_Look()
    oB1 = oDialog1.GetControl("CommandButton1")
    oDialog1.getPeer().setProperty( "NativeWidgetLook", False )
 	with oB1.getModel
		.BackgroundColor=RGB(255,0,0)
	End With
End Sub

Sub Change_Label()
	oB1 = oDialog1.GetControl("CommandButton2")
	oB1.Label ="Hello!!"
End Sub

Sub Disable_Button ()
	oB1 = oDialog1.GetControl("CommandButton3")
	oB1.Enable = False

End Sub

Sub Change_Size()
	oB1 = oDialog1.GetControl("CommandButton4")
	oB1.Model.Width = 63
	oB1.Model.Height = 27
End Sub

Looking for Something Else?

If you are looking for something else in LibreOffice macro tutorials, Or, wants to learn more about it, please follow below link for complete Macro Tutorials Index:

LibreOffice Macro Tutorial Index

You may also like...

Translate »