Tobias Krais wrote:
> Hi together,
>
> I am currently implementing a little dialog with an
> UnoControlListBoxModel. Now I want to preselect an entry of the list.
> How can I do this? I tried the SelectedItems option, but it did not work.
>
> As a workaround, I tried to set an item with the
> XListBox.selectItemsPos() function. But it had no affect, only selecting
> it from the action listener had the wished effects.
>
selectItem*s*Pos(seq_short, boolSelect) selects one or more items of a
multi-select box on a visible dialog.
selectItemPos(short, boolSelect) works for me if the list box is not
multi-selectable and the dialog is visible.
Then there is property SelectedItems of a box's model, which can be set
before loading the dialog like in the StarBasic dialog editor.
I wrote a quick and dirty Python script for testing.
Start your office like this:
> /opt/openoffice.org2.3/program/scalc
> -accept="socket,host=localhost,port=2002;urp;"
Save the script with unix-linefeeds(!) and run it with OOo's Python
runtime passing one or two optional arguments:
1. (integer) a single item to select (or -1)
2. (anything) use a multi-select box instead of a plain list.
Single-select list, pre-select none:
> /opt/openoffice.org2.3/program/python ~/ooopy/listbox.test.py
Single-select list, pre-select item 0:
> /opt/openoffice.org2.3/program/python ~/ooopy/listbox.test.py 0
Multi-select list, select all:
> /opt/openoffice.org2.3/program/python ~/ooopy/listbox.test.py -1 1
Multi-select list, select item 0
> /opt/openoffice.org2.3/program/python ~/ooopy/listbox.test.py 0 1
> import uno, sys
>
> # get item no. from first argument:
> try:
> nSelect = int(sys.argv[1])
> except:
> nSelect = -1
>
> # create a multi-select list if there is any second argument:
> try:
> boolMulti = sys.argv[2] and True
> except:
> boolMulti = False
>
> strConnect =
> "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext"
> localContext = uno.getComponentContext()
> resolver = localContext.ServiceManager.createInstanceWithContext(
> "com.sun.star.bridge.UnoUrlResolver", localContext )
> ctx = resolver.resolve(strConnect)
> smgr = ctx.ServiceManager
> seqItems = ('Item 0', 'Item 1', 'Item 2', 'Item 3', 'Item 4',)
> # sequence (0,1,2,3,4):
> seqIDs = tuple(range(len(seqItems)))
>
> oDlgModel = smgr.createInstance('com.sun.star.awt.UnoControlDialogModel')
> oDlgModel.Height = 100
> oDlgModel.Width = 100
> oCtrlModel =
> oDlgModel.createInstance('com.sun.star.awt.UnoControlListBoxModel')
> oCtrlModel.PositionX = 5
> oCtrlModel.PositionY = 5
> oCtrlModel.Height = 80
> oCtrlModel.Width = 80
> oCtrlModel.MultiSelection = boolMulti
> oCtrlModel.StringItemList = seqItems
>
> if boolMulti:
> # select all items by default:
> oCtrlModel.SelectedItems = seqIDs
> else:
> # select no item by default:
> pass
>
> # c.s.s.container.XNameContainer
> oDlgModel.insertByName('box', oCtrlModel)
>
> oDlg = smgr.createInstance('com.sun.star.awt.UnoControlDialog')
> # c.s.s.awt.XControl
> oDlg.setModel(oDlgModel)
>
> # the "living" dialog must be visible before we select an item:
> # c.s.s.awt.XWindow
> oDlg.setVisible(True)
>
> try:
> if nSelect > -1:
> # c.s.s.awt.XControlContainer
> oBox = oDlg.getControl('box')
> if boolMulti:
> # Deselect all items before selecting a single one:
> # c.s.s.awt.XListBox
> oBox.selectItemsPos(seqIDs, False)
> oBox.selectItemsPos((nSelect,), True)
> else:
> oBox.selectItemPos(nSelect, True)
> except:
> print 'Err: Can not select item #'+ str(nSelect)
> # c.s.s.awt.XDialog
> oDlg.execute()
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]