首页 > 编程 > C# > 正文

C#递归遍历窗体所有textbox控件并设置textbox事件的方法

2020-01-24 02:14:48
字体:
来源:转载
供稿:网友

本文实例讲述了C#递归遍历窗体所有textbox控件并设置textbox事件的方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public virtual void SetTextBoxOnEnterStyle(object sender, EventArgs e)
{
    if (sender is TextBox)
    {
 TextBox tbox = sender as TextBox;
 if (!tbox.ReadOnly)
 {
     tbox.BackColor = Color.Yellow;
 }
    }
}

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public virtual void SetTextBoxOnLeaveStyle(object sender, EventArgs e)
{
    if (sender is TextBox)
    {
 TextBox tbox = sender as TextBox;
 if (!tbox.ReadOnly)
 {
     tbox.BackColor = Color.White;
 }
    }
}
/// <summary>
///
/// </summary>
/// <param name="frm"></param>
public virtual void SetFormTextBoxControlStyle(Form frm)
{
    IterateControlsSetTextBox(frm.Controls);
}

/// <summary>
///
/// </summary>
/// <param name="ctls"></param>
public virtual void IterateControlsSetTextBox(Control.ControlCollection ctls)
{
    foreach (Control control in ctls)
    {
 if (control is TextBox)
 {
     (control as TextBox).Enter += new EventHandler(SetTextBoxOnEnterStyle);
     (control as TextBox).Leave += new EventHandler(SetTextBoxOnLeaveStyle);
 }

 if (control.Controls.Count > 0)
 {
     IterateControlsSetTextBox(control.Controls);
 }
    }
}

希望本文所述对大家的C#程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表