I'm on WINDOWS 10 and POWERSHELL 7.4.3.
I want to change the colors (fore- and backColor) of a button-control when the mouse hovers/leaves the button. Here is an excerpt of the button-control-settings:
$MyButton = New-Object System.Windows.Forms.Button
$MyButton.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
$MyButton.Font = New-Object System.Drawing.Font("Roboto", 12,[System.Drawing.FontStyle]::Bold,[System.Drawing.GraphicsUnit]::Point, 0)
$MyButton.Location = New-Object System.Drawing.Point(5, 112)
$MyButton.Name = "MyButton"
$MyButton.Text = "My button"
$MyButton.Size = New-Object System.Drawing.Size(150, 40)
I tried adding
$MyButton.Add_MouseHover({$MyButton.Color = [System.Drawing.Color]::Orange})
to change the foreColor but the color does not change when the mouse hovers the button.
Any suggestions?
Instead of MouseHover
, use events MouseEnter
and MouseLeave
$DemoForm = [System.Windows.Forms.Form]::new()
$MyButton = [System.Windows.Forms.Button]::new()
$MyButton.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
$MyButton.Font = [System.Drawing.Font]::new('Roboto', 12,[System.Drawing.FontStyle]::Bold,[System.Drawing.GraphicsUnit]::Point, 0)
$MyButton.Location = [System.Drawing.Point]::new(55, 112)
$MyButton.Size = [System.Drawing.Size]::new(150, 40)
$MyButton.Name = "MyButton"
$MyButton.Text = "My button"
$MyButton.Add_MouseEnter({
$this.ForeColor = [System.Drawing.Color]::Orange
$this.BackColor = [System.Drawing.Color]::CadetBlue
})
$MyButton.Add_MouseLeave({
$this.ForeColor = $DemoForm.ForeColor
$this.BackColor = $DemoForm.BackColor
})
$DemoForm.Controls.Add($MyButton)
$DemoForm.ShowDialog()
# don't forget to remove the form from memory when all done
$DemoForm.Dispose()