简体   繁体   English

单击C#中的文本框时更改背景颜色

[英]Change background color when clicked textbox in C#

I like for the textbox to change the background when the textbox has focus (if clicked with a mouse, tabbed into, etc). 我希望文本框具有焦点时可以更改背景(如果用鼠标单击,将其切换到其他位置等)。

I don't like the textchange method since it won't change until one character has been added. 我不喜欢textchange方法,因为在添加一个字符之前它不会更改。

private void txtFoo_OnGotFocus(object sender, EventArgs e)
    {
        txtFoo.BackColor = Color.LightYellow;
        txtBar.BackColor = Color.White;
    }

This doesn't seem to work for me. 这似乎对我不起作用。 What am I doing wrong? 我究竟做错了什么?

您是否还订阅了事件处理程序?

Like Ben said... did you subscribe to the event handler? 就像Ben所说的那样...您是否订阅了事件处理程序?

Is your code even executing? 您的代码是否还在执行? You can put a break point in there and check pretty easily. 您可以在此处放置一个断点,并轻松检查。

txtWFileNo.Style.Add(HtmlTextWriterStyle.BackgroundColor, "silver");

You could try using the Enter event instead 您可以尝试使用Enter事件代替

private void txtFoo_Enter(object sender, EventArgs e)
{
    txtFoo.BackColor = Color.LightYellow;
    txtBar.BackColor = Color.White;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM