在Windows应用程序中将文本框BackColor更改为焦点 [英] Change textbox BackColor on focus in a Windows application

查看:43
本文介绍了在Windows应用程序中将文本框BackColor更改为焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In a Windows form I need to change textbox BackColor on focus. I want to do this on every textbox or control focus.

当应更改此文本框对textbox1 BackColor的焦点并且现在我按Tab时,焦点将移至下一个文本框(textbox2),现在应更改textbox2的BackColor并将textbox1 BackColor更改为默认颜色.

When the focus on textbox1 BackColor of this textbox should be changed and now I press tab, focus goes to next textbox (textbox2) now the BackColor of textbox2 should be changes and textbox1 BackColor should be changed back as default color.

推荐答案

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

这篇关于在Windows应用程序中将文本框BackColor更改为焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆