[Solved] Unable to select item from a dynamic dialogbox

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

[Solved] Unable to select item from a dynamic dialogbox

Post by Mr.Dandy »

Hello forum,

This code displays a dialogbox with a listbox:

Code: Select all

REM  *****  BASIC  *****
Sub Main
	GlobalScope.BasicLibraries.LoadLibrary("Tools")
	GlobalScope.BasicLibraries.LoadLibrary("XrayTool")
	DialogLibraries.LoadLibrary("Standard")	
	oLib = DialogLibraries.GetByName("Standard")
	oDlg = Dlg_Create( 80, 80, 120, 40, "Dynamic dialog" )
	aContent = array("Albert", "Bernard", "Charly")
	Dlg_Listbox(oDlg, 10, 10, 100, 12, 1, "myList", aContent, "myList")
	oDlg.setVisible(true)
	oDlg.execute()
End Sub

Function Dlg_Create( x As Long, y As Long, w As Long, h As Long, cTitle As String )
   oModel = createUnoService( "com.sun.star.awt.UnoControlDialogModel" )
   With oModel
		.PositionX = x
		.PositionY = y
		.Width = w
		.Height = h
		.Title = cTitle
	End with
	
	oCtrl = createUnoService( "com.sun.star.awt.UnoControlDialog" )
	oCtrl.setModel( oModel )
	Dlg_Create = oCtrl
End function

Sub Dlg_Listbox(oDlg as object, x As Long, y As Long, w As Long, h As Long, _
				nIndex as long, cName As String, _
                content As Array, cListener As String )
     
	oModel = oDlg.Model.createInstance( "com.sun.star.awt.UnoControlListBoxModel" )
	With oModel
		.PositionX = x
		.PositionY = y
		.Width = w
		.Height = h
		.Name = cName
		.TabIndex = nIndex
		.DropDown = 1
	End with
   
   oDlg.Model.insertByName( cName, oModel )
   oCtrl = oDlg.getControl( cName )
   
   oCtrl.addItems( content(), 0)
   oCtrl.selectItemPos(1, true)

   oActionListener = CreateUnoListener( cListener + "_", "com.sun.star.awt.XActionListener" )
   oCtrl.addActionListener( oActionListener )
End Sub

Sub MyList_actionPerformed(oEve)
  'dummy
End Sub

Sub MyList_itemStateChanged
  'dummy
End Sub
Work but I can't have a default selected item.

Thanks for your insight
Last edited by Mr.Dandy on Tue May 03, 2016 3:03 pm, edited 1 time in total.
OpenOffice 4.1.12 - Windows 10
User avatar
RoryOF
Moderator
Posts: 34618
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Unable to select item from a dynamic dialogbox

Post by RoryOF »

See
https://wiki.openoffice.org/wiki/Docume ... c/List_Box

The selected item is returned by

Code: Select all

 oListBox.selectItemPos( 0, True )
Where 0 is the default item and True means to select it. Logically, False in that position should "mean do not select this"

I'll be playing with this today to sort a setting in my code (unless I am distracted by anything else)
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

Re: Unable to select item from a dynamic dialogbox

Post by Mr.Dandy »

selectItemPos is unable until dialogbox is not visible.

This code works:

Code: Select all

	oDlg.setVisible(true)
	oCtrl.selectItemPos(1, true)
	oDlg.execute()
OpenOffice 4.1.12 - Windows 10
User avatar
RoryOF
Moderator
Posts: 34618
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Solved] Unable to select item from a dynamic dialogbox

Post by RoryOF »

I'm working on a situation where I have a number of items in a listbox; I have no problem with a default item being returned on first use of the listbox, but I'm hoping to change the code so that the selected item becomes the default for subsequent uses of the listbox in that editing session, changeable by selecting a different item.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Post Reply