Click here to Skip to main content
15,616,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I was hoping maybe you could help me with a bit of code. I am
1. New to Object Oriented programming
2. Relatively new to Powershell
3. Struggling to get a good grasp on how to construct properly firing events that fire without a bad button lag. (interested in your background worker code bits)

I read your article and loved it as you seem to cover all these angles from a powershell WPF perspective

I was wondering why I cant seem to alter the visibility of a label from inside an event. Relevant code chunk is bolded MapPrinterButton Onclickevent

Other examples I've seen seem to be able to easily adjust the content of the label, but not its visibility, what am I missing?

Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
[XML] $XAML = Get-Content "C:\Users\5022375\Documents\Mainform2.xaml"
$XAML.Window .RemoveAttribute("x:Class" )
$Reader = New-Object System.Xml.XmlNodeReader $XAML
$MainForm = [Windows.Markup.XamlReader ]::Load($Reader )
# EVENT Handler

#addButton = $Window.FindName('addButton')
$PrintSelect = $Mainform. FindName('Printer_Select')
$MapPrinterButton = $Mainform.FindName( 'Map_Printers_Button')
$DefaultSelect = $Mainform. FindName('Default_Select')
$DefaultSelectButton = $Mainform.FindName( 'Set_Default_Button')
$SiteCodeLabel = $Mainform. FindName('Site_Code')
$SiteCodeLabel.Content = $clientname
$PrintersInstalled = Get-WmiObject Win32_Printer -ComputerName "." | ? Name -NotContains "session"
$TMSButton = $Mainform. FindName('Start_TMS')
$StatusLabel = $Mainform. FindName("MappingStatus")
$StatusLabel.visibility = "Hidden"

$DefaultSelect.Items .Clear()
Foreach ($x in $PrintersInstalled)
$DefaultSelect.Items .Add($x .Name. Substring(($x. Name.LastIndexOf( "\")+ 1)))


Foreach ($p in $Printerstochoose)

$PrintSelect.Items .Add($p )

$Printselect.Items .Refresh()

<big>    $MapPrinterButton .add_click({
    $StatusLabel = $Mainform. FindName("MappingStatus")
    $StatusLabel.visibility = "Visible"

$WS_Net = New-Object -com Wscript.Network

foreach ($p in $PrintSelect.SelectedItems)

$server = $Printersformatted | Where-Object Printer -EQ $p | Select Server -First 1
$Server = $server. server
$printstring = "\\$server \$p"
Write-Host "Working on Printer $P"
$WS_Net.AddWindowsPrinterConnection( "$printstring" )

$Maperror = $_. Exception.Message
Write-Host $Maperror -ForegroundColor Red
$PrintersInstalled = Get-WmiObject Win32_Printer -ComputerName "."

$DefaultSelect.Items .Clear()
Foreach ($x in $PrintersInstalled)
$DefaultSelect.Items .Add($x .Name. Substring(($x. Name.LastIndexOf( "\")+ 1)))
$StatusLabel.Visibility = "Hidden"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900