I’m 4 years retired, converting a routine I wrote umpteen years ago in VBScript.
This portion of the script, reads the contents of a folder,
Then builds a screen, with buttons for each TXT file found in that folder.
When a button is clicked, it calls a common routine.
I would like the name of the button (button.text, to be passed to the common routine)
But the common routine is being called only with the name of the last member found, not the member that has been clicked.
Any assistance would be greatly appreciated.
I’ve narrowed the script down to the barest bones.
Any advice about the script, even not related to my issue will be taken under consideration.
I’ve tried creating an array of button objects, $button=@{}…$button[$bn] = New-Object System.Windows.Forms.Button, but that didn’t work.
I’ve tried passing as the parm to the common routine, the variable $Button.TEXT.
That didn’t work either.
It simply is calling the common function with the last contents of the variable, $player.
Here is the script I’m working with:
######################################################
Function ValidateDep($depPlayer)
{
Write-host “validateDep for $depPlayer”
}
################## Main Line Code ####################
Remove-Variable * -ErrorAction SilentlyContinue
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$TXTfolder = "<some folder with .TXT files>"
$fc = GCI $TXTfolder -filter *.txt
$Player_names_txt = $fc.name
#remove trailing .TXT from each name
$player_names = @()
$player_names_txt | foreach {$player_names += $_.trimend(".txt");write-host $_.trimend(".txt")}
#
#
#
$form_dep = New-Object System.Windows.Forms.Form
$form_dep.StartPosition = [Windows.Forms.FormStartPosition]::Manual
$form_dep = New-Object System.Windows.Forms.Form
$form_dep.Text = 'Select player'
$window_height = ($fc.count+1)*50
$form_dep.Size = New-Object System.Drawing.Size(385,$window_height)
$form_dep.StartPosition = 'CenterScreen'
$Ypos = 030
foreach ($player in $player_names)
{
$Buttons = New-Object System.Windows.Forms.Button
$Buttons.Location = New-Object System.Drawing.Size(30,$Ypos)
$Buttons.Size = New-Object System.Drawing.Size(300,35)
$Buttons.Text = $player
$Buttons.Add_Click({ValidateDep "$player"})
$form_dep.Controls.Add($Buttons)
$ypos += 35
}
$ypos += 35 # create some white space
$CanButton = New-Object System.Windows.Forms.Button
$CanButton.Location = New-Object System.Drawing.Point(30, $Ypos)
$CanButton.Size = New-Object System.Drawing.Size(300,35)
$CanButton.Text = 'CANCEL'
$CanButton.DialogResult = [System.Windows.Forms.DialogResult]::CANCEL
$form_dep.add_click
$form_dep.Controls.Add($CanButton)
$result = ""
while ($result -ne "CANCEL")
{
$result = $form_dep.showdialog()
}
exit
Mark Kuntz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.