Bug 134232 - Improve the Help for reading and updating control object contents (see comment 3)
Summary: Improve the Help for reading and updating control object contents (see commen...
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Documentation (show other bugs)
Version:
(earliest affected)
6.4.4.2 release
Hardware: x86-64 (AMD64) Windows (All)
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-06-22 15:07 UTC by TFKane
Modified: 2021-06-03 15:21 UTC (History)
4 users (show)

See Also:
Crash report or crash signature:


Attachments
Bug 134232, Dialog and Module (1.03 KB, application/octet-stream)
2020-07-16 21:33 UTC, TFKane
Details

Note You need to log in before you can comment on or make changes to this bug.
Description TFKane 2020-06-22 15:07:09 UTC
Description:
Given this code snippet:

  Dim oDialog1 As Object
  Dim oText As Object
  BasicLibraries.LoadLibrary("Tools")
  oDialog1 = LoadDialog("Standard", "MyDialog")
  oText = oDialog1.getControl("MyTextBox")
  oText.Text = "Sample Text"
  msgbox  oText.Text

The "Sample Text" assigned to oText.Text is properly displayed by the msgbox statement.
Despite this, the "MyDialog" (a dialog, of course) doesn't update the corresponding TextBox.


Steps to Reproduce:
1.create a MyDialog with MyTextBox and a Button
2.on button click, call a Sub with code snippet


Actual Results:
The assigned text isn't displayed by the TextBox

Expected Results:
The assigned text must be displayed by the TextBox


Reproducible: Always


User Profile Reset: No



Additional Info:
Versione: 6.4.4.2 (x64)
Build ID: 3d775be2011f3886db32dfd395a6a6d1ca2630ff
Thread CPU: 4; SO: Windows 6.1 Service Pack 1 Build 7601; Resa interfaccia: predefinito; VCL: win; 
Versione locale: it-IT (it_IT); Lingua interfaccia: it-IT
Calc: CL
--
By sure the bug is also present on 6.0.x, but I'm not sure about "x".
I reproduced it on the last version 6.4.4.2 (x64) too, installed hoping to fix.
Comment 1 Xisco Faulí 2020-07-13 14:39:37 UTC
Thank you for reporting the bug. Please attach a sample document, as this makes it easier for us to verify the bug. 
I have set the bug's status to 'NEEDINFO'. Please change it back to 'UNCONFIRMED' once the requested document is provided.
(Please note that the attachment will be public, remove any sensitive information before attaching it. 
See https://wiki.documentfoundation.org/QA/FAQ#How_can_I_eliminate_confidential_data_from_a_sample_document.3F for help on how to do so.)
Comment 2 TFKane 2020-07-16 21:33:26 UTC
Created attachment 163140 [details]
Bug 134232, Dialog and Module
Comment 3 TFKane 2020-07-16 21:35:27 UTC
Hi Xisco,


I made a mistake.


Hoping the following details allows to avoid other users to made the same mistake.


To make long story short, there are two way to read and update controls contents (e.g., TextField):

1) read/update controls content immediately before the dialog is shown:

Sub ShowDialog134232
    BasicLibraries.LoadLibrary("Tools")
    oDialog134232 = LoadDialog("Standard", "Dialog134232")
	oText  = oDialog134232.getControl("MyTextBox")
	msgbox "updating <"+oText.Text+"> ... (will succeed)"
	oText.Text = "Bug 134232 ... updated text"
	oDialog134232.Execute()
End Sub


2) read/update controls content on user interaction (e.g. button click):

Sub ButtonClicked_ProperWay(Event as Object)
	Button = Event.Source()
	Context = Button.getContext()
	oText = Context.getControl("MyTextBox")
	msgbox "updating <"+oText.Text+"> ... (will succeed)"
	oText.Text = "it works"
End Sub


To make long story long, keep reading.


MODULE AND DIALOG DESCRIPTION


The attached module (Module134232) and form (Dialog134232) will explain both the flaw and the proper way to proceed.


Dialog134232 has three controls:

- MyTextBox: a textbox with preassigned text "Bug 134232"

- WrongWayButton: a button labelled with the text "Update Text - Wrong Way"

- ProperWayButton: a button labelled with the text "Update Text - Proper Way"



Module134232 has three subs:

- ShowDialog134232: to show dialog and allow user to interact with it

- ButtonClicked_WrongWay: called on the WrongWayButton button click

- ButtonClicked_ProperWay(Event as Object): called on the ProperWayButton button click

All subs pops up a message with the current text and the message that the text is going to be changed



STEP BY STEP USAGE

Load module and dialog in LibreOffice Calc.

Then, choose Tools -> Macros -> Run Macro

"Macro Selector"  dialog will be shown.

Select MyMacros -> Standard -> Module134232 in the "Library" tree on the left side, then choose ShowDialog134232 in the "Macro Name" list on the right side.
Click Run.

A message box with the text "updating <Bug 134232> .... (will succeed)" will be shown.
Click "OK".
The message box disappears and the Dialog134232 become visible.
The text in the MyTextBox text field "Bug 134232" has actually been replaced with "Bug 134232 ... updated text".

Click the "Update Text - Wrong Way" button.
A message box with the text "updating <Bug 134232> .... (will fail)" will be shown.
Click "Ok".
The message box disappear and the text field remains unchanged.

Click the "Update Text - Proper Way" button.
A message box with the text "updating <Bug 134232 ... updated text> .... (will succeed)" will be shown.
Click "Ok".
The message box disappear and the text field is replaced with "it works".


CONCLUSIONS

Essentially, my mistake - at least as far as I can understand - was the attempt to update fields on a button click in this way (ButtonClicked_WrongWay is assigned to button "Execute action" event):

Sub ButtonClicked_WrongWay
    BasicLibraries.LoadLibrary("Tools")
    oDialog134232 = LoadDialog("Standard", "Dialog134232")
	oText  = oDialog134232.getControl("MyTextBox")
	msgbox "updating <"+oText.Text+"> ... (will fail)"
	oText.Text = "this won't be displayed"
End Sub

The Sub body actually create a new Dialog instance and interact with it, leaving unchanged the displayed one.


The right way to do this, is:

Sub ButtonClicked_ProperWay(Event as Object)
	Button = Event.Source()
	Context = Button.getContext()
	oText = Context.getControl("MyTextBox")
	msgbox "updating <"+oText.Text+"> ... (will succeed)"
	oText.Text = "it works"
End Sub

However, I was unable to find any documentation that illustrated the above.
Indeed, I had to guess the the "Event as Object" parameter existance.

IMO, the LibreOffice Basic documentation should be integrated with a more accurate API documentation and possibly many examples. At least, in my experience.

Hoping this post was helpful.
Comment 4 Buovjaga 2021-06-03 15:21:31 UTC
(In reply to TFKane from comment #3)
> The right way to do this, is:
> 
> Sub ButtonClicked_ProperWay(Event as Object)
> 	Button = Event.Source()
> 	Context = Button.getContext()
> 	oText = Context.getControl("MyTextBox")
> 	msgbox "updating <"+oText.Text+"> ... (will succeed)"
> 	oText.Text = "it works"
> End Sub
> 
> However, I was unable to find any documentation that illustrated the above.
> Indeed, I had to guess the the "Event as Object" parameter existance.
> 
> IMO, the LibreOffice Basic documentation should be integrated with a more
> accurate API documentation and possibly many examples. At least, in my
> experience.
> 
> Hoping this post was helpful.

Ok, let's turn this into a documentation issue