DropDownItemCollection indexer

__getitem__(index)

Gets or sets the element at the specified index.

def __getitem__(self, index: int):
    ...
ParameterTypeDescription
indexint

Examples

Shows how to insert a combo box field, and edit the elements in its item collection.

from api_example_base import ApiExampleBase, ARTIFACTS_DIR

class ExampleFormFields(ApiExampleBase):

    def test_drop_down_item_collection(self):
        doc = aw.Document()
        builder = aw.DocumentBuilder(doc=doc)
        # Insert a combo box, and then verify its collection of drop-down items.
        # In Microsoft Word, the user will click the combo box,
        # and then choose one of the items of text in the collection to display.
        items = ['One', 'Two', 'Three']
        combo_box_field = builder.insert_combo_box('DropDown', items, 0)
        drop_down_items = combo_box_field.drop_down_items
        self.assertEqual(3, drop_down_items.count)
        self.assertEqual('One', drop_down_items[0])
        self.assertEqual(1, drop_down_items.index_of('Two'))
        self.assertTrue(drop_down_items.contains('Three'))
        # There are two ways of adding a new item to an existing collection of drop-down box items.
        # 1 - Append an item to the end of the collection:
        drop_down_items.add('Four')
        # 2 - Insert an item before another item at a specified index:
        drop_down_items.insert(3, 'Three and a half')
        self.assertEqual(5, drop_down_items.count)
        # Iterate over the collection and print every element.
        for item in drop_down_items:
            print(item)
        # There are two ways of removing elements from a collection of drop-down items.
        # 1 - Remove an item with contents equal to the passed string:
        drop_down_items.remove('Four')
        # 2 - Remove an item at an index:
        drop_down_items.remove_at(3)
        self.assertEqual(3, drop_down_items.count)
        self.assertFalse(drop_down_items.contains('Three and a half'))
        self.assertFalse(drop_down_items.contains('Four'))
        doc.save(file_name=ARTIFACTS_DIR + 'FormFields.DropDownItemCollection.html')
        # Empty the whole collection of drop-down items.
        drop_down_items.clear()

See Also