Asked by:
Having to click twice on a gui button for it to work.

Question
-
Hi,
I have created a gui using xaml to work with my powershell script. It basically works as I want it to but I have to click on a button twice before it will output the expected result. The relevant snippet is:
##################
function get-selecteddate { foreach ($v in $NewExpiry) {
if ($WPFlistView.SelectedItem.name -eq $v.name) {
$v.'New Expiry Date' = $WPFDateSelector.Selecteddate
$tempname = $v.name
$tempdate = $v.'New Expiry Date'.ToString("dd.MM.yyyy")
$WPFlistbox.AddChild("$tempname will be extended to $tempdate")
}
}
}
$WPFSelectButton.Add_Click({get-selecteddate})################
This adds some text to a listbox successfully but only on the second time the button is clicked. I am very new to creating GUIs, so it may be something obvious.
Thanks for any help!
Thursday, January 11, 2018 10:54 AM
All replies
-
$WPFSelectButton_click = { foreach ($v in $NewExpiry) { if ($WPFlistView.SelectedItem.name -eq $v.name) { $v.'New Expiry Date' = $WPFDateSelector.Selecteddate $tempname = $v.name $tempdate = $v.'New Expiry Date'.ToString("dd.MM.yyyy") $WPFlistbox.AddChild("$tempname will be extended to $tempdate") } } } $WPFSelectButton.Add_Click($WPFSelectButton_click)
\_(ツ)_/
Thursday, January 11, 2018 1:30 PM -
Hi,
I've tried this and I'm still getting the same thing unfortunately :(
I have noticed that it doesn't look like I do always have to click twice. Once I have selected a date on the date selector, the first click is not recognised and then the second and any subsequent clicks work.
If I then select another date on the date selector, the next click is again not recognised.
Thursday, January 11, 2018 3:55 PM -
There is no way to guess at what your form is doing. Somewhere your design or code are an issue.
Add a trace into the event to show that the code is being called.
Like this:
$WPFSelectButton_click = { Write-Host 'Button clicked' foreach ($v in $NewExpiry) { if ($WPFlistView.SelectedItem.name -eq $v.name) { $v.'New Expiry Date' = $WPFDateSelector.Selecteddate $tempname = $v.name $tempdate = $v.'New Expiry Date'.ToString("dd.MM.yyyy") $WPFlistbox.AddChild("$tempname will be extended to $tempdate") } } } $WPFSelectButton.Add_Click($WPFSelectButton_click)
\_(ツ)_/
Thursday, January 11, 2018 4:05 PM -
Hi Jrv,
I had thought to try that. I get the same behaviour: it writes to host on the second and subsequent clicks.
Thursday, January 11, 2018 4:35 PM -
I have managed to bypass this issue by getting rid of the select button in the end, and just using the date selector:
function get-selecteddate { foreach ($v in $NewExpiry) {
if ($WPFlistView.SelectedItem.name -eq $v.name) {
$v.'New Expiry Date' = $WPFDateSelector.Selecteddate
$tempname = $v.name
$tempdate = $v.'New Expiry Date'.ToString("dd.MM.yyyy")
$dateout = "$tempname will be extended to $tempdate"
write-host $dateout
$WPFlistbox.AddChild($dateout)
$WPFListView.ClearValue()
}
}
}
$WPFDateSelector.Add_SelectedDatesChanged({get-selecteddate})And this works first time when the date is selected.
I would be interested to find out why it was behaving in this way though.
Thursday, January 11, 2018 4:40 PM -
Then something is wrong with your form design or the WPF framework.
\_(ツ)_/
Thursday, January 11, 2018 4:40 PM -
Here is a simple form that tests the WPF and button click:
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="Get All Permissions" Height="200" Width="250" WindowStartupLocation="CenterScreen"> <Grid> <Button x:Name="startBtn" Content="Start" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,20,20,20" Width="204" Height="46" FontWeight="Bold" FontSize="22" Background="#FF17EA17" Focusable="False"/> </Grid> </Window> '@ $rdr = New-Object System.Xml.XmlNodeReader($xaml) $form = [Windows.Markup.XamlReader]::Load($rdr) $startBtn = $form.findname('startBtn') $startBtn.Add_Click({ #$this.Parent.Parent.Close() Write-Host 'Button clicked!' }) $form.showDialog()
If it works then you have junk in your code.
\_(ツ)_/
- Edited by jrv Thursday, January 11, 2018 5:03 PM
- Proposed as answer by Albert LingMicrosoft contingent staff Monday, January 29, 2018 10:14 AM
Thursday, January 11, 2018 4:57 PM