With what method or variable can I find out the name of the parent object in this example? That is, during the event of hovering the mouse over the first button, get the name $GetParentName = "Button01", second $GetParentName = "Button02", third $GetParentName = "Button03". $GetParentName is [string].
If instead of the variable $GetParentName to apply $This, then the variable $This allows you to get the values of the object, but does not get the name of the object. But how to get the name of the object? Thanks
Edited: without using $This.Name.
$A = @{
Main = [System.Windows.Forms.Form] @{ StartPosition = 'CenterParent' }
Button01 = [System.Windows.Forms.Button] @{ Top = 0 }
Button02 = [System.Windows.Forms.Button] @{ Top = 30 }
Button03 = [System.Windows.Forms.Button] @{ Top = 60 }
}
$Script = { Write-host $GetParentName }
1..3 | % {
$A["Button0$_"].Add_MouseEnter($Script)
$A.Main.Controls.Add($A["Button0$_"])
}
[void]$A.Main.ShowDialog()
You need to set the .Name property for the button controls if you want to get their names inside the MouseEnter script:
Add-Type -AssemblyName System.Windows.Forms
$A = @{
Main = [System.Windows.Forms.Form] @{ StartPosition = 'CenterParent' }
Button01 = [System.Windows.Forms.Button] @{ Top = 0 ; Name = 'Button01'}
Button02 = [System.Windows.Forms.Button] @{ Top = 30 ; Name = 'Button02'}
Button03 = [System.Windows.Forms.Button] @{ Top = 60 ; Name = 'Button03'}
}
$Script = { Write-host $this.Name }
1..3 | ForEach-Object {
$A["Button0$_"].Add_MouseEnter($Script)
$A.Main.Controls.Add($A["Button0$_"])
}
[void]$A.Main.ShowDialog()
$A.Main.Dispose()
Creating and naming the buttons inside the ForEach-Object loop could save you typing the name for each button:
Add-Type -AssemblyName System.Windows.Forms
$A = @{
Main = [System.Windows.Forms.Form] @{ StartPosition = 'CenterParent' }
}
$Script = { Write-host $this.Name }
1..3 | ForEach-Object {
$A["Button0$_"] = [System.Windows.Forms.Button] @{ Top = ($_ -1) * 30 ; Name = "Button0$_"}
$A["Button0$_"].Add_MouseEnter($Script)
$A.Main.Controls.Add($A["Button0$_"])
}
[void]$A.Main.ShowDialog()
$A.Main.Dispose()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With