cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Henry1
Level: Powered On

Multiple Defualt Values in Listbox

I have a multiselect list control with 3 values(Apple, Orange, Pear) I would like to be able to select 2 of these values by defualt. is there a way of doing this? I have tried the following and this did not work

("Apple" ; "Pear")
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
mr-dang
Level 10

Re: Multiple Defualt Values in Listbox

The ListBox is not very flexible. For more advanced uses of the ListBox, I highly recommend using a Gallery and a temporary collection. The idea is to make the Gallery work like a ListBox, yet benefit from having more flexible design and function. It's a makeshift ListBox.

  1. Setup Gallery1 with your items as you normally do. Galleries also have Padding between records, which ListBoxes do not.
  2. Add a TextBox into Gallery1 that shows what text you wanted users to see if you were using a ListBox.
  3. Add a clickable object into the Gallery so it shows up for all records. For my example, I will be using Rectangle1. Set the following properties so that the rectangle is transparent, yet clickable so that you can simulate the ListBox abilities. These properties will make it large enough to fill the Gallery's template.
    Rectangle1.Fill:
    RGBA(0,0,0,0)
    
    Rectangle1.X:
    0
    
    Rectangle1.Y:
    0
    
    Rectangle1.Width:
    Parent.TemplateWidth
    
    Rectangle1.Height:
    Parent.TemplateHeight
  4. Set Rectangle1.OnSelect to:
    If(IsEmpty(Filter(temp,RecordId=ThisItem.RecordId)),
    	Collect(temp,ThisItem),
    	Remove(temp,ThisItem)
    )
    This means, "If you click the Rectangle, Collect the selected record into the "temp" Collection if it is not already in there. If the record is in "temp," then remove it from the temp collection." The purpose of the collection is to show that a record is selected as it would be in a ListBox. The next step will be about how you know a record has membership.

  5. Setup Gallery1.TemplateFill so that the background fill of a record in the gallery appears colored when it is selected and transparent when it is not. For this to work correctly, you will need your Items to be set up with a column that has unique values. Since I use CDS, each of my entities has a PrimaryId and RecordId column that has unique values to identify each record.
    Gallery1.TemplateFill:
    If(ThisItem.RecordId in temp.RecordId, RGBA(color of your choice showing the record is selected), RGBA(0,0,0,0) )
    The formula above means, "Fill the background of the record in the Gallery with [color] if the record is in the temp Collection. If the record is not in the temp Collection, then make it transparent." This formula shows you what is selected like a ListBox would. It is capable of multi-select. Again, this only works if your table has a column that uniquely identifies records.

  6. Step 4 simulates choosing multiple items like a ListBox. Step 5 simulates showing what is chosen and what is not like a ListBox. In this step, you will be able to set a Default of what is selected, which is not possible with a ListBox.

    The idea is to add default records to the "temp" Collection you started earlier, but clear it first to "Reset" it. Below I use ClearCollect() since it resets the contents of what is currently selected in "temp" and adds records as desired. 

    ClearCollect(temp,
    	Filter(Gallery1.AllItems,[conditions here])
    )
    The formula above pulls in Items from Gallery1 that you Filter. Set the conditions to only choose whatever you need. Place the formula wherever you need it. I assume you want this makeshift ListBox to reset when you see the Screen, so a good place is to place it in the OnVisible property of a Screen. Otherwise, add it to a "reset" Button for when you want Users to reset what's chosen to default.

  7. Finally, to reference what is chosen in ths fake ListBox, check for membership in the temp Collection using "in" or "exactin."
    ______ exactin temp.columnname
  8. Stylize the Gallery however you want. If you want to look identical to a ListBox, add a Border and white Fill to Gallery1.

 

Advantages of Gallery over ListBox

  • More flexible design: template padding, individual record fill, template fill, gallery fill
  • More flexible content: you can show more than text. Since it's a gallery, you can add all objects including images to show up for each record
  • More flexible function: you can add objects with OnSelect properties which ListBox cannot do
  • The temp Collection can be Items for another gallery.
Microsoft Employee
@8bitclassroom

View solution in original post

6 REPLIES 6
Community Support Team
Community Support Team

Re: Multiple Defualt Values in Listbox

Hi @Henry1,

 

If you would like to have the list box control to select only two values at most, currently I don't think this is available.

There is SelectMultiple property which controls Whether a user can select more than one item in the listbox, but there is no property to control how many items could be selected.

Please consider to submit this as an idea in the PowerApps idea forum.

And if you would like to set the initial value of the lsit box control, then you could modify the 

Default property– controls The initial value of a control before it is changed by the user.

More reference: List Box control in PowerApps

Regards

Community Support Team _ Michael Shao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Henry1
Level: Powered On

Re: Multiple Defualt Values in Listbox

So that is not actually my question. I would like to set multiple defualt values. for example when some one opens the item the list box is already populated with two items

Highlighted
mr-dang
Level 10

Re: Multiple Defualt Values in Listbox

The ListBox is not very flexible. For more advanced uses of the ListBox, I highly recommend using a Gallery and a temporary collection. The idea is to make the Gallery work like a ListBox, yet benefit from having more flexible design and function. It's a makeshift ListBox.

  1. Setup Gallery1 with your items as you normally do. Galleries also have Padding between records, which ListBoxes do not.
  2. Add a TextBox into Gallery1 that shows what text you wanted users to see if you were using a ListBox.
  3. Add a clickable object into the Gallery so it shows up for all records. For my example, I will be using Rectangle1. Set the following properties so that the rectangle is transparent, yet clickable so that you can simulate the ListBox abilities. These properties will make it large enough to fill the Gallery's template.
    Rectangle1.Fill:
    RGBA(0,0,0,0)
    
    Rectangle1.X:
    0
    
    Rectangle1.Y:
    0
    
    Rectangle1.Width:
    Parent.TemplateWidth
    
    Rectangle1.Height:
    Parent.TemplateHeight
  4. Set Rectangle1.OnSelect to:
    If(IsEmpty(Filter(temp,RecordId=ThisItem.RecordId)),
    	Collect(temp,ThisItem),
    	Remove(temp,ThisItem)
    )
    This means, "If you click the Rectangle, Collect the selected record into the "temp" Collection if it is not already in there. If the record is in "temp," then remove it from the temp collection." The purpose of the collection is to show that a record is selected as it would be in a ListBox. The next step will be about how you know a record has membership.

  5. Setup Gallery1.TemplateFill so that the background fill of a record in the gallery appears colored when it is selected and transparent when it is not. For this to work correctly, you will need your Items to be set up with a column that has unique values. Since I use CDS, each of my entities has a PrimaryId and RecordId column that has unique values to identify each record.
    Gallery1.TemplateFill:
    If(ThisItem.RecordId in temp.RecordId, RGBA(color of your choice showing the record is selected), RGBA(0,0,0,0) )
    The formula above means, "Fill the background of the record in the Gallery with [color] if the record is in the temp Collection. If the record is not in the temp Collection, then make it transparent." This formula shows you what is selected like a ListBox would. It is capable of multi-select. Again, this only works if your table has a column that uniquely identifies records.

  6. Step 4 simulates choosing multiple items like a ListBox. Step 5 simulates showing what is chosen and what is not like a ListBox. In this step, you will be able to set a Default of what is selected, which is not possible with a ListBox.

    The idea is to add default records to the "temp" Collection you started earlier, but clear it first to "Reset" it. Below I use ClearCollect() since it resets the contents of what is currently selected in "temp" and adds records as desired. 

    ClearCollect(temp,
    	Filter(Gallery1.AllItems,[conditions here])
    )
    The formula above pulls in Items from Gallery1 that you Filter. Set the conditions to only choose whatever you need. Place the formula wherever you need it. I assume you want this makeshift ListBox to reset when you see the Screen, so a good place is to place it in the OnVisible property of a Screen. Otherwise, add it to a "reset" Button for when you want Users to reset what's chosen to default.

  7. Finally, to reference what is chosen in ths fake ListBox, check for membership in the temp Collection using "in" or "exactin."
    ______ exactin temp.columnname
  8. Stylize the Gallery however you want. If you want to look identical to a ListBox, add a Border and white Fill to Gallery1.

 

Advantages of Gallery over ListBox

  • More flexible design: template padding, individual record fill, template fill, gallery fill
  • More flexible content: you can show more than text. Since it's a gallery, you can add all objects including images to show up for each record
  • More flexible function: you can add objects with OnSelect properties which ListBox cannot do
  • The temp Collection can be Items for another gallery.
Microsoft Employee
@8bitclassroom

View solution in original post

Henry1
Level: Powered On

Re: Multiple Defualt Values in Listbox

Is there still no ability to have multiple default values selected on a list box

jonathanO
Level: Powered On

Re: Multiple Defualt Values in Listbox

@mr-dang  I know this was posted a long time ago, but I just used this technique and it worked beautifully!  Thank you 😄

Darvin
Level: Power Up

Re: Multiple Defualt Values in Listbox

Just followed these instructions and the custom 'list box' works excellent!! Much more powerful than the standard list box object.

Helpful resources

Announcements
thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

Top Kudoed Authors (Last 30 Days)
Users online (4,479)