PrimalForms/PowerShell – Event Behavior: Enter Key and Active Directory Lookup

The next event behavior I wanted to cover was hitting a Enter Key within a textbox control and have it trigger another event.

In this examble I have three basic controls a textbox,a button and a listbox.  I am using the textbox as a Search Textbox, after entering text to search the end user can either press Enter or the ‘find’ button.

First add the Event Handler for the Textbox to response only to the Enterkey.

Next add the Event Handler to search Active Directory using the text from the TextBox($txbFind) after the button($btnFind) is single clicked.

Next the code for handler_button1_Click. First the Event handler clears the listbox ($lstResults) then it searches Active Directory with the text from the Textbox ($txbFind). Then if the results if not null, the handler will list the results in the listbox($lstResults)

$handler_btnFind_Click={
$lstResults.Items.Clear()
$Users = get-qaduser $txbFind.text -service ‘MyDomain.edu’ -sizelimit 0 -includedproperties displayName | Sort displayName | select displayName
 if ($Users -ne $Null) {
  foreach ($User in $Users){
   $lstResults.Items.add($User.displayName)
  }
 }
}

Lastly we add code to the Event handler for the textbox to trigger the button($btnFind) when the Enter Key is pressed and give focus to the listbox($lstResults).

$handler_txbFind_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{
If ($_.KeyChar -eq 13){
 $btnFind.PerformClick()
 $lstResults.Focus()
}
}

The results show the matching users in the listbox

Using this code I created an admin tool that modifys anything from address to exchange administration. Happy Coding!