焦点更改文本背景色在Windows应用程序(Change textbox BackColor on

2019-09-17 09:46发布

在Windows窗体我需要改变文本框的焦点背景色。 我想这样做,每个文本框或控制的重点。

当焦点在这个文本框的背景色TextBox1中应该改变,现在我按Tab键,焦点转移到下一个文本框(TextBox2中)现在TextBox2中的背景色应该是变化和TextBox1的背景色应改回为默认颜色。

Answer 1:

在名为文本框的事件的GotFocus

Private Sub TextBox1_GotFocus(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles TextBox1.GotFocus

    textbox1.backcolor = color.red

End Sub

在文本框的事件命名引发LostFocus

Private Sub TextBox1_LostFocus(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles TextBox1.LostFocus

    textbox1.backcolor = color.white

End Sub


Answer 2:

看哪,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;
}


文章来源: Change textbox BackColor on focus in a Windows application