Powerapps tips: use toggle to set focus
Problem
During development of a “Canvas App“ in Powerapps I had to show a dialog requesting a single text as input.
The “Canvas developers“ know the way to show a dialog is to make visible a rectangle on the screen and its controls is a group of standard controls that are constrained with the rectangle visibility itself.
After followed the useful article Create dialog in powerapps I surprised that I was not able to set focus on the unique input box contained in dialog.
I assumed that “Setting focus“ over control was a trivial task but I’ve understood that in Powerapps it is not so. I read the SetFocus function limitations but again I didn’t understand the problem.
Diagnosis
After struggling to investigate the possible causes I understood that problem seemed related to the fact that the input box is initially not visible and when I try to make dialog visible jointly with input box, the subsequent invocation to SetFocus function seems not in-synch with the visible state of control.
Based upon diagnosis the solution should had to be ensure that invocation to SetFocus function happened after that input control was visible, so how to do this ?
Solution
To Help me came in to the play the “Toggle Control“, it has a useful handler named OnCheck
that is raised when the toggle is ON so that its Default
property is True
.
Based upon that has been enough add a toggle in a Dialog controls group and assuming that
- Rectangle (i.e. Dialog) is named:
Rectangle_1
- Input Box is named:
TextInput_1
- Toggle is named:
Toggle_1
- The action that show dialog perform
UpdateContext({showPopup: true})
setting the variableshowPopup
attrue
the solution is to set the following property on Toggle_1
Property | Value |
---|---|
Default |
showPopup |
OnCheck |
SetFocus( TextInput_1 ) |
Important Note
In order to avoid showing toggle not use
Visible
property but rather makes its size (witdh & height) equals to 0 otherwise the triggerOnCheck
not will be activated.
You can downlad a simple app here
That’s all, hope this help and enjoy Powerapps
Dev. Note - Container Control
Lately has been released for Canvas App a new Container Control (experimental feature yet), it allows more flexibility over controls grouping and could be used instead of Rectangle for Dialog. Take a look the the links below.