开发者

Eval Function in C#

开发者 https://www.devze.com 2023-01-18 21:33 出处:网络
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 2String that give me the name of the needed obj

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


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


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?


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.


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号