Insert and Read Text in TextField Control Using LibreOffice Basic Macro

2 min


This tutorial will show how to default a string, insert a string at runtime, reading the string from a TextField control using Basic macro.

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 covered:

Macro Tutorial Index

TextField – How to Add in a Dialog

To add a TextField, click below icon and drag it to a new dialog.
TextField

How to read text from TextField

To read the text from TextField, use getText() method. It will return the input text as string from TextField.
Example:

oTextField = oDialog1.getControl("TextField1")
Msgbox oTextField.getText()

read text text field

How to insert or update the TextField text

During runtime, to update the contents of TextField, use setText() method.
Example:

oTextField = oDialog1.getControl("TextField1")
oTextField.setText("Hello World!!!")

text field set text

How to default a text in a TextField

There are two ways you can default a text in a TextField. During design you can set any default Text for a TextField using Text property. Type anything you want in Text property and it would be filled in TextBox.

Second option is to use setText() method while loading the dialog with its controls.

TextField default Text property
TextField default Text property

Function References – Used in this article

XTextComponent

Complete Macro

To run, Copy and paste this entire code block below in Macro Editor in LibreOffice. Create a dialog with a button and a text box and then assign the test() method to the button’s click event.
Read Text

Dim oDialog1 As Object
 
Sub StartDialog1()
    BasicLibraries.LoadLibrary("Tools")
    oDialog1 = LoadDialog("Standard", "Dialog1")
    oDialog1.Execute()
End Sub

Sub test()
	oTextField = oDialog1.getControl("TextField1")
	Msgbox oTextField.getText()
End Sub
 

Set Text

Dim oDialog1 As Object
 
Sub StartDialog1()
    BasicLibraries.LoadLibrary("Tools")
    oDialog1 = LoadDialog("Standard", "Dialog1")
	oDialog1.Execute()
End Sub

Sub test()
	oTextField = oDialog1.getControl("TextField1")
	oTextField.setText("Hello World!!!")
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:

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

LibreOffice Macro Tutorial Index


We bring the latest tech, software news and stuff that matters. Stay in touch via Telegram, Twitter, YouTube, and Facebook and never miss an update!

Join our Telegram channel and stay informed on the move.

Also Read


Like it? Share with your friends!

Arindam

Creator of debugpoint.com. All time Linux user and open-source supporter. Connect with me via Telegram, Twitter, LinkedIn, or send us an email.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x