Page 1 of 1

[Solved] Unable to select item from a dynamic dialogbox

Posted: Tue May 03, 2016 2:41 pm
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

Re: Unable to select item from a dynamic dialogbox

Posted: Tue May 03, 2016 2:55 pm
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)

Re: Unable to select item from a dynamic dialogbox

Posted: Tue May 03, 2016 3:03 pm
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()

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

Posted: Tue May 03, 2016 3:09 pm
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.