OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

PowerShell - Get Reference to an dynamic created button

  • Thread starter Thread starter TheRob87
  • Start date Start date
T

TheRob87

Guest
I have the Following Script:

Code:
$ButtonList = @{
    'NumPad1' = "Enter"
    'NumPad2'  = "ESC"
}


function WriteButtonName ($sBName){
    Write-Host $sBName
    
}


$x_pos= 35

$v= get-variable -Name "x_pos"
$($v.Name)


$form = New-Object System.Windows.Forms.Form
foreach($b in $ButtonList.GetEnumerator()){
$Name = $($b.Name) 
$Value = $($b.Value)
$Name
$Value
    Remove-Variable -Name $Name
    New-Variable -Name $Name #-Value New-Object System.Windows.Forms.Button
    (get-variable -Name $Name).Value = New-Object System.Windows.Forms.Button
    (get-variable -Name $Name).Value.Location = New-Object System.Drawing.Size($x_pos,35)
    (get-variable -Name $Name).Value.Size = New-Object System.Drawing.Size(120,23)
    (get-variable -Name $Name).Value.Text = $Value
    (get-variable -Name $Name).Value.Add_Click({WriteButtonName -sBName (get-variable -Name $Name).Value.Text })

$x_pos += 130
$form.Controls.Add((get-variable -Name $Name).Value)
}

$form.showdialog()

This Scipt Create 2 Buttons, and by Clicking they return Something. But because in '$Name' is stored just the last Name I didn't get the Name from the Varible where the first Button is stored.

How can I get NUMPAD1 by click the first button.

Thanks.
<p>I have the Following Script:</p>
<pre><code>$ButtonList = @{
'NumPad1' = "Enter"
'NumPad2' = "ESC"
}


function WriteButtonName ($sBName){
Write-Host $sBName

}


$x_pos= 35

$v= get-variable -Name "x_pos"
$($v.Name)


$form = New-Object System.Windows.Forms.Form
foreach($b in $ButtonList.GetEnumerator()){
$Name = $($b.Name)
$Value = $($b.Value)
$Name
$Value
Remove-Variable -Name $Name
New-Variable -Name $Name #-Value New-Object System.Windows.Forms.Button
(get-variable -Name $Name).Value = New-Object System.Windows.Forms.Button
(get-variable -Name $Name).Value.Location = New-Object System.Drawing.Size($x_pos,35)
(get-variable -Name $Name).Value.Size = New-Object System.Drawing.Size(120,23)
(get-variable -Name $Name).Value.Text = $Value
(get-variable -Name $Name).Value.Add_Click({WriteButtonName -sBName (get-variable -Name $Name).Value.Text })

$x_pos += 130
$form.Controls.Add((get-variable -Name $Name).Value)
}

$form.showdialog()
</code></pre>
<p>This Scipt Create 2 Buttons, and by Clicking they return Something. But because in '$Name' is stored just the last Name I didn't get the Name from the Varible where the first Button is stored.</p>
<p>How can I get <strong>NUMPAD1</strong> by click the first button.</p>
<p>Thanks.</p>
Continue reading...
 

Latest posts

Top