使用单个代码将一个效果应用于所有文本框控件 [英] Apply one effect to all textbox controls with single code

查看:54
本文介绍了使用单个代码将一个效果应用于所有文本框控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



这可能是一个愚蠢的问题,但请耐心等待。我正在为我的文本框控件添加悬停效果,以便他们在悬停时更改颜色。但是我这样做如下:



   #region HoverEffects 
private void txtInitials_Enter( object sender,EventArgs e)
{
txtInitials.BackColor = Color.PaleTurquoise;
}

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

private void txtSurname_Enter( object sender,EventArgs e)
{
txtSurname.BackColor = Color.PaleTurquoise;
}

private void txtSurname_Leave( object sender,EventArgs e)
{
txtSurname.BackColor = Color.White;
}
#endregion





有没有办法让我将这个效果添加到我的所有文本框控件中,而不是让我为每个人和每个人都这样做?我有这么多文本框,这需要几个小时。我只想要几行代码,将相同的效果应用到我添加到表单的任何文本框中。



提前感谢!

解决方案

作为 Wes Aday [ ^ ]建议,从这里开始:在Windows窗体中创建事件处理程序 [ ^ ]



要实现这一目标,您必须为每个文本框声明常见的子选项:

< pre lang =cs> private void MyTextBox_Enter( object sende r,System.EventArgs e)
{
TextBox tb =(TextBox)sender;
tb.BackColor = Color.PaleTurquoise;在inititilizeComponents()之后的Windows窗体类中,
}

// 方法
txtInitials.Enter + = new EventHandler(MyTextBox_Enter);
// 为每个TextBox控件重复...


是的,创建自己的自定义TextBox控件,继承自TextBox,并添加适当的代码以执行您想要的操作。然后你就可以放弃对表格的控制来代替标准的TextBox了,你可以随心所欲地找到它,而不必像你写的那样编写任何代码。


Hi All,

This might be a stupid question but please be patient with me. I am adding a hover effect to my textbox controls for them to change color on hover. However I am doing it as follows:

#region HoverEffects
       private void txtInitials_Enter(object sender, EventArgs e)
       {
           txtInitials.BackColor = Color.PaleTurquoise;
       }

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

       private void txtSurname_Enter(object sender, EventArgs e)
       {
           txtSurname.BackColor = Color.PaleTurquoise;
       }

       private void txtSurname_Leave(object sender, EventArgs e)
       {
           txtSurname.BackColor = Color.White;
       }
       #endregion



Is there any way for me to add this effect to all of my textbox controls without having me to do this for each and everyone of them? I have so many textboxes that this is going to take me hours. I just want a few lines of code that will apply the same effect to any textbox that I add to the form.

Thanks in advance!

解决方案

As Wes Aday[^] suggested, start here: Creating Event Handlers in Windows Forms[^]

To achieve that, you have to declare common subrutine for each textbox:

private void MyTextBox_Enter(object sender, System.EventArgs e)
{
    TextBox tb = (TextBox)sender;
    tb.BackColor = Color.PaleTurquoise;
}

//in windows form class, after InititilizeComponents(); method
txtInitials.Enter += new EventHandler(MyTextBox_Enter);
//repeat it for every TextBox control...


Yeah, create your own custom TextBox control, inheriting from TextBox, and add the appropriate code to do what you want. Then you can drop your control on the form in place of the standard TextBox and you've got it everywhere you want it without having to write any code like you've written.


这篇关于使用单个代码将一个效果应用于所有文本框控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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