0

我有一个表单,其中有很多文本框(在 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();
        }

反正有没有用单一功能写这个。还是更少的代码?

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

4

3 回答 3

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;
}
于 2012-10-15T11:18:43.577 回答
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 方法的命名空间。

于 2012-10-15T11:19:31.350 回答
2

您可以创建如下所示的方法。

private void AssignDataContext(params TextBox[] textboxes)
{
    foreach (var textbox in textboxes)
    {
        textbox.ContextMenu = new ContextMenu();
    }
}
于 2012-10-15T11:18:21.410 回答