[Solved] I can't add data into ListBox in runtime

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
zenira
Posts: 88
Joined: Sun May 11, 2014 11:24 am

[Solved] I can't add data into ListBox in runtime

Post by zenira »

Hi friends
I want to add data into ListBox from TextBox in a dialog box but I can not
Image

Code: Select all

Sub Main
DialogLibraries.LoadLibrary("Standard")
oDlg = CreateUnoDialog(DialogLibraries.Standard.Dialog1 )
oDlg.execute()
End Sub
Sub AddButton
dim textbox,listbox as object
textbox=oDlg.getControl(TextField1)
listbox=oDlg.getControl(ListBox1)
listbox.additem(textbox.Text, 0) REM  this does not work
End Sub
when i add this controls on document they are work but they dont work on dialog box
thanks for your help
Last edited by zenira on Sun Apr 02, 2017 8:30 pm, edited 2 times in total.
LibreOffice 4.1.0.4 on Windows 7
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: I can't add data into ListBox in runtime

Post by JeJe »

textbox.model.text
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
zenira
Posts: 88
Joined: Sun May 11, 2014 11:24 am

Re: I can't add data into ListBox in runtime

Post by zenira »

it does not work :crazy:
LibreOffice 4.1.0.4 on Windows 7
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: I can't add data into ListBox in runtime

Post by JeJe »

You also need to put quotes round the name of the controls

Code: Select all

dim textbox,listbox as object
textbox=oDlg.getControl("TextField1")
listbox=oDlg.getControl("ListBox1")
listbox.additem(textbox.model.Text, 0) 
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
zenira
Posts: 88
Joined: Sun May 11, 2014 11:24 am

Re: I can't add data into ListBox in runtime

Post by zenira »

I did. But it does not work
LibreOffice 4.1.0.4 on Windows 7
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: I can't add data into ListBox in runtime

Post by Villeroy »

Works for me.
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
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: I can't add data into ListBox in runtime

Post by JeJe »

Works for me as well. I've attached a writer document with it.
Attachments
Untitled 1.odt
(10.79 KiB) Downloaded 177 times
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: I can't add data into ListBox in runtime

Post by Villeroy »

A combo box is a combo box becase it combines a text box with a list box.
Attachments
combo.odt
(11.25 KiB) Downloaded 182 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
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: I can't add data into ListBox in runtime

Post by RoryOF »

Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
zenira
Posts: 88
Joined: Sun May 11, 2014 11:24 am

Re: I can't add data into ListBox in runtime

Post by zenira »

ok. I will try your advices. Thank your help
zenira wrote:Hi friends
I want to add data into ListBox from TextBox in a dialog box but I can not

Code: Select all

Sub Main
DialogLibraries.LoadLibrary("Standard")
oDlg = CreateUnoDialog(DialogLibraries.Standard.Dialog1 )
oDlg.execute()
End Sub
Sub AddButton
dim textbox,listbox as object
textbox=oDlg.getControl(TextField1)
listbox=oDlg.getControl(ListBox1)
listbox.additem(textbox.Text, 0) REM  this does not work
End Sub
when i add this controls on document they are work but they dont work on dialog box
thanks for your help
when i click Add button, i get this message "BASIC runtime error.Object variable not set"
The Code can't recognize "oDlg" object when I click Add Button
so i change code like this

Code: Select all

dim oDlg as object
Sub Main
DialogLibraries.LoadLibrary("Standard")
oDlg = CreateUnoDialog(DialogLibraries.Standard.Dialog1 )
oDlg.execute()
End Sub
Sub AddButton
dim textbox,listbox as object
oDlg = CreateUnoDialog(DialogLibraries.Standard.Dialog1 )
textbox=oDlg.getControl("TextField1")
listbox=oDlg.getControl("ListBox1")
listbox.additem(textbox.Text, 0) REM  this does not work
End Sub
in this code, I don't get any error but again it does not work
LibreOffice 4.1.0.4 on Windows 7
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: I can't add data into ListBox in runtime

Post by Villeroy »

You can not do this without minimal programming skills. You've got to understand the error messages and the code.
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
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: I can't add data into ListBox in runtime

Post by JeJe »

Did you download the document I posted with it in? Did it work? If yes... and I bet it did... you got the solution...
dim oDlg as object
Sub Main
DialogLibraries.LoadLibrary("Standard")
oDlg = CreateUnoDialog(DialogLibraries.Standard.Dialog1 )
oDlg.execute()
End Sub
Sub AddButton
dim textbox,listbox as object
oDlg = CreateUnoDialog(DialogLibraries.Standard.Dialog1 )
textbox=oDlg.getControl("TextField1")
listbox=oDlg.getControl("ListBox1")
listbox.additem(textbox.Text, 0) REM this does not work
End Sub
You still haven't changed textbox.Text to textbox.Model.Text

It also won't work because in your AddButton function you create a new dialog oDlg which you don't show.

Start putting

Code: Select all

Option Explicit
at the top of your modules so every variable has to be declared.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
zenira
Posts: 88
Joined: Sun May 11, 2014 11:24 am

Re: I can't add data into ListBox in runtime

Post by zenira »

Thank you guys for your help. I did everything, I spend hours about this issue :crazy: . I changed my code. Simply I want to get textbox's text but I get "Object variable not set". :idea:
I realize that the code works on calc document but it does not work writer document. Same code.

Code: Select all

Option Explicit
dim oDlg,textbox,listbox as object
Sub Main
DialogLibraries.LoadLibrary("Standard")
oDlg = CreateUnoDialog(DialogLibraries.Standard.Dialog1 )
textbox=oDlg.getControl("TextField1")
listbox=oDlg.getControl("ListBox1")
oDlg.execute()
End Sub

Sub AddButton
Dim stext as string
sText = textbox.Model.getText() Error: Object variable not set
listbox.additem(sText, 0) 
End Sub
I think that after oDlg.execute(), all of variables relases in memory and code does not know textbox in AddButton sub despite I define in public :alarm:
LibreOffice 4.1.0.4 on Windows 7
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: I can't add data into ListBox in runtime

Post by RoryOF »

You need to read up on "Scope and Life Span of Variables" in the OpenOffice BASIC Guide.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: I can't add data into ListBox in runtime

Post by JeJe »

Your last code works fine here in OpenOffice if you change it to textbox.Model.Text.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
zenira
Posts: 88
Joined: Sun May 11, 2014 11:24 am

Re: I can't add data into ListBox in runtime

Post by zenira »

RoryOF wrote:You need to read up on "Scope and Life Span of Variables" in the OpenOffice BASIC Guide.
Yes I did.
LibreOffice 4.1.0.4 on Windows 7
zenira
Posts: 88
Joined: Sun May 11, 2014 11:24 am

Re: I can't add data into ListBox in runtime

Post by zenira »

JeJe wrote:Your last code works fine here in OpenOffice if you change it to textbox.Model.Text.
I run it in LibreOffice 4.2.1.1
it works in calc document but does not work in writer document.
LibreOffice 4.1.0.4 on Windows 7
UnklDonald418
Volunteer
Posts: 1544
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: I can't add data into ListBox in runtime

Post by UnklDonald418 »

Your declaration isn't correct. Replace

Code: Select all

dim oDlg,textbox,listbox as object 
with

Code: Select all

Public oDlg as object
then

Code: Select all

textbox=oDlg.getControl("TextField1")
listbox=oDlg.getControl("ListBox1")
oDlg.execute()
should return the values you entered
Last edited by UnklDonald418 on Sat Apr 01, 2017 11:05 pm, edited 1 time in total.
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: I can't add data into ListBox in runtime

Post by JeJe »

Try changing the names of all your variables and controls and subs.

Try a non-modal dialog

viewtopic.php?f=20&t=5815

Try posting it for people to look at
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
zenira
Posts: 88
Joined: Sun May 11, 2014 11:24 am

Re: I can't add data into ListBox in runtime

Post by zenira »

UnklDonald418 wrote:Your declaration isn't correct. Replace

Code: Select all

dim oDlg,textbox,listbox as object 
with

Code: Select all

Public oDlg as object
then

Code: Select all

oDlg.execute()
textbox=oDlg.getControl("TextField1")
listbox=oDlg.getControl("ListBox1")
should return the values you entered
Here is fresh code

Code: Select all

Option Explicit
Public oDlg,textbox,listbox as object

Sub Main
DialogLibraries.LoadLibrary("Standard")
oDlg = CreateUnoDialog(DialogLibraries.Standard.Dialog1 )
oDlg.execute()
textbox=oDlg.getControl("TextField1")
listbox=oDlg.getControl("ListBox1")
End Sub

Sub AddButton
Dim stext as string
sText = CStr(textbox.Model.getText()) 'Error: Object variable not set
listbox.additem(sText, 0) 
End Sub
It does not work :crazy:
LibreOffice 4.1.0.4 on Windows 7
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: I can't add data into ListBox in runtime

Post by JeJe »

Code: Select all

Sub Main
DialogLibraries.LoadLibrary("Standard")
oDlg = CreateUnoDialog(DialogLibraries.Standard.Dialog1 )
oDlg.execute()
textbox=oDlg.getControl("TextField1")
listbox=oDlg.getControl("ListBox1")
End Sub

Sub AddButton
Dim stext as string
sText = CStr(textbox.Model.getText()) 'Error: Object variable not set
listbox.additem(sText, 0)
End Sub
That isn't going to work because the Sub Main doesn't run any of the code following the oDlg.execute line until the dialog is closed.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
zenira
Posts: 88
Joined: Sun May 11, 2014 11:24 am

Re: I can't add data into ListBox in runtime

Post by zenira »

JeJe wrote:Try changing the names of all your variables and controls and subs.

Try a non-modal dialog

viewtopic.php?f=20&t=5815

Try posting it for people to look at
I changed names of variables ,controls and subs ..here is fresh code

Code: Select all

Option Explicit
Public Window as object
Public EnterBox,SaveBox as object

Sub Main
End Sub

Sub Run
	DialogLibraries.LoadLibrary("Standard")
	Window = CreateUnoDialog(DialogLibraries.Standard.Dg1 
	EnterBox=Window.getControl("EnterBoxControl")
	SaveBox=Window.getControl("SaveBoxControl")
	Window.execute()
End Sub

Sub AddingButton
	Dim textarea as string
	textarea = EnterBox.Model.getText() 'Error: Object variable not set
	SaveBox.additem(textarea, 0) 
End Sub
it does not again :crazy:
ok I will try non-modal dialog . thank you
LibreOffice 4.1.0.4 on Windows 7
zenira
Posts: 88
Joined: Sun May 11, 2014 11:24 am

Re: I can't add data into ListBox in runtime

Post by zenira »

I tried non-modal dialog code

Code: Select all

Option Explicit
Global g_Dlg as object
Global g_Stop as Boolean
Global TextBox,ListBox as object
Sub Main
	DialogLibraries.LoadLibrary("Standard")
	g_Dlg = CreateUnoDialog(DialogLibraries.Standard.Dg2
	TextBox=g_Dlg.getControl("TextField1")
	ListBox=g_Dlg.getControl("ListBox1")
	g_Dlg.setVisible(True)
	do until g_Stop
   		wait 1000
	loop
	g_Dlg.setVisible(False)
end sub

Sub someEvent()
   g_Stop = True
End Sub

Sub AddingButton
	Dim textarea as string
	textarea = TextBox.Model.getText() 'Error: Object variable not set
	ListBox.additem(textarea, 0) 
End Sub
:crazy: It does not work
LibreOffice 4.1.0.4 on Windows 7
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: I can't add data into ListBox in runtime

Post by JeJe »

Why do you keep changing it to TextBox.Model.getText?

Its TextBox.Model.text or TextBox.getText
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
zenira
Posts: 88
Joined: Sun May 11, 2014 11:24 am

Re: I can't add data into ListBox in runtime

Post by zenira »

JeJe wrote:Why do you keep changing it to TextBox.Model.getText?

Its TextBox.Model.text or TextBox.getText
I tried both. it does not work :crazy: :crazy: :crazy:
LibreOffice 4.1.0.4 on Windows 7
UnklDonald418
Volunteer
Posts: 1544
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: I can't add data into ListBox in runtime

Post by UnklDonald418 »

This morning using the IDE I set a break point on the offending line in the AddingButton and a watch on EnterBox. The watch revealed that EnterBox was returning a Null value despite being declared Public. It then occurred to me that of course it's Null because the assignment was made before the Dialog was executed. So …

Code: Select all

Sub AddingButton

   Dim textarea as string
    
   EnterBox=oDlg.getControl("TextField1")
   SaveBox=oDlg.getControl("ListBox1")  
   textarea = EnterBox.Text   
   SaveBox.additem(textarea, SaveBox.getItemCount)
   EnterBox.Text = ""
End Sub
I added a couple of things.
additem(textarea,0) inserts the new entry at the top of the list, additem(textarea, SaveBox.getItemCount) adds it to the bottom of the list.
Also the last line clears the Text Box, ready for a new entry.
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
zenira
Posts: 88
Joined: Sun May 11, 2014 11:24 am

Re: I can't add data into ListBox in runtime

Post by zenira »

Lastest code but it is not work
Attachments
DialogExample.odt
example
(10.7 KiB) Downloaded 190 times
LibreOffice 4.1.0.4 on Windows 7
UnklDonald418
Volunteer
Posts: 1544
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: I can't add data into ListBox in runtime

Post by UnklDonald418 »

I downloaded your example.
When I run the dialog and type something into the text box and press the Add button that string appears in the list box.
What doesn't work?
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
zenira
Posts: 88
Joined: Sun May 11, 2014 11:24 am

[Solved]I can't add data into ListBox in runtime

Post by zenira »

Finally it work... :bravo: :super:
I notice where is fault. When i run it on IDE clicking "Run Basic", it does not run.I get this message
Image
But I add a Run button on page and assign it Sub Main(), it works
Image
it works now
Image
Thank you guys, specially UnklDonald418,JeJe,RoryOF,Villeroy. thank you advices. :bravo:
LibreOffice 4.1.0.4 on Windows 7
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved]I can't add data into ListBox in runtime

Post by Villeroy »

GlobalScope.DialogLibraries.loadLibrary("Standard") [global library]
DialogLibraries.loadLibrary("Standard") [embedded library]
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
Post Reply