我有一个表单,其中有很多文本框(在 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 中一样为所有文本框提供相同的属性?