This is a migrated thread and some comments may be shown as answers.

How to find if a Control is RadComboBox

1 Answer 123 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Zinoviy Margovskiy
Top achievements
Rank 1
Zinoviy Margovskiy asked on 01 Apr 2010, 10:24 PM
Hi,

We have a base page where we need to traverse through all controls on the current page instance and set them to ReadOnly mode.
This works fine for most of the controls but does not work for RadComboBox.  Process never gets into if (cntrl is RadComboBox) clause. Here is the function:
       private void SetControlReadOnly(Control cntrl) 
        { 
            if (cntrl.HasControls()) 
            { 
                foreach (Control nestedContrl in cntrl.Controls) 
                { 
                    SetControlReadOnly(nestedContrl); 
                } 
            } 
            else 
            { 
                if (cntrl is RadComboBox) 
                { 
                    RadComboBox ddl = (RadComboBox)cntrl; 
                    ddl.Enabled = false
                } 
                else if (cntrl is RadInputControl) 
                { 
                    SetRadInputControlReadOnly((RadInputControl)cntrl); 
                } 
                else if (cntrl is CheckBox) 
                { 
                    CheckBox chk = ((CheckBox)cntrl); 
                    chk.Enabled = false
                } 
                else if (cntrl is TextBox) 
                { 
                    TextBox txt = ((TextBox)cntrl); 
                    txt.Enabled = true
                    txt.ReadOnly = true
                    txt.ForeColor = System.Drawing.Color.Black; 
                } 
            } 
 
        } 
 
 
        private void SetRadInputControlReadOnly(RadInputControl input) 
        { 
            input.Enabled = true
            input.ReadOnly = true
            //input.BorderStyle = BorderStyle.None; 
            input.DisabledStyle.ForeColor = System.Drawing.Color.Black; 
        } 

1 Answer, 1 is accepted

Sort by
0
Kalina
Telerik team
answered on 02 Apr 2010, 02:33 PM
Hello Zinoviy Margovskiy,

In fact your approach is correct, just note that RadComboBox in fact "has controls" (RadComboBoxHeaderFooterControl, RadComboBoxItem etc.)

That is why the check "if (cntrl.HasControls())" leads to recursive call of SetControlReadOnly method and attempt of disabling the RadComboBox child-controls not the RadComboBox itself.

I made a small change in your code and now it works properly:

protected void Page_Load(object sender, EventArgs e)
    {
        SetControlReadOnly(Page.Form);
    }
    private void SetControlReadOnly(Control cntrl)
    {
         
 
        if (cntrl.HasControls())
        {
            if (cntrl is RadComboBox)
            {
                RadComboBox ddl = (RadComboBox)cntrl;
                ddl.Enabled = false;
            }
 
            foreach (Control nestedContrl in cntrl.Controls)
            {
                SetControlReadOnly(nestedContrl);
            }
        }
        else
        {
            //if (cntrl is RadComboBox)
            //{
            //    RadComboBox ddl = (RadComboBox)cntrl;
            //    ddl.Enabled = false;
            //}
            //else
            if (cntrl is RadInputControl)
            {
                SetRadInputControlReadOnly((RadInputControl)cntrl);
            }
            else if (cntrl is CheckBox)
            {
                CheckBox chk = ((CheckBox)cntrl);
                chk.Enabled = false;
            }
            else if (cntrl is TextBox)
            {
                TextBox txt = ((TextBox)cntrl);
                txt.Enabled = true;
                txt.ReadOnly = true;
                txt.ForeColor = System.Drawing.Color.Black;
            }
        }
 
    }
 
 
    private void SetRadInputControlReadOnly(RadInputControl input)
    {
        input.Enabled = true;
        input.ReadOnly = true;
        //input.BorderStyle = BorderStyle.None;
        input.DisabledStyle.ForeColor = System.Drawing.Color.Black;
    }


All the best,
Kalina
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
ComboBox
Asked by
Zinoviy Margovskiy
Top achievements
Rank 1
Answers by
Kalina
Telerik team
Share this question
or