C#.NET : Accessing a UI component using only it’s name as string

I wanted to create a function that can change the state of any UI component that is inherited from class Control. So,how?

public
void Enable(string ctrlName, bool state)

{

            Control[] ct = this.Controls.Find(ctrlName,true);

            ct[0].Enabled = state;

}


 


 

This function will change the state of a UI component. What this code does is to find the control in the form with the name as value of ctrName. After finding the control save it to Control[] ct . Since only 1 control exist in this array ct[0] points to the control that we want and accessing it's property as inherited from class Control.


 

That's it!

Comments

Popular posts from this blog

Digital Logic

Visual Studio 2008 MFC: How to embed a dialog to a dialog

How to statically link applications to wxWidgets via Visual Studio 2008 and in Linux