I am doing a GUI in Visual Studuio for a script I made for Powershell and I need to use radio buttons to add variables to make this script works, like for example, the script works with 5 printer brands, so the user needs to select one of this to progress. Then, for each brand you have inside the models. At the end, will execute the code to create a print queue on a selected server, which includes the variables held on the radio buttons below:
$maker - brand of the device.
$model - model of the device.
$modelfull - full model name for comment purposes.
$driver - print driver to assign to the print queue.
How can I add those variables to each radio button? I.e. I have two models, Deskjet 3050 and Deskjet 3045, how can I convert those models into a variable once user select the radio button and press Next on the window?
Below you can see example of the code from Visual Studio where is the frame with the radio buttons:
<Page x:Class="Print_Queue_Configuration_Tool.Maker_2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Print_Queue_Configuration_Tool"
mc:Ignorable="d"
d:DesignHeight="570" d:DesignWidth="754"
Title="Maker_2">
<Grid x:Name="Maker_2_Grid">
<Button x:Name="Maker_2_ButtonBack" Content="Back" HorizontalAlignment="Left" Margin="360,525,0,0" VerticalAlignment="Top" Width="120" Height="35"/>
<Button x:Name="Maker_2_ButtonNext" Content="Next" HorizontalAlignment="Left" Margin="485,525,0,0" VerticalAlignment="Top" Width="120" Height="35"/>
<Button x:Name="Maker_2_ButtonCancel" Content="Cancel" HorizontalAlignment="Left" Margin="610,525,0,0" VerticalAlignment="Top" Width="120" Height="35" IsCancel="True"/>
<Image x:Name="Maker_2_ImageLogo" HorizontalAlignment="Left" Height="44.264" Margin="480.157,30.312,0,0" VerticalAlignment="Top" Width="273.843" Source="logo.png" RenderTransformOrigin="0.5,0.5"/>
<TextBlock x:Name="Maker_2_TextMaker_2" HorizontalAlignment="Left" Margin="70,140,0,0" TextWrapping="Wrap" Text="Please select the maker of the device:" VerticalAlignment="Top"/>
<RadioButton x:Name="Maker_2_RadioMaker1" Content="Canon" VerticalAlignment="Center" Margin="272.25,220.25,351.75,333.75" Height="16" Width="130" GroupName="Makers_2" FontWeight="Bold"/>
<RadioButton x:Name="Maker_2_RadioMaker2" Content="Epson" VerticalAlignment="Center" Margin="272.25,260.25,351.75,293.75" Height="16" Width="130" GroupName="Makers_2" FontWeight="Bold"/>
</Grid>
</Page>
Here you can see how I have assigned the variables to each button, but of course it's not working:
$wpf.Maker_2_RadioMaker1.add_Checked({
$maker = 'Canon'
})
$wpf.Maker_2_RadioMaker2.add_Checked({
$maker = 'Epson'
})
Thank you!!
PS: I am new in this so I am still learning. Please be polite, remember that you were in my position once :)
Here your Page is working... I had to adapt the xml a bit.
Note the FindName to Address the Radiobuttons
[xml]$xaml = #"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window"
Title="Maker_2" Height="770" Width="800">
<Grid x:Name="Maker_2_Grid">
<Button x:Name="Maker_2_ButtonBack" Content="Back" HorizontalAlignment="Left" Margin="360,525,0,0" VerticalAlignment="Top" Width="120" Height="35"/>
<Button x:Name="Maker_2_ButtonNext" Content="Next" HorizontalAlignment="Left" Margin="485,525,0,0" VerticalAlignment="Top" Width="120" Height="35"/>
<Button x:Name="Maker_2_ButtonCancel" Content="Cancel" HorizontalAlignment="Left" Margin="610,525,0,0" VerticalAlignment="Top" Width="120" Height="35" IsCancel="True"/>
<Image x:Name="Maker_2_ImageLogo" HorizontalAlignment="Left" Height="44.264" Margin="480.157,30.312,0,0" VerticalAlignment="Top" Width="273.843" Source="logo.png" RenderTransformOrigin="0.5,0.5"/>
<TextBlock x:Name="Maker_2_TextMaker_2" HorizontalAlignment="Left" Margin="70,140,0,0" TextWrapping="Wrap" Text="Please select the maker of the device:" VerticalAlignment="Top"/>
<RadioButton x:Name="Maker_2_RadioMaker1" Content="Canon" VerticalAlignment="Center" Margin="272.25,220.25,351.75,333.75" Height="16" Width="130" GroupName="Makers_2" FontWeight="Bold"/>
<RadioButton x:Name="Maker_2_RadioMaker2" Content="Epson" VerticalAlignment="Center" Margin="272.25,260.25,351.75,293.75" Height="16" Width="130" GroupName="Makers_2" FontWeight="Bold"/>
</Grid>
</Window>
"#
function Check-Radios
{
if($this.Name -eq "Maker_2_RadioMaker1") {Write-Host "Canon"}
if($this.Name -eq "Maker_2_RadioMaker2") {Write-Host "Epson"}
}
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)
$window.FindName("Maker_2_RadioMaker1").add_Checked({Check-Radios})
$window.FindName("Maker_2_RadioMaker2").add_Checked({Check-Radios})
$window.ShowDialog()
Now it's working as requested. In Function Check-Radios you can set your variables depending on choosen Radiobutton
This example meets your requirements that when you press "Next" the correct selection of the radio button is displayed. This should take you further.
[xml]$xaml = #"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window"
Title="Maker_2" Height="770" Width="800">
<Grid x:Name="Maker_2_Grid">
<Button x:Name="Maker_2_ButtonBack" Content="Back" HorizontalAlignment="Left" Margin="360,525,0,0" VerticalAlignment="Top" Width="120" Height="35"/>
<Button x:Name="Maker_2_ButtonNext" Content="Next" HorizontalAlignment="Left" Margin="485,525,0,0" VerticalAlignment="Top" Width="120" Height="35"/>
<Button x:Name="Maker_2_ButtonCancel" Content="Cancel" HorizontalAlignment="Left" Margin="610,525,0,0" VerticalAlignment="Top" Width="120" Height="35" IsCancel="True"/>
<Image x:Name="Maker_2_ImageLogo" HorizontalAlignment="Left" Height="44.264" Margin="480.157,30.312,0,0" VerticalAlignment="Top" Width="273.843" Source="logo.png" RenderTransformOrigin="0.5,0.5"/>
<TextBlock x:Name="Maker_2_TextMaker_2" HorizontalAlignment="Left" Margin="70,140,0,0" TextWrapping="Wrap" Text="Please select the maker of the device:" VerticalAlignment="Top"/>
<RadioButton x:Name="Maker_2_RadioMaker1" Content="Canon" VerticalAlignment="Center" Margin="272.25,220.25,351.75,333.75" Height="16" Width="130" GroupName="Makers_2" FontWeight="Bold"/>
<RadioButton x:Name="Maker_2_RadioMaker2" Content="Epson" VerticalAlignment="Center" Margin="272.25,260.25,351.75,293.75" Height="16" Width="130" GroupName="Makers_2" FontWeight="Bold"/>
</Grid>
</Window>
"#
function Check-Radios
{
$Global:radioSelected = $this;
}
$radioSelected = $null
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)
$window.FindName("Maker_2_RadioMaker1").add_Checked({Check-Radios})
$window.FindName("Maker_2_RadioMaker2").add_Checked({Check-Radios})
$window.FindName("Maker_2_ButtonNext").add_Click({Write-Host "You have selected: $($radioSelected.Content)"});
$window.ShowDialog()
EDIT:
here an example for an object that holds your printerdata
$printerList = [System.Collections.ArrayList]::new()
$printerList.Add([pscustomobject]#{Maker = "Canon";Model = "Pixma";ModelFull="Pixma0815";driver = "driverxyz1"})
$printerList.Add([pscustomobject]#{Maker = "Epson";Model = "Workforce";ModelFull="Workforce2020";driver = "driverxyz2"})
You can simply rewrite your "next button ClickMethod" like this...
$window.FindName("Maker_2_ButtonNext").add_Click({ Write-Host $($printerList | where Maker -eq $($radioSelected.Content)) });
it will give you the printers with selected maker
With Navigationwindow and pages you can now go to the page depending in selected printer.
Best Regards
Unfortunately, importing wpf as xaml for powershell is for me not entirely functional
I designed an example for you in which all elements were created in the code and also realized that you first select the manufacturer (radio buttons) and on the next page the model or full model, both are listed as an example. But you actually only need the combo with the full model.
here it is with Powershell
using namespace System.Windows
using namespace System.Windows.Controls
using namespace System.Windows.Navigation
[System.Reflection.Assembly]::LoadWithPartialName("PresentationFramework")
[System.Reflection.Assembly]::LoadWithPartialName("PresentationCore")
[System.Reflection.Assembly]::LoadWithPartialName("WindowsBase")
function Check-Radios
{
$Global:selectedMaker = $this.Content
}
$printerList = [System.Collections.ArrayList]::new()
$printerList.Add([pscustomobject]#{Maker = "Canon";Model = "Pixma";ModelFull="Pixma0815";driver = "driverxyz1"})
$printerList.Add([pscustomobject]#{Maker = "Canon";Model = "Pixma";ModelFull="Pixma1615";driver = "driverxyz1"})
$printerList.Add([pscustomobject]#{Maker = "Epson";Model = "Workforce";ModelFull="Workforce2020";driver = "driverxyz2"})
$selectedMaker = "Canon";
$navigationWindow = [NavigationWindow]::new()
$navigationPage1 = [Page]::new()
$navigationPage2 = [Page]::new()
#Page with printers
$printerLabel = [Label]::new()
$printerLabel.Content = ($printerList | where Maker -eq $selectedMaker | Select -First 1).Maker
$printerModelCombo = [ComboBox]::new()
$printerModelFullCombo = [ComboBox]::new()
$printerStackPanel = [StackPanel]::new()
$printerStackPanel.AddChild($printerLabel)
$printerStackPanel.AddChild($printerModelCombo)
$printerStackPanel.AddChild($printerModelFullCombo)
$navigationPage2.Content = $printerStackPanel
#Page with radiobuttons
$radioButton1 = [RadioButton]::new()
$radioButton1.Content ="Canon"
$radioButton1.Name = "RadioCanon"
$radioButton2 = [RadioButton]::new()
$radioButton2.Content ="Epson"
$radioButton2.Name = "RadioEpson"
$radioButton1.add_Checked({Check-Radios})
$radioButton2.add_Checked({Check-Radios})
$button1 = [Button]::new()
$button1.Content = "Next"
$button1.add_Click(
{
foreach($entry in ($printerList | where Maker -eq $selectedMaker))
{
# Model
if($printerModelCombo.Items.IsEmpty -or !$printerModelCombo.Items.Content.Contains($entry.Model))
{
$printerModelComboItem = [ComboBoxItem]::new()
$printerModelComboItem.Content = $entry.Model
$printerModelCombo.AddChild($printerModelComboItem)
}
# Model Full
$printerModelFullComboItem = [ComboBoxItem]::new()
$printerModelFullComboItem.Content = $entry.ModelFull
$printerModelFullCombo.AddChild($printerModelFullComboItem)
}
$printerModelCombo.SelectedIndex = 0
$printerModelFullCombo.SelectedIndex = 0
$navigationWindow.NavigationService.Content = $navigationPage2
})
$grid = [Grid]::new()
$gridRowDef1 = [RowDefinition]::new()
$gridRowDef2 = [RowDefinition]::new()
$gridRowDef1.Height ="4*"
$gridRowDef2.Height ="1*"
$grid.RowDefinitions.Add($gridRowDef1)
$grid.RowDefinitions.Add($gridRowDef2)
$stackPanel = [StackPanel]::new()
$stackPanel.AddChild($radioButton1)
$stackPanel.AddChild($radioButton2)
$grid.AddChild($stackPanel)
$grid.AddChild($button1)
[Grid]::SetRow($stackPanel,0)
[Grid]::SetRow($button1,1)
$navigationPage1.Content = $grid;
$navigationWindow.Title = "Navigation"
$navigationWindow.Width = 960
$navigationWindow.Height = 600
$navigationWindow.NavigationService.Content = $navigationPage1
$navigationWindow.ShowsNavigationUI = $false
$navigationWindow.ShowDialog()
Related
I'm coming with more problems to you.
I've built a simple GUI to create a printer or change the driver on a print server. However, when I click the button RUN, it does not make anything just only pops up the message that the operation was successful. May somebody help me checking the code and tell me what I am doing wrong?
#region pop up the gui
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
Add-Type -AssemblyName PresentationFramework
[xml]$xaml = #"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Print Queue Configuration Tool" Height="800" Width="800" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Topmost="True">
<Grid>
<TextBlock HorizontalAlignment="Left" Margin="24,85,0,0" TextWrapping="Wrap" Text="Welcome to Print Queue Configuration Tool" VerticalAlignment="Top" Width="456" FontSize="22" FontWeight="Bold"/>
<Image HorizontalAlignment="Left" Height="41" Margin="537,36,0,0" VerticalAlignment="Top" Width="257" Source="logo.png"/>
<TextBlock HorizontalAlignment="Left" Margin="72,140,0,0" TextWrapping="Wrap" Text="What do you want to do?" VerticalAlignment="Top" RenderTransformOrigin="0.422,0.157" Height="31" Width="257" FontSize="16"/>
<RadioButton x:Name="RadioCreate" Content="Create a print queue." HorizontalAlignment="Left" Margin="102,194,0,0" VerticalAlignment="Top" IsChecked="True"/>
<RadioButton x:Name="RadioChange" Content="Change driver to a print queue." HorizontalAlignment="Left" Margin="311,194,0,0" VerticalAlignment="Top"/>
<ComboBox x:Name="ComboMaker" HorizontalAlignment="Left" Margin="460,260,0,0" VerticalAlignment="Top" Width="290">
<ComboBoxItem Content="Dascom"/>
<ComboBoxItem Content="HP"/>
<ComboBoxItem Content="Kyocera"/>
<ComboBoxItem Content="Lexmark"/>
<ComboBoxItem Content="Zebra"/>
</ComboBox>
<ComboBox x:Name="ComboModel" HorizontalAlignment="Left" Margin="460,310,0,0" VerticalAlignment="Top" Width="290">
</ComboBox>
<ComboBox x:Name="ComboTray" HorizontalAlignment="Left" Margin="460,360,0,0" VerticalAlignment="Top" Width="290">
</ComboBox>
<ComboBox x:Name="ComboDriver" HorizontalAlignment="Left" Margin="460,410,0,0" VerticalAlignment="Top" Width="290">
</ComboBox>
<TextBox x:Name="TextBoxPQ" HorizontalAlignment="Left" Height="22" Margin="460,460,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="290" SelectionOpacity="4"/>
<TextBox x:Name="TextBoxServer" HorizontalAlignment="Left" Height="22" Margin="460,510,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="290"/>
<TextBox x:Name="TextBoxHostname" HorizontalAlignment="Left" Height="22" Margin="460,560,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="290"/>
<TextBox x:Name="TextBoxLocation" HorizontalAlignment="Left" Height="22" Margin="460,610,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="290"/>
<TextBlock HorizontalAlignment="Left" Margin="72,260,0,0" TextWrapping="Wrap" Text="Select the maker:" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
<TextBlock HorizontalAlignment="Left" Margin="72,310,0,0" TextWrapping="Wrap" Text="Select the model:" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
<TextBlock HorizontalAlignment="Left" Margin="72,360,0,0" TextWrapping="Wrap" Text="Select the no. of additional trays (only for model CX825, CX921, CX922, MX910, X950):" VerticalAlignment="Top" Width="304" Height="32" FontWeight="Bold"/>
<TextBlock HorizontalAlignment="Left" Margin="72,410,0,0" TextWrapping="Wrap" Text="Select the driver:" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
<TextBlock HorizontalAlignment="Left" Margin="72,460,0,0" TextWrapping="Wrap" Text="Add the print queue name:" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
<TextBlock HorizontalAlignment="Left" Margin="72,510,0,0" TextWrapping="Wrap" Text="Add the server name:" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
<TextBlock HorizontalAlignment="Left" Margin="72,560,0,0" TextWrapping="Wrap" Text="Add the hostname:" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
<TextBlock HorizontalAlignment="Left" Margin="72,610,0,0" TextWrapping="Wrap" Text="Add the location (i.e. DE-Eltmann):" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
<Button x:Name="ButtonRun" Content="Run" HorizontalAlignment="Left" Margin="660,710,0,0" VerticalAlignment="Top" Width="90" Height="35"/>
</Grid>
</Window>
"#
#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)
#Add actions to button Run
$RadioCreate = $window.FindName("RadioCreate")
$RadioChange = $window.FindName("RadioChange")
$ComboMaker = $window.FindName("ComboMaker")
$ComboModel = $window.FindName("ComboModel")
$ComboDriver = $window.FindName("ComboDriver")
$ComboMaker.Add_DropdownClosed({
$ComboModel.Items.Clear()
$ComboDriver.Items.Clear()
switch ($ComboMaker.Text) {
"Dascom" {
$ComboModel.Items.Add("Dascom T2150s")
$ComboModel.Items.Add("Dascom T2380")
}
"HP" {
$ComboModel.Items.Add("HP LaserJet M507dn")
$ComboModel.Items.Add("HP LaserJet M700 M712dn")
$ComboModel.Items.Add("HP Color LaserJet M553dn")
$ComboModel.Items.Add("HP OfficeJet Pro 8210")
$ComboModel.Items.Add("HP PageWide Pro 452dw")
$ComboModel.Items.Add("HP PageWide Enterprise Color 556dn")
$ComboModel.Items.Add("HP DesignJet T1600")
}
"Kyocera" {
$ComboModel.Items.Add("Kyocera P3155dn")
}
"Lexmark" {
$ComboModel.Items.Add("Lexmark CX625adhe")
$ComboModel.Items.Add("Lexmark CX825dte")
$ComboModel.Items.Add("Lexmark CX921de")
$ComboModel.Items.Add("Lexmark CX922de")
$ComboModel.Items.Add("Lexmark MX722adhe")
$ComboModel.Items.Add("Lexmark MS725dvn")
$ComboModel.Items.Add("Lexmark MX910de")
$ComboModel.Items.Add("Lexmark X950de")
}
"Zebra" {
$ComboModel.Items.Add("Zebra ZD620t 300 dpi")
$ComboModel.Items.Add("Zebra ZT620 300 dpi")
$ComboModel.Items.Add("Zebra 220Xi4 300 dpi")
$ComboModel.Items.Add("Zebra ZT610-300 dpi")
$ComboModel.Items.Add("Zebra ZQ630 Mobile Printer")
}
}
switch ($ComboMaker.Text) {
"Dascom" {
$ComboDriver.Items.Add("Tally T2150_")
}
"HP" {
$ComboDriver.Items.Add("HP DesignJet HPGL2 Driver")
$ComboDriver.Items.Add("HP Designjet T790ps 44in PS3")
$ComboDriver.Items.Add("HP DesignJet T930 HPGL2")
$ComboDriver.Items.Add("HP DesignJet Universal Print Driver HPGL2")
$ComboDriver.Items.Add("HP DesignJet Universal Print Driver HPGL2(v4.5.0)")
$ComboDriver.Items.Add("HP Universal Printing PS")
$ComboDriver.Items.Add("HP Universal Printing PS (v6.3.0)")
$ComboDriver.Items.Add("HP Universal Printing PS (v6.4.1)")
}
"Kyocera" {
$ComboDriver.Items.Add("Kyocera Classic Universaldriver KPDL")
$ComboDriver.Items.Add("Kyocera Classic Universaldriver KPDL (A4 models)")
}
"Lexmark" {
$ComboDriver.Items.Add("Lexmark Universal v2 PS3")
$ComboDriver.Items.Add("Lexmark Universal v2 PS3 (2.10.0.0)")
$ComboDriver.Items.Add("Lexmark Universal v2 PS3 (2.11.0.0)")
$ComboDriver.Items.Add("Lexmark Universal v2 PS3 (2.13.0.0)")
$ComboDriver.Items.Add("Lexmark Universal v2 PS3 (2.14.0.0)")
$ComboDriver.Items.Add("Lexmark Universal v2 PS3 (2.14.1.0)")
}
"Zebra" {
$ComboDriver.Items.Add("ZDesigner 110Xi4 203 dpi")
$ComboDriver.Items.Add("ZDesigner ZDesigner 110Xi4 300 dpi")
$ComboDriver.Items.Add("ZDesigner 220Xi4 300 dpi")
$ComboDriver.Items.Add("ZDesigner GX430t")
$ComboDriver.Items.Add("ZDesigner QLn420 (CPCL)")
$ComboDriver.Items.Add("ZDesigner QLn420 (ZPL)")
$ComboDriver.Items.Add("ZDesigner S4M-203dpi ZPL")
$ComboDriver.Items.Add("ZDesigner ZD620-300dpi ZPL")
$ComboDriver.Items.Add("ZDesigner ZM400 200 dpi (EPL)")
$ComboDriver.Items.Add("ZDesigner ZM400 200 dpi (ZPL)")
$ComboDriver.Items.Add("ZDesigner ZM400 300 dpi (ZPL)")
$ComboDriver.Items.Add("ZDesigner ZM600 200 dpi (ZPL)")
$ComboDriver.Items.Add("ZDesigner ZQ510 (ZPL)")
$ComboDriver.Items.Add("ZDesigner ZQ630 (ZPL)")
$ComboDriver.Items.Add("ZDesigner ZT230-200dpi ZPL")
$ComboDriver.Items.Add("ZDesigner ZT410-300dpi ZPL")
$ComboDriver.Items.Add("ZDesigner ZT420-300dpi ZPL")
$ComboDriver.Items.Add("ZDesigner ZT610-300dpi ZPL")
$ComboDriver.Items.Add("ZDesigner ZT610R-203dpi ZPL")
$ComboDriver.Items.Add("ZDesigner ZT610R-300dpi ZPL")
}
}
switch ($ComboModel.Text) {
"Lexmark CX825dte" {
$ComboTray.Items.Add("3")
}
"Lexmark CX921de" {
$ComboTray.Items.Add("0")
$ComboTray.Items.Add("1")
$ComboTray.Items.Add("2")
$ComboTray.Items.Add("3")
}
"Lexmark CX922de" {
$ComboTray.Items.Add("0")
$ComboTray.Items.Add("1")
$ComboTray.Items.Add("2")
$ComboTray.Items.Add("3")
}
"Lexmark MX910de" {
$ComboTray.Items.Add("0")
$ComboTray.Items.Add("1")
$ComboTray.Items.Add("2")
$ComboTray.Items.Add("3")
}
"Lexmark X950de" {
$ComboTray.Items.Add("0")
$ComboTray.Items.Add("1")
$ComboTray.Items.Add("2")
$ComboTray.Items.Add("3")
}
}
})
<# this part contains the variable assignment but not working
$maker = $ComboMaker.SelectedItem
$modelfull = $ComboModel.SelectedItem
$tray = $ComboTray.SelectedItem
$driver = $ComboDriver.SelectedItem
$TextBoxPQ = $window.FindName("TextBoxPQ")
$PQ = $TextBoxPQ.Text
$TextBoxServer = $window.FindName("TextBoxServer")
$server = $TextBoxServer.Text
$TextBoxHostname = $window.FindName("TextBoxHostname")
$hostname = $TextBoxHostname.Text
$TextBoxLocation = $window.FindName("TextBoxLocation")
$location = $TextBoxLocation.Text
#>
$ButtonRun = $window.FindName("ButtonRun")
$ButtonRun.Add_Click({
# here variables are not working as well
$maker = $ComboMaker.SelectedItem
$modelfull = $ComboModel.SelectedItem
$tray = $ComboTray.SelectedItem
$driver = $ComboDriver.SelectedItem
$TextBoxPQ = $window.FindName("TextBoxPQ")
$PQ = $TextBoxPQ.Text
$TextBoxServer = $window.FindName("TextBoxServer")
$server = $TextBoxServer.Text
$TextBoxHostname = $window.FindName("TextBoxHostname")
$hostname = $TextBoxHostname.Text
$TextBoxLocation = $window.FindName("TextBoxLocation")
$location = $TextBoxLocation.Text
if ($RadioCreate.Checked -eq $true){
PortCreation
PrinterCreation
if ($maker -eq 'Lexmark') {
ApplyConfigLexmark
}
}
if ($RadioChange.Checked -eq $true) {
ChangeDriver
if ($maker -eq 'Lexmark') {
ApplyConfigLexmark
}
}
[System.Windows.MessageBox]::Show('Operation completed!')
})
#Show form
$window.ShowDialog() | Out-Null
#endregion
Also I would like to link the model selection to another variable called 'model' that will use a shortcut of the model to deploy some specific settings and once the Operation success mesasge' pops up and you press OK, all comboboxes and texboxes reset to blank.
FYI: PortCreation, PrinterCreation, ChangeDriver and ApplyConfigLexmark are functions that execute code in Powershell so I am not putting the whole code. Variables that I am using are below:
$maker # choose the brand of the physical device
$model # short name of the device model
$modelfull # Full device model
$tray # number of the trays the device has
$driver # Driver name
$PQ # print queue name
$server # hostname of the server
$hostname # hostname of the printer
$location # location of the device
I tested your code and changed this line to show the maker and it works. Why do you say the variable is not binding?
[System.Windows.MessageBox]::Show('Operation completed!' + $maker)
Also Checked should be IsChecked
if ($RadioCreate.IsChecked -eq $true){
I have a configuration script in powershell that I am trying to marry with a WFP GUI. After a few failed forays with code and gui in the same thread I have been reading a number of pages about running the GUI in its own runspace. Below is the code that I have pieced together.
The form runs but I get this error related to the button click I am trying to use to hide/unhide grids.
You cannot call a method on a null-valued expression.
At C:\Projects\AVPC Setup\Working Files\MT Trial 3.ps1:218 char:18
+ $syncHash.fullConfig.Add_Click({
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
With the form running these variables are visible in the syncHash so I am not sure what I am missing.
$Global:syncHash = [hashtable]::Synchronized(#{})
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.Name = "Config GUI"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
$psCmd = [PowerShell]::Create().AddScript({
[xml]$xaml = #"
<Window x:Class="psguiconfig.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:psguiconfig"
mc:Ignorable="d"
Title="Config" Height="175" Width="500">
<Grid x:Name="mainGrid" HorizontalAlignment="Left" Height="144" Margin="0,1,0,0" VerticalAlignment="Top" Width="490">
<Grid x:Name="choiceGrid" HorizontalAlignment="Left" Height="144" Margin="0,1,0,0" VerticalAlignment="Top" Width="490" Visibility="Visible">
<Label Content="Choose Config Type" HorizontalAlignment="Center" Height="26" Margin="0,0,0,60" VerticalAlignment="Center" FontWeight="SemiBold"/>
<Button x:Name="fullConfig" Content="Full" HorizontalAlignment="Center" Margin="0,45,250,0" VerticalAlignment="Center" Width="75"/>
<Button x:Name="stepConfig" Content="Step By Step" HorizontalAlignment="Center" Margin="0,45,0,0" VerticalAlignment="Center" Width="75"/>
<Button x:Name="upgradeConfig" Content="Upgrade" HorizontalAlignment="Center" Margin="250,45,0,0" VerticalAlignment="Center" Width="75" IsEnabled="False"/>
</Grid>
<Grid x:Name="fullGrid" HorizontalAlignment="Left" Height="144" Margin="0,1,0,0" VerticalAlignment="Top" Width="490" Visibility="Hidden">
<Label x:Name="fullLabel" Content="Full Config" HorizontalAlignment="Center" Height="26" Margin="0,0,0,60" VerticalAlignment="Center" FontWeight="SemiBold"/>
<ProgressBar x:Name="fullProgress" HorizontalAlignment="Center" Height="20" Margin="0,40,0,0" VerticalAlignment="Center" Width="400"/>
<Label x:Name="fullProgressLabel" Content="Tasking Stuffs" Foreground="#FFFFFF" HorizontalAlignment="Center" Height="20" Margin="45,82,245,42" VerticalAlignment="Center" FontWeight="SemiBold" FontSize="7" Width="200"/>
<Label x:Name="fullProgressPercentageLabel" Content="0%" Foreground="#FFFFFF" HorizontalContentAlignment="Right" Height="20" Margin="419,82,45,42" VerticalAlignment="Center" FontWeight="SemiBold" FontSize="7" Width="26"/>
</Grid>
</Grid>
</Window>
"#
$AttributesToRemove = #(
'x:Class',
'mc:Ignorable'
)
foreach ($Attrib in $AttributesToRemove) {
if ( $xaml.Window.GetAttribute($Attrib) ) {
$xaml.Window.RemoveAttribute($Attrib)
}
}
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$syncHash.Window=[Windows.Markup.XamlReader]::Load( $reader )
[xml]$XAML = $xaml
$xaml.SelectNodes("//*[#*[contains(translate(name(.),'n','N'),'Name')]]") | %{
#Find all of the form types and add them as members to the synchash
$syncHash.Add($_.Name,$syncHash.Window.FindName($_.Name) )
}
$syncHash.Window.ShowDialog() | Out-Null
$syncHash.Error = $Error
})
$psCmd.Runspace = $newRunspace
$data = $psCmd.BeginInvoke()
Function Update-Window {
Param (
$Control,
$Property,
$Value,
[switch]$AppendContent
)
If ($Property -eq "Close") {
$syncHash.Window.Dispatcher.invoke([action]{$syncHash.Window.Close()},"Normal")
Return
}
$syncHash.$Control.Dispatcher.Invoke([action]{
If ($PSBoundParameters['AppendContent']) { $syncHash.$Control.AppendText($Value) } Else { $syncHash.$Control.$Property = $Value }
}, "Normal")
}
$syncHash.fullConfig.Add_Click({
Update-Window -Control choiceGrid -Property Visibility -Value Hidden
Update-Window -Control fullGrid -Property Visibility -Value Visible
})
I have two text boxes. I would like to mirror the first text box to the second text box, but if you manually update the second box after the text has been mirrored, it should stay as that regardless of if you update the first text box again.
<TextBox x:Name="TextBoxUserLogonNameUPN" HorizontalAlignment="Left" Height="23" Margin="10,41,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="300"/>
<TextBox x:Name="TextBoxUserLogonNameSamAccountName" HorizontalAlignment="Left" Height="23" Margin="326,100,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="310" Text="{Binding Path=Text, ElementName=TextBoxUserLogonNameUPN, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
I have tried the above but with Mode=OneWay in the Binding. If I update the first text box a second time, after manually updating the second, it wipes what was in the second and mirrors again.
I am using WPF with PowerShell. I am happy to manage this by the Powershell if it can't be done in Xaml.
Assuming that textbox1 losing focus is the event that should copy textbox1 text to textbox2, this code should work for you. All the action is in the function textBox1_LostFocus. The first time through, simply set the textbox2Tagproperty to a value that you test for second and subsequent times through
Note: The form has two labels and two text boxes
WpfWindow1.xaml.ps1 is the entry point for this program
WpfWindow1.xaml.ps1
function Add-ControlVariables {
New-Variable -Name 'textBox1' -Value $window.FindName('textBox1') -Scope 1 -Force
New-Variable -Name 'textBox2' -Value $window.FindName('textBox2') -Scope 1 -Force
}
function Load-Xaml {
[xml]$xaml = Get-Content -Path $PSScriptRoot\WpfWindow1.xaml
$manager = New-Object System.Xml.XmlNamespaceManager -ArgumentList $xaml.NameTable
$manager.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml");
$xaml.SelectNodes("//*[#x:Name='textBox1']", $manager)[0].RemoveAttribute('TextChanged')
$xaml.SelectNodes("//*[#x:Name='textBox2']", $manager)[0].RemoveAttribute('TextChanged')
$xaml.SelectNodes("//*[#x:Name='textBox1']", $manager)[0].RemoveAttribute('LostFocus')
$xamlReader = New-Object System.Xml.XmlNodeReader $xaml
[Windows.Markup.XamlReader]::Load($xamlReader)
}
function Set-EventHandlers {
$textBox1.add_TextChanged({
param([System.Object]$sender,[System.Windows.Controls.TextChangedEventArgs]$e)
textBox1_TextChanged($sender,$e)
})
$textBox2.add_TextChanged({
param([System.Object]$sender,[System.Windows.Controls.TextChangedEventArgs]$e)
textBox2_TextChanged($sender,$e)
})
$textBox1.add_LostFocus({
param([System.Object]$sender,[System.Windows.RoutedEventArgs]$e)
textBox1_LostFocus($sender,$e)
})
}
$window = Load-Xaml
Add-ControlVariables
Set-EventHandlers
function textBox1_TextChanged
{
param($sender, $e)
#do stuff you want to do when textBox1.Text changes
}
function textBox2_TextChanged
{
param($sender, $e)
#do stuff you want to do when textBox2.Text changes
}
function textBox1_LostFocus
{
param($sender, $e)
if ($textBox2.Tag -ne "mirrored")
{
#turn off textBox2 TextChanged event so that code in textBox2_TextChanged is not executed
$textBox2.remove_TextChanged({
param([System.Object]$sender,[System.Windows.Controls.TextChangedEventArgs]$e)
textBox2_TextChanged($sender,$e)
})
$textBox2.Text = $sender.Text
$textBox2.Tag = "mirrored"
#turn textBox2 TextChanged event back on
$textBox2.add_TextChanged({
param([System.Object]$sender,[System.Windows.Controls.TextChangedEventArgs]$e)
textBox2_TextChanged($sender,$e)
})
}
}
$window.ShowDialog()
WpfWindow1.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="29" Margin="79,45,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="132" TextChanged="textBox1_TextChanged" LostFocus="textBox1_LostFocus"/>
<TextBox x:Name="textBox2" HorizontalAlignment="Left" Height="29" Margin="298,45,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="105" TextChanged="textBox2_TextChanged"/>
<Label Content="Text1:" HorizontalAlignment="Left" Height="25" Margin="32,45,0,0" VerticalAlignment="Top" Width="63"/>
<Label Content="Text2:" HorizontalAlignment="Left" Height="25" Margin="242,45,0,0" VerticalAlignment="Top" Width="56"/>
</Grid>
</Window>
So I'm developing a Powershell based WPF application and I am receiving the following error:
Exception calling "ShowDialog" with "0" argument(s): "Unidentified: USB_Disable="
At C:\Users\miste\OneDrive\Documents\Visual Studio 2015\Projects\Vaccinate-USB\Vaccinate-USB\Vaccinate-USB.ps1:344 char:5
+ $Form.ShowDialog() | out-null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : RuntimeException
Now the fairly odd thing is that the "USB_Disable=" string is only found in a hereString that never comes into contact with $Form.ShowDialog(). Below is an excerpt of the initializer function that contains the hereString, then the form loader function. If you need the full code let me know(it's about 800 lines)
The initializer:
function Initialize-ARD
{
if (-not (Test-Admin))
{
$wshpop = New-Object -ComObject Wscript.Shell
$wshpop.Popup('Please run as an administrator.',0,'Error!',0x0)
exit
}
if(-not (Test-Path -Path $ARDHome))
{
if(-not (Test-IsNetConnected))
{
$wshpop = New-Object -ComObject Wscript.Shell
$wshpop.Popup('Please make sure you have an internet connection for first run. Connect to the internet and the re-run the program.',0,'Error!',0x0)
exit
}
New-Item -Path $ARDHome -ItemType 'Directory' -Force
New-Item -Path "$ARDHome\config.ini" -ItemType 'File' -Force
#HERESTRING STARTS HERE!
Set-Content -Path "$ARDHome\config.ini" -Force -Value #'
[USB]
USB_Block=False
USB_Lock=False
USB_Delete=False
USB_Disable=False
[CD]
CD_Block=False
CD_Lock=False
CD_Delete=False
CD_Disable=False
[Firewall]
Firewall_Enabled=False
Firewall_Disabled=True
Firewall_UpdateFreq=0.5
'#
#New-Download -URL 'http://ha.x10.bz/package.zip/' -Destination "$ARDHome\package.zip"
#Expand-ZIPFile -File "$ARDHome\package.zip" -Destination $ARDHome
#Import-Module "$ARDHome\DeviceManagment\DeviceManagement.psd1" -Force
}
else{}
$script:ini = Get-IniFile -FilePath "$ARDHome\config.ini"
$USBBlock = $ini.USB.USB_Block
$USBLock = $ini.USB.USB_Lock
$USBDelete = $ini.USB.USB_Delete
$USBDisable = $ini.USB.USB_Disable
$CDBlock = $ini.CD.CD_Block
$CDLock = $ini.CD.CD_Lock
$CDDelete = $ini.CD.CD_Delete
$CDEject = $ini.CD.CD_Eject
$FirewallEnabled = $ini.Firewall.Firewall_Enabled
$FirewallDisabled = $ini.Firewall.Firewall_Disabled
$FirewallValue = $ini.Firewall.Firewall_UpdateFreq
if($USBBlock -eq "True")
{
$USBBlock = $true
}
else
{
$USBBlock = $false
}
if($USBLock -eq "True")
{
$USBLock = $true
}
else
{
$USBLock = $false
}
if($USBDelete -eq "True")
{
$USBDelete = $true
}
else
{
$USBDelete = $false
}
if($USBDisable -eq "True")
{
$USBDisable = $true
}
else
{
$USBDisable = $false
}
if($CDBlock -eq "True")
{
$CDBlock = $true
}
else
{
$CDBlock = $false
}
if($CDDelete -eq "True")
{
$CDDelete = $true
}
else
{
$CDDelete = $false
}
if($CDEject -eq "True")
{
$CDEject = $true
}
else
{
$CDEject = $false
}
if($CDLock -eq "True")
{
$CDLock = $true
}
else
{
$CDLock = $false
}
if($FirewallEnabled -eq "True")
{
$FirewallEnabled = $true
}
else
{
$FirewallEnabled = $false
}
if($FirewallDisabled -eq "True")
{
$FirewallDisabled = $true
}
else
{
$FirewallDisabled = $false
}
$USB_Block.IsChecked = $USBBlock
$USB_Lock.IsChecked = $USBLock
$USB_Delete.IsChecked = $USBDelete
$USB_Disable.IsChecked = $USDisable
$CD_Block.IsChecked = $CDBlock
$CD_Lock.IsChecked = $CDLock
$CD_Delete.IsChecked = $CDDelete
$CD_Eject.IsChecked = $CDEject
$Firewall_Enabled.IsChecked = $FirewallEnabled
$Firewall_Disabled.IsChecked = $FirewallDisabled
$Firewall_UpdateFreq.Value = $FirewallValue
}
The form and its loader:
function Initialize-Window
{
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = #"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Autorun Defender" Height="376" Width="528" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize" Background="Black" Opacity="1">
<Grid Margin="0,10,4,1">
<TabControl Name="tabControl" HorizontalAlignment="Left" Height="285.5" VerticalAlignment="Top" Width="493" Margin="10,0,0,0" Background="#FF2E2E2E">
<TabItem Header="Welcome" Background="#FF2E2E2E" Margin="-2,0,2,0" Width="80">
<Grid Background="#FF2E2E2E">
<Viewbox HorizontalAlignment="Left" Margin="10,0,0,87.5" Width="116">
<Image Name="image" Height="100" Width="100" Source="$(Get-ScriptDirectory)\Logo.png"/>
</Viewbox>
<Label Name="title1" Content="Autorun" HorizontalAlignment="Left" Margin="126,0,0,0" VerticalAlignment="Top" Height="67" Width="191" FontSize="48" FontStyle="Italic" FontWeight="Bold" FontFamily="Harlow Solid Italic"/>
<Label Name="title2" Content="Defender" HorizontalAlignment="Left" Margin="206,67,0,87.5" Width="205" FontSize="48" FontStyle="Italic" FontWeight="Bold" FontFamily="Harlow Solid Italic"/>
<Label Name="copyright" Content="© 2015 LogoiLab" VerticalAlignment="Bottom" FontSize="12" Height="26.5" HorizontalAlignment="Left" Width="101" Foreground="#FFD4D4D4"/>
</Grid>
</TabItem>
<TabItem Header="USB" Background="#FF2E2E2E" Margin="-2,0,2,0" Width="80">
<Grid Background="#FF2E2E2E">
<GroupBox Name="USB_Passive" Header="Passive" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="224" Width="230" Foreground="#FFD4D4D4">
<Grid>
<CheckBox Name="USB_Block" Content="Block Autorun" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
<CheckBox Name="USB_Lock" Content="Lock Autorun Edits" Margin="10,30,89.656,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
</Grid>
</GroupBox>
<GroupBox Name="USB_Active" Header="Active" HorizontalAlignment="Left" Margin="249,10,0,0" VerticalAlignment="Top" Height="224" Width="230" Foreground="#FFD4D4D4">
<Grid>
<CheckBox Name="USB_Delete" Content="Delete Autorun.inf Files" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
<CheckBox Name="USB_Disable" Content="Disable USBs With Autorun" HorizontalAlignment="Left" Margin="10,30,0,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
</Grid>
</GroupBox>
</Grid>
</TabItem>
<TabItem Header="CD/DVD" Background="#FF2E2E2E" Margin="-2,0,2,0" Width="80">
<Grid Background="#FF2E2E2E">
<GroupBox Name="CD_Passive" Header="Passive" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="224" Width="230" Foreground="#FFD4D4D4">
<Grid>
<CheckBox Name="CD_Block" Content="Block Autorun" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
<CheckBox Name="CD_Lock" Content="Lock Autorun Edits" Margin="10,30,89.656,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
</Grid>
</GroupBox>
<GroupBox Name="CD_Active" Header="Active" HorizontalAlignment="Left" Margin="249,10,0,0" VerticalAlignment="Top" Height="224" Width="230" Foreground="#FFD4D4D4">
<Grid>
<CheckBox Name="CD_Delete" Content="Delete Autorun.inf Files" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
<CheckBox Name="CD_Eject" Content="Eject CDs With Autorun" HorizontalAlignment="Left" Margin="10,30,0,0" Width="227" Height="26" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
</Grid>
</GroupBox>
</Grid>
</TabItem>
<TabItem Header="Firewall" Background="#FF2E2E2E" Margin="-2,0,2,0" Width="80">
<Grid Background="#FF2E2E2E">
<TextBlock TextWrapping="WrapWithOverflow" Foreground="#FFD4D4D4">
This is a device firewall. It will block new devices from being "Pluged and Played". This type of firewall is meant to prevent such attacks as "BadUSB" or other hardware attacks. Please note that this may cause system errors(especially if your system has a lot of software defined devices).
</TextBlock>
<RadioButton Background="black" Name="Firewall_Enabled" Content="Enable Device Firewall" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="30,83.04,0,0" Foreground="#FFD4D4D4"/>
<RadioButton Background="black" Name="Firewall_Disabled" Content="Disable Device Firewall" HorizontalAlignment="Left" Margin="30,103,0,111.54" Foreground="#FFD4D4D4"/>
<TextBlock TextWrapping="WrapWithOverflow" Height="87.12" Margin="10,0" VerticalAlignment="Bottom" Foreground="#FFD4D4D4">
Check Frequency:
<LineBreak/>
This is how often the firewall checks for new devices, set this very low if you're afraid of BadUSB or its variants.
</TextBlock>
<Slider Minimum=".50" Maximum="300" TickPlacement="BottomRight" TickFrequency="5" IsSnapToTickEnabled="True" Name="Firewall_UpdateFreq" Margin="10,0,75,10" Height="29.96" VerticalAlignment="Bottom" Value="{Binding Count}"/>
<Label Name="SliderValue_Label" Content="" Height="29.96" Margin="0,0,10,10" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="60" Foreground="#FFD4D4D4"/>
</Grid>
</TabItem>
<TabItem Header="Info" Background="#FF2E2E2E" Margin="-2,0,2,-1" RenderTransformOrigin="0.137,0.501" Height="46" VerticalAlignment="Top" Width="80" >
<Grid Background="#FF2E2E2E">
<Viewbox HorizontalAlignment="Left" Height="97.389" Margin="10,10,0,0" VerticalAlignment="Top" Width="100">
<Image Name="Avatar" Height="100" Width="100" Source="$(Get-ScriptDirectory)\LogoiLab.png"/>
</Viewbox>
<Label Name="username" Content="© 2015 LogoiLab" Margin="115,10,10,0" VerticalAlignment="Top" FontSize="24" FontWeight="Bold" FontStyle="Italic" Foreground="#FFD4D4D4"/>
<Label Name="quote" Content="~ The logos behind the pathos." Margin="115,51.923,10,0" VerticalAlignment="Top" FontWeight="Bold" FontStyle="Italic" Foreground="#FFD4D4D4"/>
<Button Name="link" Content="http://www.github.com/LogoiLab" VerticalAlignment="Top" Margin="115,79.656,10,0" Cursor="Hand" FontStyle="Oblique"/>
<WebBrowser Margin="10,115,10,10" Source="http://ha.x10.bz/AutorunDefender/changelog.html"/>
</Grid>
</TabItem>
<TabItem Header="Feedback" Background="#FF2E2E2E" Margin="-2,0,2,-1" RenderTransformOrigin="0.137,0.501" Height="46" VerticalAlignment="Top" Width="80" >
<Grid Background="#FF2E2E2E">
<WebBrowser Margin="10,10,10,10" Source="http://goo.gl/forms/B77vvnhLPV"/>
</Grid>
</TabItem>
</TabControl>
<Button Name="Apply" Content="Apply" HorizontalAlignment="Left" Margin="353,290.5,0,0" VerticalAlignment="Top" Width="150" Height="35.5"/>
<Label Name="Status_Label" VerticalContentAlignment="Center" Content="" Margin="10,0,170,10" VerticalAlignment="Bottom" Height="35.5" Foreground="White"/>
</Grid>
</Window>
"#
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$form=[Windows.Markup.XamlReader]::Load($reader)
$xaml.SelectNodes("//*[#Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name) -Scope Script }
$Firewall_Enabled.Add_Click({
$Firewall_Enabled.IsChecked = $True
$FireWall_Disabled.IsChecked = $False
})
$Firewall_Disabled.Add_Click({
$Firewall_Enabled.IsChecked = $False
$FireWall_Disabled.IsChecked = $True
})
$link.Add_Click({
Start-Process 'http://www.github.com/LogoiLab'
})
$Firewall_UpdateFreq.Add_ValueChanged({
$SliderValue_Label.Content = ($Firewall_UpdateFreq.value).ToString() + " Sec"
})
$Apply.Add_Click({
$Status_Label.ForeGround = 'yellow'
$Status_Label.Content = 'Applying Changes...'
Start-Handler
})
$Status_Label.Foreground = 'green'
$Status_Label.Content = 'Ready...'
$Form.Add_ContentRendered({
Initialize-ARD
})
#$Form.Add_Loaded({
# Initialize-ARD
#})
$Form.Add_Closing({
Deinitialize-Window
})
$Form.ShowDialog() | out-null
}
This is a very strange error that I have never encountered before, somehow the ShowDialog method is getting hold of a string that should never be parsed like that(not to mention not even be allowed to access).
Any help would be greatly appreciated!
The issue is with a .NET memory management bug, run a CCleaner and reboot your system.
I have few questions as I am usign XAML for the first time.
How do I use the button (BrowseButton) to browse a folder in Harddrive ?
In this case as the button is inside
Can I use the way I have shown below?
Actually first dockpanel will hold the image and some label and the other dockpanel will have tabcontrol in it.
If I have a tabcontrol, How can I add a listview which can increase the tabs during runtime.. and the listview should be available in the runtime also.
How to add a close("X") mark on the top of the tab to close the button
probably I asked a lot of questions, sorry :(
Please help
<Grid>
<DockPanel>
<StackPanel>
<Image Name="imgClientPhoto" HorizontalAlignment="Left" VerticalAlignment="Top" Width="auto" Height="auto" Grid.Column="0" Grid.Row="0" Margin="0"
Source="D:ehtmp_top_left.gif" MinWidth="450" MinHeight="100" Grid.IsSharedSizeScope="True">
</Image>
<Button x:Name="BrowseButton" Margin="0,13.638,30,14.362" Content="Browse" Click="BrowseButton_Click" HorizontalAlignment="Right" Width="111" />
<TextBox x:Name="txtBxBrowseTB" Margin="46,10,146,10" Text="TextBox" TextWrapping="Wrap" TextChanged="BrowseTB_TextChanged"></TextBox>
<Label HorizontalAlignment="Left" Margin="-14,22,0,10" Name="label1" Width="69.75" FontSize="13" VerticalContentAlignment="Top" HorizontalContentAlignment="Center">Path:</Label>
</Grid>
</GroupBox>
</Grid>
</StackPanel>
</DockPanel>
<DockPanel>
<StackPanel Margin="0,158,0,0" HorizontalAlignment="Left" Width="330" Height="557.5" VerticalAlignment="Top">
<TextBox Height="50" Name="textBox1" Width="240" Margin="0,35,0,0" VerticalAlignment="Stretch" HorizontalAlignment="Right" />
<ListBox Height="470" Name="listBox1" Width="332" Background="LightGray" Margin="0,0,0,0" BorderBrush="IndianRed" BorderThickness="3" />
</StackPanel>
<TabControl Height="234" Name="tabControl1" Width="1035" Margin="0,0,0,0">
<TabItem Header="tabItem1" Name="tabItem1">
<Grid Height="144" />
</TabItem>
</TabControl>
</DockPanel>
</Grid>
1) The browse code should be something like this:
private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.FolderBrowserDialog browse = new System.Windows.Forms.FolderBrowserDialog();
browse.RootFolder= Environment.SpecialFolder.MyDocuments;
browse.SelectedPath = "C:\\InitalFolder\\SomeFolder";
if (browse.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtBxBrowseTB.Text = browse.SelectedPath;
}
}
2) I have tried to simplify the xaml. Does this look like something you could go with?:
<Grid>
<Image Name="imgClientPhoto" Margin="0" Height="262" VerticalAlignment="Top" HorizontalAlignment="Left" ></Image>
<StackPanel VerticalAlignment="Stretch" >
<StackPanel VerticalAlignment="Top" Orientation="Horizontal" Height="30">
<Button Name="BrowseButton" Content="Browse" Click="BrowseButton_Click" HorizontalAlignment="Right" Width="111" />
<Label Name="label1" Width="69.75" FontSize="13" VerticalContentAlignment="Center" HorizontalContentAlignment="Right">Path:</Label>
<TextBox Name="txtBxBrowseTB" Width="200" Text="TextBox" VerticalContentAlignment="Center" TextWrapping="Wrap" TextChanged="BrowseTB_TextChanged"></TextBox>
</StackPanel>
<StackPanel>
<StackPanel Orientation="Vertical">
<TextBox Height="50" Name="textBox1" />
<ListBox Height="470" Name="listBox1" Background="LightGray" Margin="0,0,0,0" BorderBrush="IndianRed" BorderThickness="3" MouseLeftButtonUp="listBox1_MouseLeftButtonUp">
<ListBoxItem>User1</ListBoxItem>
<ListBoxItem>User2</ListBoxItem>
<ListBoxItem>User3</ListBoxItem>
<ListBoxItem>User4</ListBoxItem>
</ListBox>
</StackPanel>
<TabControl Name="tabControl1" />
</StackPanel>
</StackPanel>
</Grid>
You can also have a close button in the tab item Header. As many of the content properties in xaml controls, the content can actually be controls, and not necessary just a text.
<TabControl Name="tabControl1" >
<TabItem Name="tabItem1" >
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<Label>TabHeader1</Label>
<Button Height="20" Width="20" FontWeight="Bold" Click="CloseTabButton_Click">X</Button>
</StackPanel>
</TabItem.Header>
<Grid Height="144">
<ListView />
</Grid>
</TabItem>
</TabControl>
3) Here is how you would add the tabs in C# dynamically:
private void listBox1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
//Get selected item
ListBoxItem item = (ListBoxItem)listBox1.SelectedItem;
string itemText = item.Content.ToString();
//Check if item is already added
foreach (TabItem tItem in tabControl1.Items)
{
if ((string)tItem.Tag == itemText)
{
//Item already added, just activate the tab
tabControl1.SelectedItem = tItem;
return;
}
}
//Build new tab item
TabItem tabItem = new TabItem();
tabItem.Tag = itemText;
//First build the Header content
Label label = new Label();
Button button = new Button();
label.Content = itemText;
button.Content = "X";
button.Height = 20;
button.Width = 20;
button.FontWeight = FontWeights.Bold;
button.Click += CloseTabButton_Click; //Attach the event click method
button.Tag = tabItem; //Reference to the tab item to close in CloseTabButton_Click
StackPanel panel = new StackPanel();
panel.Orientation = Orientation.Horizontal;
panel.Children.Add(label);
panel.Children.Add(button);
tabItem.Header = panel;
//Then build the actual tab content
//If you need only the ListView in here, you don't need a grid
ListView listView = new ListView();
//TODO: Populate the listView with what you need
tabItem.Content = listView;
//Add the finished tabItem to your TabControl
tabControl1.Items.Add(tabItem);
tabControl1.SelectedItem = tabItem; //Activate the tab
}
private void CloseTabButton_Click(object sender, RoutedEventArgs e)
{
//The tab item was set to button.Tag when it was added
Button button = (Button)sender;
TabItem tabItem = (TabItem)button.Tag;
if (tabItem != null)
{
button.Click -= CloseTabButton_Click; //Detach event handler to prevent memory leak
tabControl1.Items.Remove(tabItem);
}
}