在Windows应用程序中更改焦点上的文本框BackColor

时间:2012-05-22 07:39:01

标签: windows winforms forms

在Windows窗体中,我需要在焦点上更改文本框BackColor。我想在每个文本框或控件焦点上执行此操作。

当应该更改对此文本框的textbox1 BackColor的焦点时,现在我按Tab键,焦点转到下一个文本框(textbox2),现在textbox2的BackColor应该更改,textbox1 BackColor应该更改为默认颜色。

2 个答案:

答案 0 :(得分:0)

在文本框中名为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

答案 1 :(得分:0)

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