C# 表单中的Foreach控件
我的表单中有很多C# 表单中的Foreach控件,c#,winforms,devexpress,C#,Winforms,Devexpress,我的表单中有很多SimpleButton(DevExpress控件)。我想通过代码将它们的AllowFocus设置为false foreach (Control x in this.Controls) { if (x is SimpleButton) { ((SimpleButton)x).AllowFocus = false; } } 当我使用此代码时,实际上什么也没有发生。它仍然允许焦点。从您的评论中可以看出,SImpleButton对象并不直接在表
SimpleButton
(DevExpress控件)。我想通过代码将它们的AllowFocus
设置为false
foreach (Control x in this.Controls)
{
if (x is SimpleButton)
{
((SimpleButton)x).AllowFocus = false;
}
}
当我使用此代码时,实际上什么也没有发生。它仍然允许焦点。从您的评论中可以看出,SImpleButton对象并不直接在表单上,因此迭代表单的控件集合不会返回这些对象 您需要迭代GroupControl的控件集合 干杯试着这样做:
var buttons = this.Controls.OfType<Control>()
.SelectMany(x => x.Controls.OfType<SimpleButton>());
foreach(var button in buttons)
button.AllowFocus = false;
var buttons=this.Controls.OfType()
.SelectMany(x=>x.Controls.OfType());
foreach(按钮中的var按钮)
button.AllowFocus=false;
已解决:
foreach (Control x in groupControl1.Controls)
{
if (x is SimpleButton)
{
((SimpleButton)x).AllowFocus = false;
}
}
可能只是检查类型的一种情况:
如果(typeof(x)=typeof(SimpleButton))我建议最好使用重新草书功能,我通常会将所有控件放在主容器面板中,您只需将该容器传递给函数,函数的其余部分就可以了
private void FocusControls(Control ctl)
{
if ((ctl.GetType() == typeof(GroupBox)) ||
(ctl.GetType() == typeof(DevExpress.XtraEditors.GroupControl)) ||
(ctl.GetType() == typeof(DevExpress.XtraEditors.PanelControl)) ||
(ctl.GetType() == typeof(DevExpress.XtraTab.XtraTabControl)) ||
(ctl.GetType() == typeof(DevExpress.XtraTab.XtraTabPage))
)
{
foreach (Control obj in ctl.Controls)
FocusControls(obj);
}
if (ctl.GetType() == typeof(SimpleButton))
{
SimpleButton objTemp = (SimpleButton)ctl;
objTemp.AllowFocus = false;
}
}
SimpleButton是直接添加到表单中,还是可能添加到面板或其他容器控件(例如TabPage)?请检查是否实际执行了最里面的语句。在GroupControl中,GroupControl在XtraTabPage中,是的,如果(x是SimpleButton)正在执行,您认为
如何?