Reputation: 167
Is there a way to set the border color Red on focus and then back to none when focus is lost? I'd also like to set the border style thicker at the same time if possible.
$form= New-Object system.Windows.Forms.Form
$form.Text= "Testing"
$form.StartPosition= 'CenterScreen'
$form.ClientSize= '400,200'
$form.text= "Form"
$form.BackColor= "#0077f7"
$form.TopMost= $true
$form.AutoScroll= $true
$form.Add_KeyDown({if ($_.KeyCode -eq "Escape") { $form.Close() } }) # if escape, exit
$form.KeyPreview= $True
$lbl_one= New-Object system.Windows.Forms.Label
$lbl_one.text= "Input"
$lbl_one.AutoSize= $true
$lbl_one.width= 25
$lbl_one.height= 10
$lbl_one.location= New-Object System.Drawing.Point(10,10)
$lbl_one.Font= 'Verdana,12'
$lbl_one.ForeColor= "#fafa00"
$txt_one= New-Object system.Windows.Forms.TextBox
$txt_one.TabIndex= 1
$txt_one.multiline= $false
$txt_one.text= "test1"
$txt_one.width= 100
$txt_one.height= 20
$txt_one.location= New-Object System.Drawing.Point(100,10)
$txt_one.Font= 'Verdana ,12'
$txt_one.ForeColor= "#0077f7"
$txt_one.Add_GotFocus({ $txt_one.BackColor="LightBlue" } )
$txt_one.Add_LostFocus( { $txt_one.BackColor="White" } )
$lbl_two= New-Object system.Windows.Forms.Label
$lbl_two.text= "Input"
$lbl_two.AutoSize= $true
$lbl_two.width= 25
$lbl_two.height= 10
$lbl_two.location= New-Object System.Drawing.Point(10,40)
$lbl_two.Font= 'Verdana,12'
$lbl_two.ForeColor= "#fafa00"
$txt_two= New-Object system.Windows.Forms.TextBox
$txt_two.TabIndex= 1
$txt_two.multiline= $false
$txt_two.text= "test2"
$txt_two.width= 100
$txt_two.height= 20
$txt_two.location= New-Object System.Drawing.Point(100,40)
$txt_two.Font= 'Verdana ,12'
$txt_two.ForeColor= "#0077f7"
$txt_two.Add_GotFocus({ $txt_two.BackColor="LightBlue" } )
$txt_two.Add_LostFocus( { $txt_two.BackColor="White" } )
$form.controls.AddRange(@($lbl_one,$txt_one,$lbl_two,$txt_two))
[void]$form.ShowDialog()
Upvotes: 1
Views: 6724
Reputation: 61068
This may be cludgy, but you can try adding this helper function on top of the code:
function Paint-FocusBorder([System.Windows.Forms.Control]$control) {
# get the parent control (usually the form itself)
$parent = $control.Parent
$parent.Refresh()
if ($control.Focused) {
$control.BackColor = "LightBlue"
$pen = [System.Drawing.Pen]::new('Red', 2)
}
else {
$control.BackColor = "White"
$pen = [System.Drawing.Pen]::new($parent.BackColor, 2)
}
$rect = [System.Drawing.Rectangle]::new($control.Location, $control.Size)
$rect.Inflate(1,1)
$parent.CreateGraphics().DrawRectangle($pen, $rect)
}
and set up the GotFocus
and LostFocus
event handlers like this:
$txt_one.Add_GotFocus({ Paint-FocusBorder $this })
$txt_one.Add_LostFocus({ Paint-FocusBorder $this })
...
$txt_two.Add_GotFocus({ Paint-FocusBorder $this })
$txt_two.Add_LostFocus({ Paint-FocusBorder $this })
and at the very end of the code:
# call the Paint-FocusBorder when the form is first drawn
$form.Add_Shown({Paint-FocusBorder $txt_one})
# show the form
[void]$form.ShowDialog()
# clean-up
$form.Dispose()
P.S. Inside an event handler scriptblock, you can refer to the control itself using the $this
Automatic variable.
Also, do a $form.Dispose()
when done with it.
Upvotes: 2