Page 1 of 1

[Solved] Add/remove data from one ListBox to another

Posted: Wed Apr 26, 2017 1:31 pm
by sertvik
Hi. Can someone help me out with this one?

What i want for a macro to do:
1) Take data from cells and put it into ListBox1 (done)
2.1) On pressing a button add an entry from ListBox1 to ListBox2 AND do not overwrite it if the next entry is added
2.2) if entry goes from ListBox1 to ListBox2, remove it from ListBox1
2.3) make multi selection to work
3) Use entries from ListBox1 for some anything, for example simply add to a cell or cells

2.1,2.2 and 2.3 are the ones i don't understad how to do. I can't find any info on the internet about ListBox unless its listbox inside the document and not macross type form that i need.
Is there're a simple option to add an entry to ListBox like .InsertData or is there more sophisticated way of doing this?

Code: Select all

Dim MyArr() As String
Dim MyArr2() As String
Dim MyString as String
Dim oDialog1 as Object
Dim oSheet

Function CountColumnsRows(Doc,SheetIndex,NumColumns,NumRows)
	Sheet = Doc.Sheets.getByIndex(SheetIndex)
	Curs = Sheet.createCursor
	Curs.gotoEndOfUsedArea(True)
	NumRows = Curs.Rows.Count
	NumColumns = Curs.Columns.Count
End Function

Sub Dialog1Show() 	
    DialogLibraries.LoadLibrary("Standard")
    oDialog1 = CreateUnoDialog( DialogLibraries.Standard.Dialog1 )
   	oSheet = thisComponent.Sheets(0)
   	
	REM Count Rows for first Sheet of this document
	CountColumnsRows(ThisComponent,0,NumColumns,NumRows)
	
   	Redim MyArr(NumRows) As String
   	Redim MyArr2(NumRows) As String
	row=0
	col=1
	while row <> NumRows
		MyArr(row)=oSheet.getCellByPosition(col,row).String
		row=row+1
	wend

   	oDialog1.getControl("ListBox1").getModel().StringItemList = MyArr()

    oDialog1.Execute()
End Sub

Sub Dialog1Box()
	MyArr2(0) = oDialog1.getControl("ListBox1").getSelectedItem()
	oDialog1.getControl("ListBox2").getModel().StringItemList = MyArr2()
End Sub

Sub Dialog1Action()
	oSheet.getCellByPosition(5,5).String = MyArr2(0)
End Sub

Re: Add/remove data from one ListBox to another

Posted: Wed Apr 26, 2017 1:43 pm
by RoryOF
A very full set of macros to manipulate Listboxes is distributed with OpenOffice (and probably with LibreOffice - I haven't checked). This can be accessed through /Tools /Macros /Organise macros /OpenOffice BASIC : OpenOffice Macros :Tools :Listbox.

Re: Add/remove data from one ListBox to another

Posted: Thu Apr 27, 2017 11:04 am
by sertvik
Wow thanks, didn't know about that. Solved my problem even better than i hoped it would.
MoveSelectedListBox(SourceListbox, TargetListbox) - does what it says and works with multiselection. Awesome!