Delete items from combo box

Hello everybody
sorry for my english (translated by google)

I need to delete the items of a combobox, but I can’t do it.
below is the code I wrote:

sub Cmbx_Update
dim oSheet, oCmbxSearch As Object
dim iItems, iCycle as integer
	oSheet = ThisComponent.CurrentController.ActiveSheet
	oCmbxSearch = oSheet.DrawPage.Forms.GetByName("frmForms").GetByName("CmbxSearch")
	REM -- get the number of items in the combo
	iItems = oCmbxSearch.ItemCount
	For iCycle = 0 to iItems - 1
     	oCmbxSearch.RemoveItems(0, iCycle)
	Next
end sub

It gives me the error:
BASIC runtime error.
Property or method not found: RemoveItems.

I haven’t found a list with all the methods and properties of a combo box. Does not exist?

thank you

There are no <removeAllItems()> or <removeItem()> methods in com.sun.star.awt.XComboBox, only in com.sun.star.awt.XItemList.

Hello,

The line of code should be:

oCmbxSearch.removeItem(x)

where x = index of item to be deleted. So if removing all items x = iCycle.

Again if all items, then this is quicker:

oCmbxSearch.removeAllItems()

No “For” statement required.

Also, for properties and methods, use an object inspector such as MRI:

1 Like

Hi, yes I had also tried:
oCmbxSearch.RemoveItems (iCycle)
And
oCmbxSearch.removeAllItems ()
but I always get the mistake
Property or method not found: RemoveItems.
or
Property or method not found: removeAllItems.

com.sun.star.awt.XComboBox
There are no <removeAllItems()> method, but there is
removeItems(nPos As Integer, nCount As Integer)
It removes a number of items at the specified position.

Call cbo.removeItems(0, cbo.ItemCount)

But:
XItemList has the removeAllItems() method.

Hello,
I was able to delete items with oCmbxSearch.removeAllItems () as suggested by Ratslinger
Works well

I made a mistake earlier and thought it didn’t work

but oCmbxSearch.removeItem (x) doesn’t work

removeItems()

@Loki2323

Sorry but should have tested this line. There is an error in that the incorrect index is used - should always be ‘0’.

Have re-tested both methods an are working:

Sub clrCombo
	oForm = ThisComponent.Drawpage.Forms.getByName("Form")
    oCmbxSearch = oForm.getByName("Combo Box 1")
    oCmbxSearch.removeAllItems()

REM OR ----

	REM -- get the number of items in the combo
'	iItems = oCmbxSearch.ItemCount
'	For iCycle = 0 to iItems - 1
'        oCmbxSearch.removeItem(0)
'	Next

End Sub

Also, to use the removeItems(nPos As Integer, nCount As Integer) method, you need to obtain the view:

'Get VIEW
    Doc = ThisComponent
	DocCtl = Doc.getCurrentController()
  	CtlView = DocCtl.GetControl(oCmbxSearch)
    CtlView.removeItems(0, iItems) Rem removes all items
1 Like

thanks, oCmbxSearch.removeItem (0, iCycle) works fine too

    '** DELETE ITEM (by Ratslinger) ********************************************
    sub Cmbx_Remove
    dim oSheet, oCmbxSearch as Object
    dim iItems, iCycle as integer
    dim sStr as string
    	oSheet = ThisComponent.CurrentController.ActiveSheet
    	oCmbxSearch = oSheet.DrawPage.Forms.GetByName("Form").GetByName("CmbxSearch")
    	iItems = oCmbxSearch.ItemCount
    	For iCycle = 0 to iItems - 1
            oCmbxSearch.removeItem(0, iCycle)
    	Next
    end sub

.removeItem (0, iCycle) can be used to choose whether to delete some items and others not
example:

sub Cmbx_Remove
dim oSheet, oCmbxSearch, oDoc, oDocCtl, oCtlView As Object
dim iItems, iCycle as integer
dim sStr as string
	oSheet = ThisComponent.CurrentController.ActiveSheet
	oCmbxSearch = oSheet.DrawPage.Forms.GetByName("Form").GetByName("CmbxSearch")
	oDoc = ThisComponent
	oDocCtl = oDoc.getCurrentController()
  	oCtlView = oDocCtl.GetControl(oCmbxSearch)
	iItems = oCmbxSearch.ItemCount
	For iCycle = 0 to iItems - 1
		sStr = oCmbxSearch.GetItem(0)
		if sStr <> "James" or "Alessio" then
        	oCmbxSearch.removeItem(0, iCycle)
        end if
	Next
end sub

but it tells me that the .GetItem method was not found
i can’t understand how object methods work
thank you

@Loki2323 Be careful to use correct upper/lower case. Basic is overly forgiving but if you use say Python you will get errors.

getItem is accessed through the view:

sStr = oCtlView.GetItem(0)

For me MRI is the fastest way to find what I am looking for. For others documentation may be the way to go. See: XComboBox Interface Reference

Also not certain just what you want to do with:

oCmbxSearch.removeItem(0, iCycle)

Hello
I would like to delete an item by specifying the name.
Example:

sub Cmbx_Remove
dim oSheet, oCmbxSearch, oDoc, oDocCtl, oCtlView As Object
dim iItems, iCycle as integer
dim sStr as string
	oSheet = ThisComponent.CurrentController.ActiveSheet
	oCmbxSearch = oSheet.DrawPage.Forms.GetByName("Form").GetByName("CmbxSearch")
	oDoc = ThisComponent
	oDocCtl = oDoc.getCurrentController()
  	oCtlView = oDocCtl.GetControl(oCmbxSearch)
	iItems = oCmbxSearch.ItemCount
	For iCycle = 0 to iItems - 1
		sStr = oCmbxSearch.GetItem(0)
		if sStr = "James" then
        	oCmbxSearch.removeItem(0, iCycle)
        end if
	Next
end sub

Cerco e cancello solo “James”

But what I would like to do, is to use a combo box that loads the names present in a column of calc.
Problem:
When I add a name or delete it from the calc column, the combo box does not update.
Solution:
By clicking on the “update” button the macro sub Cmbx_Update does:

  1. delete all items;
  2. Add all the names in column B to the combo box.

I tried to link the combo box to column B using the “source cells area” property, but it also loads empty strings “”

sub Cmbx_Update
	'************************************************************************
	'** adds items to combo box
	dim oSheet, oCmbxSearch As Object
	dim iItems, iCycle as integer
	dim sStr as string
	oSheet = ThisComponent.CurrentController.ActiveSheet
	oCmbxSearch = oSheet.DrawPage.Forms.GetByName("Form").GetByName("CmbxSearch")
	oCmbxSearch.removeAllItems()
	'************************************************************************
	'** adds items to combo box
	dim oRange, oCell As Object
	dim iRow, iColumns as integer
	Dim AddItem as string
	oRange = oSheet.getCellRangebyName("B10:B50")
	iColumns = oRange.Columns.getCount() - 1
	For iRow = 0 To oRange.Rows.getCount() - 1
		oCell = oRange.getCellByPosition(iColumns, iRow)
		If oCell.String <> "" Then
			if iRow = 0 then oCmbxSearch.text = oCell.String
			AddItem = oCell.String
			oCmbxSearch.insertItemText(oCmbxSearch.ItemCount, AddItem)
		end if
	next
end sub

So I have no idea how to do it.
Is there a better way than what I am doing?
I attach the file
ComboBox_Controls_09_100821.ods (13.2 KB)

@Loki2323 It is becoming a far cry from what was originally asked. I don’t even know what you want now. You state:

I would like to delete an item

but then state:

But what I would like to do, is to use a combo box that loads the names

which it appears it does with the sample provided.

Sorry but not sure what you want or need any more.

1 Like

Yes, I think I went off topic. actually you have already answered the initial question:
Delete items from combo box
Thank you

I got acquainted with two different comboboxes. And why are there two of them?

com.sun.star.form.XFormComponent → com.sun.star.form.OComboBoxModel
com.sun.star.awt.XControl → stardiv.Toolkit.UnoComboBoxControl


In the first there are methods that @Ratslinger wrote about, in the second there are those that I mentioned.

Do dialog boxes have their own controls?

Very strange and not obvious.

@eeigor Just one - model & view of same.

Controls in dialogs are the same as in forms. Clarification → they are not data aware!

Edit: if you want some info on model/view, see links here → Base macro form model vs. form controller