我的表單中有很多SimpleButton
(DevExpress控件)。我想通過代碼爲他們設置AllowFocus
至false
。形式的Foreach控制
foreach (Control x in this.Controls)
{
if (x is SimpleButton)
{
((SimpleButton)x).AllowFocus = false;
}
}
當我使用此代碼時沒有什麼真正發生。它仍然允許關注。
我的表單中有很多SimpleButton
(DevExpress控件)。我想通過代碼爲他們設置AllowFocus
至false
。形式的Foreach控制
foreach (Control x in this.Controls)
{
if (x is SimpleButton)
{
((SimpleButton)x).AllowFocus = false;
}
}
當我使用此代碼時沒有什麼真正發生。它仍然允許關注。
從您的評論中可以明顯看出,SImpleButton對象並不直接位於Form上,因此迭代Form的Controls集合不會返回這些對象。
您需要迭代GroupControl的Controls集合。
乾杯
嘗試這種方式:
var buttons = this.Controls.OfType<Control>()
.SelectMany(x => x.Controls.OfType<SimpleButton>());
foreach(var button in buttons)
button.AllowFocus = false;
解決:
foreach (Control x in groupControl1.Controls)
{
if (x is SimpleButton)
{
((SimpleButton)x).AllowFocus = false;
}
}
可能只是檢查的類型的情況下: 如果(typeof運算(X)= = typeof(SimpleButton))
你認爲'if(x是SimpleButton)'在做什麼? –
我建議更好地擁有重劃功能,我通常將所有控件放在主容器面板中,並且您只需將該容器傳遞給Function,剩下的東西函數將爲您執行。
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)? –
請檢查最裏面的語句是否被實際執行。 – Codor
在GroupControl中,GroupControl在XtraTabPage中,是的 – user3091970