How to set the selected-value of a drop-down?

We want to programmatically set the drop-down value.

Well, in its simplest form you do this (for example) :

Drag a drop down onto the form and, in the properties set the items to “Item 1”, “Item 2”, etc.
Then on a button click event, do this :

self.dropdown.selected_value = "Item 2"

When you run it, the drop down will show “Item 1” by default, and clicking the button will change it to “Item 2”

2 Likes

Thnx. I don’t know how my coder missed that one . .

I run into the following error when trying to set the self.dropdown.selected_value property via client code
“Cannot read property ‘ob$type’ of undefined”

Sounds like you might not be using the right component name?

Is your component called “dropdown”?

he he, no. I was simplifying my example.

here is a snippet:

          global highest_instance_number
          highest_instance_number = max(self.bqs_instance_drop_down.items)
          print("Highest Instance Number is " + str(highest_instance_number))
          self.bqs_instance_drop_down.selected_value = highest_instance_number

bqs_instance_drop_down.items is a list of integers.

:slight_smile: thought that might have been too easy.

Any chance you can share a clone link of your project (here or PM)?

https://anvil.works/docs/editor/cloning

I might be able to spot what is wrong.

Sure, how do I PM you?

Click your icon top right, there’s an envelope icon there for PMs.

Found the mail icon but no ability to send via that feature. If you initiate a PM with me, I can reply via that thread… Do you mind?

Is it possible to set the selected value of a dropdown?

I am asking because I have tried all the above suggestions and none of them worked.

Thanks a lot.

If none of them worked, it might be an accidental timing issue. That is, unintentionally doing things (setting the list, setting the selected value) in a different order than you expected.

For example, setting the selected value in __init__, before the list gets filled in.

Of course, without seeing all the relevant code, all any forum member can do is speculate. Like I just did.

1 Like