How Can I Evaluate a String in C# Windows Application because I need to Dynamically select object in a form based on the Combination of 2 String that give me the name of the needed object
-
Actually, I don't think it's an exact duplicate. Well, at least with question pointed as a duplicate. The question here is about winforms. – default locale Oct 07 '10 at 12:05
-
Maybe this question duplicates with http://stackoverflow.com/questions/1178967/how-do-i-refer-to-a-windows-form-control-by-name-c-vb – default locale Oct 07 '10 at 12:06
4 Answers
You can tryControlCollection.Find method to find control by name.
For example:
MyForm.Controls.Find("FooButton", true);
Method returns an array of Control element with the Name property set to "FooButton".
There is no C# eval equivalent. But by the link you can find some useful answers. Ofc, if you want to find or evaluate something than winform controls
UPDATE: I think sometimes it is better get control by key directly. For example:
Control control = this.Controls["FooTxtBox"];
if(control==null)
{
MessageBox.Show("Control not found");
}
control.Text = "something";

- 1
- 1

- 13,035
- 13
- 56
- 62
-
I've tried this line of code but it's not working "MyForm.Controls" require an Object Reference I'm using VS 2008 SP1 and .net Framework 3.5 SP1 – Mario Oct 07 '10 at 08:22
-
It's just a sample code. MyForm - it's a form variable in my application. You should place your form name instead. Or if you're running this code in the Form class you can use this.Controls.Find("FooButton", true); – default locale Oct 07 '10 at 08:37
-
You better provide code snippet and error information (error message, line). Currently I don't understand what's the problem. – default locale Oct 07 '10 at 08:50
-
OK it's done the Problem was that I wasn't Create an Instance of the Form any way MyForm.Controls is Returning an array but the Find is not working – Mario Oct 07 '10 at 08:55
-
OK, what do you mean by "is not working"? Compilation error, runtime error, wrong result? By the way, Form.Controls property returns ControlCollection class instance. And if you have .net framework 2.0 or higher you can use Find method and get the array of Controls. If you have .Net1.1 you have to iterate your ControlCollection to find control. – default locale Oct 07 '10 at 09:18
-
I'm using .net framework 3.5 the problem (wrong result) is that find is returning an empty array where the String is the name of an existent TextBox – Mario Oct 07 '10 at 09:29
-
Again, you better provide code. I mean, ControlCollection.Find call line and Textbox initialization code (from your form designer). This approach works for me and the documentation is pretty clear. And it's hard to me to find the errors without the code. – default locale Oct 07 '10 at 09:41
-
CuttingStyle CS = new CuttingStyle(); MessageBox.Show(CS.Controls.Find("panel9", true) + ""); I use this simple peace of code and panel9 is a hidden panel in the form but the messageBox is returning "system.windows.forms.control[]" instead CS.Controls.count return the true number this peace of code is called on a button click 10x for your corporation – Mario Oct 07 '10 at 12:07
-
Well, Find method returns array. To access it elements you should try:CS.Controls.Find("panel9", true)[0] to get first element of the array. If you'll try to convert it to string it will give you something like: "system.windows.form.panel". So, what do you want to get? Panel itself? Panel.Text? Panel.Name? try it. – default locale Oct 07 '10 at 12:15
-
And again, check the approach in the update section of my answer. I think it'll work for you. – default locale Oct 07 '10 at 12:16
-
the approach in the update is working but about the array I know that but the problem is that the array is empty and that was I said before and what appear in the messagebox result 10x for your corporation – Mario Oct 07 '10 at 12:40
This is a feature (compiler as a service) that should be available in the next version of the .NET Framework, version 5.
Perhaps reflection could be your solution for this?

- 2,320
- 1
- 15
- 24
Have a look at this:
http://www.logiclabz.com/c/evaluate-function-in-c-net-as-eval-function-in-javascript.aspx (Link is dead, please provide an updated source)
Just use the string as the lookup for the Form.Controls
collection. Then when you've got the instance of the control, just call whatever method you need on it to select it.

- 54,199
- 15
- 94
- 116