powershell screen with dynamically built buttons call a function based on the button clicked

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”
}

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>################## 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
</code>
<code>################## 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 </code>
################## 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

New contributor

Mark Kuntz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật