我的表单有以下代码:
private void txt1_Enter(object sender, EventArgs e)
{
txt1.SelectAll();
txt1.BackColor = Color.LightBlue;
}
private void txt2_Enter(object sender, EventArgs e)
{
txt2.SelectAll();
txt2.BackColor = Color.LightBlue;
}
private void txt1_Leave(object sender, EventArgs e)
{
txtThermalConductivity.BackColor = Color.White;
}
private void txt2_Leave(object sender, EventArgs e)
{
txtThermalConductivity.BackColor = Color.White;
}
我的表单上还有另外 20 个文本框,我想对其执行相同的操作。是否可以将所有进入事件和所有离开事件合并起来,这样我总共有两个事件而不是 44 个单独的事件?
最佳答案
在设计器 View 中,选择每个文本框并设置 Enter
和 Leave
事件以指向每个文本框的单个实现。
然后你可以这样做:
private void txt_enter(object sender, EventArgs e) {
((TextBox)sender).BackColor = Color.LightBlue;
}
private void txt_leave(object sender, EventArgs e) {
((TextBox)sender).BackColor = Color.White;
}
此外,SelectAll
不是必需的,因为您要设置整个文本框的背景颜色。而不是 RichTextBox
的 SelectionColor
。
关于c# - 输入时更改文本框的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15868881/