【问题标题】:Apply event format to all textboxes将事件格式应用于所有文本框
【发布时间】:2022-01-23 12:15:30
【问题描述】:

我已经写了这段代码:

private void maskedNASC_KeyDown(object sender, KeyEventArgs e)
{  
    maskedNASC.BackColor = Color.Aqua;
}

private void maskedNASC_Leave(object sender, EventArgs e)
{  
    maskedNASC.BackColor = Color.White;
}

我想将此属性应用于表单的所有文本框和屏蔽文本。

我见过很多这样的代码:

void SetProperty(Control ctr) // resalta textbox onfocus
{
    foreach (Control control in ctr.Controls)
    {
        if (control is TextBox)
        {
            control.Leave == control.BackColor = Color.Aqua;
            control.KeyDown += BackColor = Color.White ;
        }
    }
}

写这个的正确方法是什么??

谢谢。亚历杭德罗。

我已将此添加到 form1.designer 中:

this.maskedNASC.Leave += TextBoxEvent_Leave;
this.maskedNASC.KeyDown += TextBox_KeyDown;

但是下面的代码有错误

 [![enter image description here][1]][1]

【问题讨论】:

  • ` control.Leave == control.BackColor = Color.Aqua;`

标签: c#


【解决方案1】:

你可以这样写

void SetProperty(Control ctr) // resalta textbox onfocus
{
    foreach (Control control in ctr.Controls)
    {
        if (control is TextBox)
        {
            control.Leave -= control_Leave; //Remove control_Leave if already binded. 
            control.Leave += control_Leave; //Assigne control_Leave 
            control.KeyDown -= control_KeyDown ; //Remove control_KeyDown if already binded. 
            control.KeyDown += control_KeyDown ; //Assigne control_KeyDown 
        }
    }
}



private void control_Leave(object sender, EventArgs e)
{
   ((Control)sender).BackColor = Color.Aqua;
}

private void control_KeyDown(object sender, KeyEventArgs e)
{
   ((Control)sender).BackColor = Color.White;
}

【讨论】:

    【解决方案2】:

    例如,您可以使事件更通用(使用 C# 8.0 中的模式匹配)

        private void TextBoxEvent_Leave(object sender, EventArgs e)
        {
            if (sender is TextBox bx)
            {
                bx.BackColor = Color.White;
            }
        }
    
        private void TextBox_KeyDown(object sender, EventArgs e)
        {
            if (sender is TextBox bx)
            {
                bx.BackColor = Color.Red;
            }
        }
    

    在构造函数中添加

            this.maskedNASC.Leave += TextBoxEvent_Leave;
            this.maskedNASC.KeyDown += TextBox_KeyDown;
    

    对于您拥有的每个文本框

    您也可以根据您使用的 C# 版本尝试以下操作

        private void TextBoxEvent_Leave(object sender, EventArgs e)
        {
            if (sender is TextBox)
            {
                ((TextBox)sender).BackColor = Color.White;
            }
        }
    
        private void TextBox_KeyDown(object sender, EventArgs e)
        {
            if (sender is TextBox)
            {
                ((TextBox)sender).BackColor = Color.Red;
            }
        }
    

    【讨论】:

    • 谢谢,但是这段代码在bx中有错误,你能解释一下吗?谢谢
    • 如果您使用的是早期版本的 C#,我已经添加了事件,因为第一个解决方案使用的是 C#8.0 中的一个特性模式匹配。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多