can someone please tell me what i'm doing wrong here

Apr 19, 2013 at 1:20 PM
Edited Apr 20, 2013 at 3:47 AM
The next and prevous buttons only work once by the looks. I've been looking for help and trying everything I can think of
Import-Module showui

$commandlist = Get-Command
[int]$cID = 0
            $Name = $commandlist[$cID]
            $ID = $cID

UniformGrid -ControlName CommandList-browser {

    UniformGrid {

        button "First" -name first -On_Click {
            $cID = 0
            $Name.text = $commandlist[$cID]
            $ID.text = $cID
            }

        button "Next" -name next -On_Click {
            if ($cID -lt ($commandlist.length-1)) {
                $cID++
                $Name.text = $commandlist[$cID]
                $ID.text = $cID
            }
        }
    }

    UniformGrid {
        button "Prev" -name prev -On_Click {
            if ($cID -gt 0) {
                $cID--
                $Name.text = $commandlist[$cID]
                $ID.text = $cID
            }            
        }
        button "Last" -name last -On_Click {
            $cID = $commandlist.length-1
            $Name.text = $commandlist[$cID]
            $ID.text = $cID
        }
    }
   
    UniformGrid {
            Label "ID"
            TextBox -Name ID -text $ID
            Label "Name"
            TextBox -Name Name -text $Name
        }
    UniformGrid {
        button "Quit" -name quit -On_Click {
            $parent | Set-UIValue -PassThru | Close-Control
            
        }
    }
}-Show

$cID
help $commandlist[$cID]
After testing it on another machine its working, My machine is running windows8, theirs is running windows 7, both seem to be running the same version of powershell,(it says 1.0 at the command prompt) and we are using the same version of showui(1.4) the only difference appears to be the versions of the OS and the ISE.

Is there that much difference between win 7 and 8 and or powershell ISE's as far as these scripts go ???
Apr 20, 2013 at 8:33 AM
Edited Apr 22, 2013 at 11:18 PM
ok, I now have it working by myself ;) on win 8 but man what a headache i have, Why would there be a difference between out setups that much that it would stop things going right.. what if we had to have this run on many different installs
Import-Module showui
$commandlist = get-command
$cID = 0
$Name = $commandlist[$cID]
$ID = $cID
UniformGrid -ControlName CommandList-browser -Columns 5 {
    button "First" -name first -On_Click {
        $global:cID = 0
        $Name.text = $global:commandlist[$global:cID]
        $ID.text = $global:cID
    }
    button "Next" -name next -On_Click {
        if ($global:cID -lt ($global:commandlist.length-1)) {
            $global:cID ++
            $Name.text = $global:commandlist[$global:cID]
            $ID.text = $global:cID
        }
    }
    button "Prev" -name prev -On_Click {
        if ($global:cID -gt 0) {
            $global:cID --
            $Name.text = $global:commandlist[$global:cID]
            $ID.text = $global:cID
        }            
    }
    button "Last" -name last -On_Click {
        $global:cID = $global:commandlist.length-1
        $Name.text = $global:commandlist[$global:cID]
        $ID.text = $global:cID
    }
    button "Quit" -name quit -On_Click  {
        $parent | Set-UIValue -PassThru | Close-Control
    } 
    Label "ID"
    TextBox -Name ID -text $global:ID
    Label "Name"
    TextBox -Name Name -text $global:Name
}-Show

$global:commandlist[$global:cID]
Coordinator
Apr 21, 2013 at 7:28 PM
Edited Apr 22, 2013 at 3:01 AM
Glad you got it sorted out -- I didn't really understand your question on that last post though, so if you still need help, please try again ;)

Incidentally, you can move the setup of the $commandList etc. inside the UI script, and then you won't have to $global: everything. You can also pass it in by setting it to the DataContext ... which is the natural way of doing databinding in WPF (and in ShowUI).

Something like this:
# Add the Help Synopsis to the cmdletinfo
$data = Get-Command -Type Cmdlet | Select-Object *, @{ Name="Description"; Exp={(Get-Help $_.Name).Synopsis} }

StackPanel {
    StackPanel -Orientation Horizontal {
        Label Name
        TextBox -Name Name -DataBinding @{ Text = Binding -Path "Name" -Mode OneWay }
    }
    StackPanel -Orientation Horizontal {
        Label Description
        TextBox -Name ID -DataBinding @{ Text = Binding -Path "/Description" -Mode OneWay } -MinLines 2
    }

    StackPanel -Orientation Horizontal {
        button "First" -name next -On_Click {
            $view = [System.Windows.Data.CollectionViewSource]::GetDefaultView( $this.DataContext )
            $view.MoveCurrentToFirst()
        }
        button "Prev" -name next -On_Click {
            $view = [System.Windows.Data.CollectionViewSource]::GetDefaultView( $this.DataContext )
            $view.MoveCurrentToPrevious()
            if ($view.IsCurrentBeforeFirst) { $view.MoveCurrentToFirst() }
        }
        button "Next" -name next -On_Click {
            $view = [System.Windows.Data.CollectionViewSource]::GetDefaultView( $this.DataContext )
            $view.MoveCurrentToNext()
            if ($view.IsCurrentAfterLast) { $view.MoveCurrentToLast() }
        }
        button "Last" -name next -On_Click {
            $view = [System.Windows.Data.CollectionViewSource]::GetDefaultView( $this.DataContext )
            $view.MoveCurrentToLast()
        }
    }
} -Show -DataContext $data
Apr 22, 2013 at 11:38 PM
Thanks for your example :)
I'm of really old school of dos batch scripts. not much knowledge (yet) of powershell or the available stuff that can be called on these days.

I've also noticed that your way takes a lot longer to run, and if someone doesn't have IIS installed does your way still work.? mine will

to do with my question. is there a way to write the script once and have it work on different security levels as the different versions of powershell are setup differently. my friends system was running windows 7 & powershell v2 , (where the script was originally started, he was having problems, and i got it to the stage in my first post , it wasn't working on mine but it was on his. I then got it working on mine too with the global) and my system windows 8 and powershell v3 , I was thinking that the script in post 1 should have worked in both but it didn't . so is there a way to check or get it so that I don't have to know the setup on others machines and still have the scripts work as intended. (IE: written for the lowest common denominator and not having to change the other newer/older systems so that it'll run without needing a re-write

could you please show me what you mean by
Incidentally, you can move the setup of the $commandList etc. inside the UI script, and then you won't have to $global: everything.
As I have tried moving the initialisation of the variables into

UniformGrid -ControlName CommandList-browser -Columns 5 {

and deleting global: from the variables but it still doesn't work
Coordinator
Apr 23, 2013 at 1:06 AM
My example took longer to run just because of the data line, it calls Get-Help for every single Cmdlet on your system to populate the description. Not sure why you mentioned IIS -- there's nothing IIS-related in there.
$data = Get-Command -Type Cmdlet | Select-Object *, @{ Name="Description"; Exp={ (Get-Help $_.Name).Synopsis } }
You could run it with just $data = Get-Command but then it's just names, which seemed like a boring example ;-)

PowerShell 3 introduced some changes in variable scope (which might be what you were seeing going from one system to another), you could test that by running the one that works on your friend's system in a window on your system that you start with: PowerShell.exe -Version 2 ... of course, if that's the case, then it's really hard to say which would be the "lowest" common denominator.

I think I was wrong about just declaring them inside. You'd have to set them to the DataContext or use the Get/Set-UIValue commands to store them. Because the variables are being used in the event-handlers, they need to be globally scoped. The event handlers are in module scope, which PowerShell hides away from the caller's scope (that's confusing, and I don't know why they do it, but anyway).

Because DataContext cascades down, you can do something like the following. However, I can't recommend this way over the other (it's a little silly to stick a cID in there instead of using the built-in "CurrentItem" stuff, and data-binding as in my previous example is definitely easier to work with instead of having to update repeatedly). Also, I dislike UniformGrid, which is a little ugly.
UniformGrid -DataContext @{
    # Initialize the variables: (No $, because you're defining a hashtable)
    CommandList = get-command
    cID = 0
} -ControlName CommandBrowser -Columns 5 {

    button "First" -name first -On_Click {
        $CommandId.Text = $this.DataContext.cID = 0
        $CommandName.Text = $this.DataContext.CommandList[$this.DataContext.cID]
    }
    button "Next" -name next -On_Click {
        if ($this.DataContext.cID -lt ($this.DataContext.CommandList.length-1)) {
            $CommandId.Text = $this.DataContext.cID += 1
            $CommandName.Text = $this.DataContext.CommandList[$this.DataContext.cID]
        }
    }
    button "Prev" -name prev -On_Click {
        if ($this.DataContext.cID -gt 0) {
            $CommandId.Text = $this.DataContext.cID -= 1
            $CommandName.Text = $this.DataContext.CommandList[$this.DataContext.cID]
        }            
    }
    button "Last" -name last -On_Click {
        $CommandId.Text = $this.DataContext.cID = $this.DataContext.CommandList.length - 1
        $CommandName.Text = $this.DataContext.CommandList[$this.DataContext.cID]
    }
    button "Quit" -name quit -On_Click  {
        # Set-UIValue to the current item
        $parent | Set-UIValue -Value $this.DataContext.CommandList[$this.DataContext.cID] -PassThru | Close-Control
    }

    Label "ID"
    TextBox -Name CommandId -Text $($this.DataContext.cID)
    
    Label "Name"
    TextBox -Name CommandName -Text $($this.DataContext.CommandList[$this.DataContext.cID])

} -Show
Apr 23, 2013 at 6:45 AM
Thank you so much for all your help.

why i mentioned IIS is when i try running your code as normal user it comes up with
Process should have elevated status to access IIS configuration data.