Это может быть глупо, но вы можете попробовать добавить эту вспомогательную функцию поверх кода:
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)
}
и настроить обработчики событий GotFocus
и LostFocus
следующим образом:
$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 })
и в самом конце кода:
# 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()
PS Внутри блока сценария обработчика событий вы можете ссылаться на сам элемент управления, используя $this
Automati c переменная .
Кроме того, сделайте $form.Dispose()
, когда закончите с ней.