在 Windows 窗体中,我需要将文本框 BackColor 更改为焦点。我想在每个文本框或控件焦点上执行此操作。
当此文本框的 textbox1 BackColor 的焦点应该更改并且现在我按 Tab 键时,焦点转到下一个文本框 (textbox2) 现在 textbox2 的 BackColor 应该更改并且 textbox1 BackColor 应该更改回默认颜色。
看看 C# 解决方案:
//Properties declaration
private System.Drawing.Color NormalColor = System.Drawing.Color.FromArgb(50, 82, 110);
private System.Drawing.Color FocusColor = System.Drawing.Color.FromArgb(42, 65, 84);
// Else where in the Constructor
textBox_Username.Enter += EnterEvent;
textBox_Password.Enter += EnterEvent;
textBox_Username.Leave += LeaveEvent;
textBox_Password.Leave += LeaveEvent;
// Outside the Constructor
private void EnterEvent(object sender, EventArgs e)
{
if (sender is TextBox)
((TextBox)sender).BackColor = FocusColor;
}
private void LeaveEvent(object sender, EventArgs e)
{
if (sender is TextBox)
((TextBox)sender).BackColor = NormalColor;
}
Private Sub TextBox1_GotFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles TextBox1.GotFocus
textbox1.backcolor = color.red
End Sub
Private Sub TextBox1_LostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles TextBox1.LostFocus
textbox1.backcolor = color.white
End Sub