无论如何要减少我的文本框常用代码

时间:2012-10-15 11:15:06

标签: c#

我有一个表单,其中有许多文本框(在tabControl布局中)。我正在禁用所有文本框上的右键单击功能。通过提供以下代码:

        txtAmountChildPlans.ContextMenu = new ContextMenu();
        txtCityHome.ContextMenu = new ContextMenu();
        txtCityPersonal.ContextMenu = new ContextMenu();
        txtCityRetirement.ContextMenu = new ContextMenu();
        txtEmailCar.ContextMenu = new ContextMenu();
        txtEmailCarIns.ContextMenu = new ContextMenu();
        txtEmailHome.ContextMenu = new ContextMenu();
        txtEmailOp.ContextMenu = new ContextMenu();
        txtEmailPersonal.ContextMenu = new ContextMenu();
        txtEmailSenior.ContextMenu = new ContextMenu();
        txtEmailTwoIns.ContextMenu = new ContextMenu();
        txtFullNamePersonal.ContextMenu = new ContextMenu();
        txtManufacturerCar.ContextMenu = new ContextMenu();
        txtMobileCar.ContextMenu = new ContextMenu();
        txtMobileCarIns.ContextMenu = new ContextMenu();
        txtMobileHome.ContextMenu = new ContextMenu();
        txtMobileNoRetirement.ContextMenu = new ContextMenu();
        txtMobileOp.ContextMenu = new ContextMenu();
        txtMobilePersonal.ContextMenu = new ContextMenu();
        txtMobileSenior.ContextMenu = new ContextMenu();
        txtMobileTwoIns.ContextMenu = new ContextMenu();
        txtModelCar.ContextMenu = new ContextMenu();
        txtMonthlySalaryCar.ContextMenu = new ContextMenu();
        txtNameHome.ContextMenu = new ContextMenu();
        txtNameRetirement.ContextMenu = new ContextMenu();
        txtPensionRetirement.ContextMenu = new ContextMenu();
        txtRegCarIns.ContextMenu = new ContextMenu();
        txtRegTwoIns.ContextMenu = new ContextMenu();

编辑:

        foreach (var textbox in this.Controls.OfType<TextBox>())
        {
            textbox.ContextMenu = new ContextMenu();
        }
        foreach (var textbox in this.tabCarInsurance.Controls.OfType<TextBox>())
        {
            textbox.ContextMenu = new ContextMenu();
        }
        foreach (var textbox in this.tabHealth.Controls.OfType<TextBox>())
        {
            textbox.ContextMenu = new ContextMenu();
        }
        foreach (var textbox in this.tabHomeLoans.Controls.OfType<TextBox>())
        {
            textbox.ContextMenu = new ContextMenu();
        }
        foreach (var textbox in this.tabRetirement.Controls.OfType<TextBox>())
        {
            textbox.ContextMenu = new ContextMenu();
        }

无论如何都要在sigle函数中写这个。或更少的代码?

这对我来说并不好看。在css中是否有任何方法可以为所有文本框提供相同的属性?

3 个答案:

答案 0 :(得分:6)

您可以枚举所有文本框并在循环中创建新的上下文菜单

foreach (TextBox textbox in AllTextBoxes(this))
{
    textbox.ContextMenu = new ContextMenu();
}

public IEnumerable<TextBox> AllTextBoxes(Control control)
{
    List<TextBox> result = new List<TextBox>();
    result.AddRange(control.Controls.OfType<TextBox>());
    foreach (var childControl in control.Controls.OfType<Control>())
    {
        result.AddRange(AllTextBoxes(childControl));
    }
    return result;
}

答案 1 :(得分:4)

如果没有嵌套控件,您可以按照这些方式执行操作:

foreach(var textbox in Controls.OfType<TextBox>())
{   
    textbox.ContextMenu = new ContextMenu();
}

如果必须处理嵌套控件,可以使用扩展方法

public static class ControlExtensions
{
    public static IEnumerable<T> AllControlsOfType<T>(this Control control) 
        where T : Control
    { 
         T found = control as T;
         if(found != null)
         {
             yield return found;
         } 

         foreach (var child in control.Controls.Cast<Control>())
         {
            foreach (var item in AllControls<T>(child))
            {
               yield return item;
            }
         }
    }
}

可以由您的表单中的代码使用

foreach(var textbox in this.AllControlsOfType<TextBox>())
{   
    textbox.ContextMenu = new ContextMenu();
}

确保引用包含Extension方法的命名空间。

答案 2 :(得分:2)

您可以创建如下方法。

private void AssignDataContext(params TextBox[] textboxes)
{
    foreach (var textbox in textboxes)
    {
        textbox.ContextMenu = new ContextMenu();
    }
}