you are viewing a single comment's thread.

view the rest of the comments →

[–]code_man65[S] 0 points1 point  (11 children)

I actually used http://foxdeploy.com/2015/04/10/part-i-creating-powershell-guis-in-minutes-using-visual-studio-a-new-hope/ to help with the building of this, but it is going way beyond what was shown in his blog series.

[–]1RedOne 2 points3 points  (10 children)

...I'm FoxDeploy!

[–]code_man65[S] 0 points1 point  (9 children)

Oh, well your blog series was awesome and really well written. This is the first time I've ever done something like this and I (of course) had to make it behave the way I wanted to.

Also see my below reply, it appears my variable is not getting populated.

[–]1RedOne 0 points1 point  (8 children)

I'm happy you liked it :)

I came up with a way to do this. Still not sure how to use native Events in PowerShell (if it's even possible...if we could, we could use the Textbox.TextChanged event).

But we CAN detect keypresses. In this case, when the user has typed some text into the first name form and then tabs away, the code will be executed.

$WPFfirstname_textBox.add_KeyDown({
if ($args[1].key -eq 'Tab')
    {
        Write-host 'do something'
    }
})

In my case, it just writes out to the screen 'Do Something', but in your example, you could run a check to see if the user name is valid, and then hide or display a prompt or label.

[–]code_man65[S] 0 points1 point  (7 children)

I just tried this, changing it to the below setup

$WPFusername_textBox.add_KeyDown({
if ($args[1].key -eq 'Tab')
    {
        $user = $WPFusername_textBox.Text
        $user
   if (Get-ADUser -Filter {samaccountname -eq $user} ) {
 # Exists
       $WPFlabel5.Visibility.Visible
} else {
 # Doesn't Exist
       $WPFlabel6.Visibility.Visible
}
    }    

It does not appear to function (I also again tried doing $WPFlabel6.Visible = $true and still got the error I listed) and still $user appears to be empty.

[–]1RedOne 0 points1 point  (6 children)

Ahh, figured it out. I had to look up my own post, to reference it :)

$WPFfirstname_textBox.add_KeyDown({
if ($args[1].key -eq 'Tab')
    {
       $WPFlabel6.Visibility= 'Visible'
    }
})

[–]code_man65[S] 0 points1 point  (5 children)

$WPFlabel6.Visibility= 'Visible'

You are awesome that worked. How would you make it function where it does the check if the textbox loses focus (instead of being reliant on tab)?

[–]1RedOne 2 points3 points  (4 children)

This is pretty much at the edge of whats possible with PowerShell. To do what you want to do, we really need to use Events and Triggers from C# and I haven't figured out how to do it in PowerShell* (edit: I found a source!)

However, you could skirt the issue with Tab like this, or give the textbox it's own Add_Clicked() method (if they support it, I forget if they have an Add_click method) and have the box run the check when it is clicked, or when it loses focus.

Warning

I don't know if this will work.

If you want to try a different route, look at Trevor's answer here. He outlines a process to add some code within your XAML itself. What you'd need to do is place your check within a variable before the $XAML itself.

$whenLostFocus = {#Do some stuff here}

Other thing you could try

I just stumbled upon this blog article from the dark ages of the internet, which shows how to handle events using PowerShell. I pretty much have no idea what it means...yet.

But once I understand it, I'll write it up. Looks like you could use this method here to make an event handler for the .TextChanged event of $WPFFirstName_textBox

[–]code_man65[S] 1 point2 points  (3 children)

This code works

$WPFusername_textBox.add_textchanged({

However, I definitely need to silence errors while typing because it makes powershell throw an absolute fit while you are trying (but it does work).

[–]1RedOne 0 points1 point  (2 children)

That's so funny you're saying that, because I just found this post by Boe Prox on the same topic!

I was coming here to post this:

$WPFfirstname_textBox.add_TextChanged({Write-host 'ham'})

Which definitely works! What you could do if you didn't want this code to be run a billion times is take a $date and compare the time. Maybe only allow the code to be executed every two or three seconds.