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

Creating a macro - Writing a Script - Using the API

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

Postby zenira » Thu Mar 30, 2017 9:16 pm

Hi friends
I want to add data into ListBox from TextBox in a dialog box but I can not
Image
Code: Select all   Expand viewCollapse view
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
zenira
 
Posts: 85
Joined: Sun May 11, 2014 11:24 am

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

Postby JeJe » Thu Mar 30, 2017 9:48 pm

textbox.model.text
Openoffice 4.1.2
Windows 8
JeJe
 
Posts: 114
Joined: Wed Mar 09, 2016 2:40 pm

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

Postby zenira » Fri Mar 31, 2017 8:00 am

it does not work :crazy:
LibreOffice 4.1.0.4 on Windows 7
zenira
 
Posts: 85
Joined: Sun May 11, 2014 11:24 am

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

Postby JeJe » Fri Mar 31, 2017 10:40 am

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

Code: Select all   Expand viewCollapse view
dim textbox,listbox as object
textbox=oDlg.getControl("TextField1")
listbox=oDlg.getControl("ListBox1")
listbox.additem(textbox.model.Text, 0)
Openoffice 4.1.2
Windows 8
JeJe
 
Posts: 114
Joined: Wed Mar 09, 2016 2:40 pm

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

Postby zenira » Fri Mar 31, 2017 11:53 am

I did. But it does not work
LibreOffice 4.1.0.4 on Windows 7
zenira
 
Posts: 85
Joined: Sun May 11, 2014 11:24 am

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

Postby Villeroy » Fri Mar 31, 2017 12:14 pm

Works for me.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 16.04, OpenOffice 4.x & LibreOffice 5.x
User avatar
Villeroy
Volunteer
 
Posts: 23980
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

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

Postby JeJe » Fri Mar 31, 2017 12:29 pm

Works for me as well. I've attached a writer document with it.
Attachments
Untitled 1.odt
(10.79 KiB) Downloaded 29 times
Openoffice 4.1.2
Windows 8
JeJe
 
Posts: 114
Joined: Wed Mar 09, 2016 2:40 pm

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

Postby Villeroy » Fri Mar 31, 2017 12:57 pm

A combo box is a combo box becase it combines a text box with a list box.
Attachments
combo.odt
(11.25 KiB) Downloaded 29 times
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 16.04, OpenOffice 4.x & LibreOffice 5.x
User avatar
Villeroy
Volunteer
 
Posts: 23980
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

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

Postby RoryOF » Fri Mar 31, 2017 5:42 pm

Apache OpenOffice 4.1.3 on Xubuntu 16.04 (mostly 64 bit version) and infrequently on Win2K/XP
14 October 2016 was Pooh's 90th birthday
User avatar
RoryOF
Moderator
 
Posts: 24552
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

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

Postby zenira » Sat Apr 01, 2017 10:22 am

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   Expand viewCollapse view
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   Expand viewCollapse view
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
zenira
 
Posts: 85
Joined: Sun May 11, 2014 11:24 am

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

Postby Villeroy » Sat Apr 01, 2017 10:24 am

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 16.04, OpenOffice 4.x & LibreOffice 5.x
User avatar
Villeroy
Volunteer
 
Posts: 23980
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

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

Postby JeJe » Sat Apr 01, 2017 10:53 am

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   Expand viewCollapse view
Option Explicit


at the top of your modules so every variable has to be declared.
Openoffice 4.1.2
Windows 8
JeJe
 
Posts: 114
Joined: Wed Mar 09, 2016 2:40 pm

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

Postby zenira » Sat Apr 01, 2017 10:15 pm

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   Expand viewCollapse view
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
zenira
 
Posts: 85
Joined: Sun May 11, 2014 11:24 am

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

Postby RoryOF » Sat Apr 01, 2017 10:20 pm

You need to read up on "Scope and Life Span of Variables" in the OpenOffice BASIC Guide.
Apache OpenOffice 4.1.3 on Xubuntu 16.04 (mostly 64 bit version) and infrequently on Win2K/XP
14 October 2016 was Pooh's 90th birthday
User avatar
RoryOF
Moderator
 
Posts: 24552
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

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

Postby JeJe » Sat Apr 01, 2017 10:34 pm

Your last code works fine here in OpenOffice if you change it to textbox.Model.Text.
Openoffice 4.1.2
Windows 8
JeJe
 
Posts: 114
Joined: Wed Mar 09, 2016 2:40 pm

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

Postby zenira » Sat Apr 01, 2017 10:38 pm

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: 85
Joined: Sun May 11, 2014 11:24 am

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

Postby zenira » Sat Apr 01, 2017 10:41 pm

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
zenira
 
Posts: 85
Joined: Sun May 11, 2014 11:24 am

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

Postby UnklDonald418 » Sat Apr 01, 2017 10:54 pm

Your declaration isn't correct. Replace
Code: Select all   Expand viewCollapse view
dim oDlg,textbox,listbox as object

with
Code: Select all   Expand viewCollapse view
Public oDlg as object

then
Code: Select all   Expand viewCollapse view
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 subject line
Apache OpenOffice 4.1.3 - Windows 10 Professional
UnklDonald418
Volunteer
 
Posts: 502
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

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

Postby JeJe » Sat Apr 01, 2017 10:59 pm

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

Try a non-modal dialog

https://forum.openoffice.org/en/forum/v ... =20&t=5815

Try posting it for people to look at
Openoffice 4.1.2
Windows 8
JeJe
 
Posts: 114
Joined: Wed Mar 09, 2016 2:40 pm

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

Postby zenira » Sat Apr 01, 2017 11:07 pm

UnklDonald418 wrote:Your declaration isn't correct. Replace
Code: Select all   Expand viewCollapse view
dim oDlg,textbox,listbox as object

with
Code: Select all   Expand viewCollapse view
Public oDlg as object

then
Code: Select all   Expand viewCollapse view
oDlg.execute()
textbox=oDlg.getControl("TextField1")
listbox=oDlg.getControl("ListBox1")

should return the values you entered

Here is fresh code
Code: Select all   Expand viewCollapse view
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
zenira
 
Posts: 85
Joined: Sun May 11, 2014 11:24 am

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

Postby JeJe » Sat Apr 01, 2017 11:12 pm

Code: Select all   Expand viewCollapse view
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.
Openoffice 4.1.2
Windows 8
JeJe
 
Posts: 114
Joined: Wed Mar 09, 2016 2:40 pm

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

Postby zenira » Sat Apr 01, 2017 11:34 pm

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

Try a non-modal dialog

https://forum.openoffice.org/en/forum/v ... =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   Expand viewCollapse view
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: 85
Joined: Sun May 11, 2014 11:24 am

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

Postby zenira » Sat Apr 01, 2017 11:53 pm

I tried non-modal dialog code
Code: Select all   Expand viewCollapse view
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
zenira
 
Posts: 85
Joined: Sun May 11, 2014 11:24 am

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

Postby JeJe » Sun Apr 02, 2017 12:19 am

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

Its TextBox.Model.text or TextBox.getText
Openoffice 4.1.2
Windows 8
JeJe
 
Posts: 114
Joined: Wed Mar 09, 2016 2:40 pm

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

Postby zenira » Sun Apr 02, 2017 12:41 am

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
zenira
 
Posts: 85
Joined: Sun May 11, 2014 11:24 am

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

Postby UnklDonald418 » Sun Apr 02, 2017 3:27 pm

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   Expand viewCollapse view
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 subject line
Apache OpenOffice 4.1.3 - Windows 10 Professional
UnklDonald418
Volunteer
 
Posts: 502
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

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

Postby zenira » Sun Apr 02, 2017 6:48 pm

Lastest code but it is not work
Attachments
DialogExample.odt
example
(10.7 KiB) Downloaded 35 times
LibreOffice 4.1.0.4 on Windows 7
zenira
 
Posts: 85
Joined: Sun May 11, 2014 11:24 am

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

Postby UnklDonald418 » Sun Apr 02, 2017 7:49 pm

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 subject line
Apache OpenOffice 4.1.3 - Windows 10 Professional
UnklDonald418
Volunteer
 
Posts: 502
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

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

Postby zenira » Sun Apr 02, 2017 8:16 pm

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
zenira
 
Posts: 85
Joined: Sun May 11, 2014 11:24 am

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

Postby Villeroy » Sun Apr 02, 2017 10:46 pm

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 16.04, OpenOffice 4.x & LibreOffice 5.x
User avatar
Villeroy
Volunteer
 
Posts: 23980
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany


Return to Macros and UNO API

Who is online

Users browsing this forum: Lupp and 9 guests