Skip to content

Instantly share code, notes, and snippets.

@1RedOne
Created November 9, 2016 19:37
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save 1RedOne/1ae10476607dda8508860da190dcf000 to your computer and use it in GitHub Desktop.
PowerShell GUI - Enable a tab after user input and validation
$logo = "$($env:temp)\FoxDeploy.png"
wget 'https://dl.dropboxusercontent.com/u/6268163/Foxdeploy_full.png' -OutFile $logo
#ERASE ALL THIS AND PUT XAML BELOW between the @" "@
$inputXML = @"
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid Background="#FF2D2B2B">
<TabControl x:Name="tabControl">
<TabItem Header="Tab Number 1">
<Grid Background="#FFE5E5E5">
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Margin="43,76,0,0" Height="37" Width="266"><Run Text="Fill out this form to enable the rest of the UI"/><InlineUIContainer>
</InlineUIContainer></TextBlock>
<Image x:Name="image" HorizontalAlignment="Left" Height="62" Margin="44,9,0,0" VerticalAlignment="Top" Width="229" Source="$logo"/>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="27" Margin="38,113,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="130"/>
</Grid>
</TabItem>
<TabItem Header="Tab Number 2" IsEnabled="False">
<Grid Background="#FFE5E5E5">
<TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Height="153" Margin="68,58,0,0" TextWrapping="Wrap" Text="Welcome to page number 2" VerticalAlignment="Top" Width="388" FontSize="48"/>
</Grid>
</TabItem>
</TabControl>
</Grid>
</Window>
"@
$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = $inputXML
#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch [System.Management.Automation.MethodInvocationException] {
Write-Warning "We ran into a problem with the XAML code. Check the syntax for this control..."
write-host $error[0].Exception.Message -ForegroundColor Red
if ($error[0].Exception.Message -like "*button*"){
write-warning "Ensure your &lt;button in the `$inputXML does NOT have a Click=ButtonClick property. PS can't handle this`n`n`n`n"}
}
catch{#if it broke some other way <span class="wp-smiley wp-emoji wp-emoji-bigsmile" title=":D">:D</span>
Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."
}
#===========================================================================
# Store Form Objects In PowerShell
#===========================================================================
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}
Function Get-FormVariables{
if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true}
write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
get-variable WPF*
}
Get-FormVariables
#===========================================================================
# Use this space to add code to the various form elements in your GUI
#===========================================================================
#Add a listener to enable the second tab, if the first tab is filled out
$WPFtextBox.Add_TextChanged({
#this code runs when the text box content changes
#check to see if the length is 0, if so, stop
if ($WPFtextBox.Text.Length -eq 0){
$WPFtabControl.Items[1].IsEnabled = $false
continue
}
$WPFtabControl.Items[1].IsEnabled = $true
})
#Adding code to a button, so that when clicked, it pings a system
# $WPFbutton.Add_Click({ Test-connection -count 1 -ComputerName $WPFtextBox.Text
# })
#===========================================================================
# Shows the form
#===========================================================================
$Form.ShowDialog() | out-null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment