سؤال

I have a lot of SimpleButton (DevExpress Controls) in my form. I want to set AllowFocus to false for them through code.

foreach (Control x in this.Controls)
{
    if (x is SimpleButton)
    {
        ((SimpleButton)x).AllowFocus = false;
    }
}

Nothing really happen when I use this code. It still allow focus.

هل كانت مفيدة؟

المحلول

From your comment, it is clear that SImpleButton objects are not directly on the Form, so iterating the Form's Controls collection is not going to return those.

You need to iterate the GroupControl's Controls collection.

Cheers

نصائح أخرى

Solved :

 foreach (Control x in groupControl1.Controls)
        {
            if (x is SimpleButton)
            {
                ((SimpleButton)x).AllowFocus = false;
            }
        }

Try it this way:

var buttons = this.Controls.OfType<Control>()
    .SelectMany(x => x.Controls.OfType<SimpleButton>());

foreach(var button in buttons)
      button.AllowFocus = false;

I suggest better to have Re-cursive function, I generally place all controls in main container panel, and you just need pass that container to Function, rest of things function will do for you.

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;
                    }
        }

Might just be a case of checking the types: if (typeof(x) == typeof(SimpleButton))

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top