Hi - kinda new to .NET and Powershell integration with it.
Please have a look at the following code. to my understanding it $bAttachment should contain an AttachmentLabel object with my custom properties. However it seems to return a standart issue [Forms.Button]::new(), without any properties set!
I’ve checked via the VSCode extension that during construction my object actually contains all of the set properties. What am I doing wrong?
using namespace System.Windows.Forms
using namespace System.Windows.DataFormats
using namespace PresentationFramework
[...]
class AttachmentLabel : System.Windows.Forms.Button {
AttachmentLabel() {
$this.Name = 'bAttachment'
$this.TextAlign = [System.Drawing.ContentAlignment]::MiddleLeft
$this.AutoSize = $True
$this.AutoSizeMode = [System.Windows.Forms.AutoSizeMode]::GrowAndShrink
$this.FlatAppearance.BorderColor = [Drawing.Color]::FromName('GrayText')
$this.Add_Click({
$global:mailContent.Attachments -= $(Get-Item $this.Text)
$fLPAttachments.Controls.Remove($this)
$this.Dispose()
})
}
AttachmentLabel([string]$name, [string]$text) { # For some reason constructs AttachmentLabel fine and then returns new System.Windows.Forms.Button
$this = $this::new()
$this.Name = $name
$this.Text = $text
}
}
$fLPAttachments.Add_DragEnter({
if ($_.Data.GetFormats($True) -ccontains [DataFormats]::FileDrop) {
foreach ($a in $_.Data.GetData([DataFormats]::FileDrop)) {
$attachment = Get-Item $a
$bAttachment = [AttachmentLabel]::new("bAttachment-$($attachment.FullName)", "[X] $($attachment.Name)")
$fLPAttachments.Controls.Add($bAttachment)
}
}
})