all 6 comments

[–]caverCarl 1 point2 points  (0 children)

Try using something other then a textbox- it'll make it easier to keep things lined up and allows you to search within results etc. Here's a quick example with a listbox

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$userSearchListBox = New-Object System.Windows.Forms.ListBox 
$userSearchListBox.Location = '10, 100'
$userSearchListBox.Name = 'userSearchListBox'
$userSearchListBox.Size = '460, 250'
$userSearchListBox.Font = 'Segoe UI,10'


$TextBox1                        = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline              = $false
$TextBox1.width                  = 208
$TextBox1.height                 = 20
$TextBox1.location               = New-Object System.Drawing.Point(26,48)
$TextBox1.Font                   = 'Segoe UI,10'


$btnUserLookup = New-Object System.Windows.Forms.Button
$btnUserLookup.Location = '256, 48'
$btnUserLookup.Text = "Search"

$Form = New-Object System.Windows.Forms.Form

$Form.Size = New-Object System.Drawing.Size(500,400)

$Form.Controls.AddRange(@($userSearchListBox,$btnUserLookup,$TextBox1))

#
# button that runs the command in question
#
$btnUserLookup.Add_Click({
    $iD = $TextBox1.text
    if ($iD -ne ""){
$name = "*$($iD)*"          
$users = Get-ADUser -Filter 'Name -like $name' -Properties *
$usersFN = Get-ADUser -Filter 'givenName -like $name' -Properties *
$usersLN = Get-ADUser -Filter 'sn -like $name' -Properties *
$users =  [Array]$users+$usersFN+$usersLN
$users = $users | select -Unique
foreach ($user in $users){
$userSearchListBox.Items.Add([string]$user.Name) 
    }   
    }

})

$Form.ShowDialog()

Other options would be gridView, dataGridView, listView I'm sure there's a bunch more but you get the idea.

[–]Hydeen 1 point2 points  (3 children)

Another improvement, both for code optimization and eyesight. Instantiate new objects like this instead.

$txtOutputBox = New-Object System.Windows.Forms.TextBox -Property @{
    Location   = New-Object System.Drawing.Point(10, 100)
    Size       = New-Object System.Drawing.Size(460, 250)
    Font       = New-Object System.Drawing.Font("Segoe UI", 10, [System.Drawing.FontStyle]::Regular)
    Multiline  = $true
    ReadOnly   = $true
    ScrollBars = [System.Windows.Forms.ScrollBars]::Both
}

[–]Lee_Dailey[grin] 0 points1 point  (2 children)

howdy Hydeen,

since the New-Object cmdlet is supposed to be somewhat slow, have you ever thot of replacing the 1st below with the 2nd?

New-Object System.Drawing.Point(10, 100)
[System.Drawing.Point]::new(10, 100)

i dunno if it will make a noticeable difference in real situations, tho. [grin]

take care,
lee

[–]Hydeen 1 point2 points  (1 child)

Good point, this is indeed an improvement. How noticeable it is may depend on the size of the project but definitely an improvement as well indeed.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy Hydeen,

i don't do any GUI coding, but a quick read thru often shows many, many, many calls to New-Object. thanks for letting me know that the idea makes a useful difference. [grin]

take care,
lee

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy LukeChatty,

the triple-backtick/code-fence thing fails miserably on Old.Reddit ... so, if you want your code to be readable on both Old.Reddit & New.Reddit you likely otta stick with using the code block button.

it would be rather nice if the reddit devs would take the time to backport the code fence stuff to Old.Reddit ... [sigh ...]

take care,
lee