Get and access control by name(string) C#

Create interanl static method its name to getControlByName and write this below code on this.

   internal static Control getControlByName(Control ctrl, string ctlName)
            foreach (Control child in ctrl.Controls)
                if (String.Compare(child.Name, ctlName, false) == 0)
                    return child;
                else if (child.HasChildren)
                    Control retChild = getControlByName(child, ctlName);
                    if (retChild != null)
                        return retChild;
            return null;

For use method :

   private void button1_Click(object sender, EventArgs e)
            Control ctl = getControlByName(this, "ControlName<stringnameofcontrol>");
            if (ctl != null)

Convert Control To sightly control :

//And Other Control

Leave a Reply

Your email address will not be published. Required fields are marked *