Write a GUI in Powershell – Part 1

1. Create XAML interface in Visual Studio.  This will be fast and less coding.  Delete x:class property in Window tag and save the file.

Codes:


<Window
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:Collections="clr-namespace:System.Collections;assembly=mscorlib"
x:Name="window" Title="Shutdown PC" Height="436.098" Width="544">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="298*"/>
<RowDefinition Height="107*"/>
</Grid.RowDefinitions>
<Grid IsEnabled="{Binding Stopped}" Margin="0,0,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="46*"/>
<RowDefinition Height="34*"/>
<RowDefinition Height="34*"/>
<RowDefinition Height="29*"/>
<RowDefinition Height="37*"/>
<RowDefinition Height="79*"/>
<RowDefinition Height="39*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="102*"/>
<ColumnDefinition Width="135*"/>
<ColumnDefinition Width="113*"/>
<ColumnDefinition Width="36*"/>
<ColumnDefinition Width="148*"/>
</Grid.ColumnDefinitions>
<RadioButton x:Name="rbCountDown" Content="Count Down" HorizontalAlignment="Left" Margin="10,25,0,0" VerticalAlignment="Top" Height="15" Width="85" GroupName="Type" IsChecked="True"/>
<RadioButton x:Name="rbEndTime" Content="End Time" HorizontalAlignment="Left" Margin="10,12,0,0" VerticalAlignment="Top" Height="15" Width="85" GroupName="Type" Grid.Row="1"/>
<RadioButton x:Name="rbIdleTime" Content="Idle Time" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="2" VerticalAlignment="Top" Height="15" Width="68" GroupName="Type"/>
<RadioButton x:Name="rbKeepAwake" Content="Keep Awake" HorizontalAlignment="Left" Margin="10,9,0,0" Grid.Row="3" VerticalAlignment="Top" Height="15" Width="84" GroupName="Type"/>
<Label Content="Hours" Grid.Column="1" HorizontalAlignment="Left" Margin="10,-5,0,0" VerticalAlignment="Top" Width="78" Height="26"/>
<Label Content="Minutes" Grid.Column="2" HorizontalAlignment="Left" Margin="10,-5,0,0" VerticalAlignment="Top" Height="26" Width="74"/>
<Label Content="Seconds" Grid.Column="4" HorizontalAlignment="Left" Margin="10,-5,0,0" VerticalAlignment="Top" Height="26" Width="93"/>
<ComboBox x:Name="cbHours" Grid.Column="1" Margin="10,21,10,0" Height="22" VerticalAlignment="Top" IsEditable="True" SelectedIndex="0"/>
<ComboBox x:Name="cbMinutes" Grid.Column="2" Margin="10,21,10,0" Height="22" VerticalAlignment="Top" IsEditable="True" SelectedIndex="1" Grid.ColumnSpan="2"/>
<ComboBox x:Name="cbSeconds" Grid.Column="4" Margin="10,21,10,0" Height="22" VerticalAlignment="Top" IsEditable="True" SelectedIndex="0"/>
<Label Content="To:" Grid.Column="1" HorizontalAlignment="Left" Margin="8,6,0,0" Grid.Row="1" VerticalAlignment="Top" Width="55" Height="26"/>
<TextBox x:Name="tbEndTime" Grid.Column="1" Margin="45,8,11,0" Grid.Row="1" VerticalAlignment="Top" Height="22" Grid.ColumnSpan="2" />
<Button x:Name="btNow" Content="Now+1hr" Grid.Column="3" HorizontalAlignment="Left" Margin="9,9,0,0" Grid.Row="1" VerticalAlignment="Top" Width="75" Height="20" Grid.ColumnSpan="2"/>
<ComboBox x:Name="cbIdleTime" Grid.Column="1" Margin="10,8,0,0" Grid.Row="2" VerticalAlignment="Top" Height="22"  IsEditable="True" Text="{Binding IdleTime}">
<ComboBoxItem Content="15"/>
<ComboBoxItem Content="20"/>
<ComboBoxItem Content="25"/>
<ComboBoxItem Content="30"/>
<ComboBoxItem Content="45"/>
<ComboBoxItem Content="60"/>
<ComboBoxItem Content="120"/>
</ComboBox>
<Label Content="Interval" VerticalAlignment="Top" Margin="12,3,0,0" Grid.Row="4" Height="26" HorizontalAlignment="Left" Width="80"/>
<ComboBox x:Name="cbInterval" Text="{Binding DataContext[Interval], Mode=TwoWay, RelativeSource={RelativeSource Self}}" Grid.Column="1" Margin="10,7,102,0" Grid.Row="4" VerticalAlignment="Top" Height="22" IsEditable="True" SelectedIndex="0" Grid.ColumnSpan="2" >
<ComboBoxItem Content="1" />
<ComboBoxItem Content="2" />
<ComboBoxItem Content="3" />
<ComboBoxItem Content="4" />
<ComboBoxItem Content="5" />
</ComboBox>
<TextBox x:Name="tbStatus" Grid.ColumnSpan="5" Margin="22,10,23,3" VerticalScrollBarVisibility="Visible" Grid.Row="5" TextWrapping="Wrap" IsReadOnly="True" IsReadOnlyCaretVisible="True"/>
<ProgressBar x:Name="pbStatus" Value="{Binding Running.TotalSeconds, Mode=OneWay}" Minimum="0" FlowDirection="RightToLeft" Margin="22,9,23,0" Grid.Row="6" Grid.ColumnSpan="5" Height="21" VerticalAlignment="Top">
<ProgressBar.Foreground>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform CenterY="0.5" CenterX="0.5"/>
<SkewTransform CenterY="0.5" CenterX="0.5"/>
<RotateTransform Angle="90" CenterY="0.5" CenterX="0.5"/>
<TranslateTransform/>
</TransformGroup>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="Yellow" Offset="1"/>
<GradientStop Color="Lime"/>
</LinearGradientBrush>
</ProgressBar.Foreground>
</ProgressBar>
<TextBox x:Name="cbRunning" Grid.Column="1" Margin="17,9,10,0" Grid.Row="6" Grid.ColumnSpan="3" Text="{Binding Running, StringFormat=hh\\:mm\\:ss}" IsEnabled="False" TextAlignment="Center" BorderThickness="0" Background="{x:Null}" Foreground="#FFFF0000" FontWeight="Bold" Height="22" VerticalAlignment="Top"/>
<Label Content="Minutes" Grid.Column="2" Margin="11,6,0,0" Grid.Row="2" Height="26" VerticalAlignment="Top" HorizontalAlignment="Left" Width="53"/>
<Label Content="Seconds" Grid.Column="2" HorizontalAlignment="Right" Margin="0,5,39,0" Grid.Row="4" VerticalAlignment="Top" RenderTransformOrigin="0.974,-0.808"/>
</Grid>
<Grid Margin="0,0" Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="16*"/>
<RowDefinition Height="91*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="21*"/>
<ColumnDefinition Width="86*"/>
<ColumnDefinition Width="221*"/>
<ColumnDefinition Width="208*"/>
</Grid.ColumnDefinitions>
<Label Content="Command to be executed when time is reached" HorizontalAlignment="Left" Margin="0,-6,0,0" VerticalAlignment="Top" Grid.ColumnSpan="3" Width="491" Height="26" Grid.RowSpan="2" Grid.Column="1"/>
<TextBox x:Name="tbCmd" Margin="0,3,23,35" TextWrapping="Wrap" AcceptsReturn="True"  VerticalScrollBarVisibility="Visible" IsEnabled="True" Grid.ColumnSpan="3" Grid.Row="1" SpellCheck.IsEnabled="True" Grid.Column="1" Text="{Binding Text, ElementName=cbCmds, Mode=OneWay}"/>
<Button x:Name="btExec" Content="Exec" Margin="0,0,0,10" Height="20" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="75" Grid.Column="1" Grid.Row="1"/>
<ComboBox x:Name="cbCmds" SelectedIndex="0" Grid.Column="2" Margin="10,0,10,10" Grid.Row="1" VerticalAlignment="Bottom">
<ComboBoxItem Content="Write-Host ($viewModel.Display() -join &quot;`n&quot;)" />
<ComboBoxItem Content="Shutdown -t 30 -s" />
<ComboBoxItem Content="Shutdown -a"/>
</ComboBox>
<Button x:Name="btReset" Content="Reset" Grid.Column="3" HorizontalAlignment="Left" Margin="10,0,0,10" Width="75" Height="20" VerticalAlignment="Bottom" Grid.Row="1"/>
<Button x:Name="btStart" Content="Start" Margin="0,0,40,10" Height="20" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="75" Grid.Column="3" Grid.Row="1"/>
</Grid>
</Grid>
</Window>

 

Advertisements
This entry was posted in C#, Powershell, XAML. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s