Add new item to Active UI

Jun 17, 2014 at 9:49 PM
Edited Jun 17, 2014 at 9:52 PM
Hi, I'm new to the website and recent convert from trying WPF in Visual Studio. Things I found easier in Powershell were far more difficult using other developer languages. So, I'm trying some things out with ShowUI. Basically I'm trying to update the current window with a new object. In the example below I want to add a new label when a button click is detected. Later I might just want to update the label text when retrieving data from another source. I've missed a basic concept somewhere. Any help appreciated.
Import-Module Showui


Function NewForm () 
    New-Window -title "Main form"  -Width 500 -Height 250 -WindowStartupLocation CenterScreen {

        Grid -Rows 8 -columns 4 -Margin 5 -ControlName 'Grid1'  {

            New-Label "Name : " -row 1 -column 0 -HorizontalAlignment Right
            New-ComboBox -row 1 -column 1 -SelectedIndex 0 -IsTextSearchEnabled:$true -Height 25 -Width 90 -Name UserList -items { $users } 
            New-Button "Update" -row 7 -column 1 -ColumnSpan 2 -On_Click  { 

                New-Label "New Item" -row 3 -column 1 

} -show

Jun 17, 2014 at 10:16 PM
I should have said in the OP, I may have missed similar posts on the forum. If thats the case just point me at them and Ill go and read them. Cheers
Jul 3, 2014 at 8:23 AM
Edited Jul 3, 2014 at 8:37 AM
I'm so sorry, I thought I'd answered this!

We actually provide a script function for this: Add-ChildControl

window -Tag @() {
   Grid -Name "layout" -Rows 8 -Columns 2 -Margin 10 {
      ComboBox -Name "name" -Column 1 -SelectedIndex 0 -IsTextSearchEnabled -items { $users }
      Label "Name:" -Target "Name" -Column 0  -horizontalalignment right
      Button "Update" -Row 7 -Column 1 -horizontalalignment right -on_click {
         TextBlock $Name.Text -Row (@($Window.Tag).Length + 1) | Add-ChildControl $layout
         $Window.Tag += @($Name.Text) 
         $Name.SelectedIndex = 0
} -Show -SizeToContent Height -Width 300
Now, that right there has to be the most learning I've packed into a sample in a while ;-)

1) Don't use ControlName unless you really mean it -- it has scope consequences in ShowUI that get really weird.
2) Note that all controls that have names get magic variables in event handlers, so you can mess with them
3) If you want a persistent variable, the best place to put it is in the control on the tag or in the ResourceDictionary
4) Normally, the "Tag" property of a ui element just serves as a spot to store whatever you want ... but in ShowUI, when a window closes, anything that's on the "Tag" property is output, so this will actually output the selected user(s) when the window closes.

For the sake of the demo, I store the selected user on the tag, and then hard-coded "+1" as the the "Row" so that you can add more than one user ... obviously you could, instead, have another container there (e.g.: a stackpanel) that you add the things to so that you don't ever run out of space.

That would be slicker, perhaps:

window {
   Grid -Name "layout" -Rows 3 -Columns 2 -Margin 10 {
      ComboBox -Name "name" -Column 1 -SelectedIndex 0 -IsTextSearchEnabled -items { $users }
      Label "Name:" -Target "Name" -Column 0  -horizontalalignment right
      StackPanel -Name "SelectedUsers" -Row 1 -ColumnSpan 2
      Button "Update" -Row 2 -Column 1 -horizontalalignment right -on_click {
         TextBlock $Name.Text | Add-ChildControl $SelectedUsers
         $Name.SelectedIndex = 0
} -Show -SizeToContent Height -Width 300
Note that in this example, I didn't set-uivalue or update the window.tag by hand, so when it closes, no output.
Hypothetically, instead of a TextBlock you can use a Label, or even write your own custom control function (any function which returns a control, say, for instance, a grid with a bunch of stuff on it) to output a set of controls with extra data or for the user to enter extra data ...