开发者

C# dynamic control names in .NET CF

开发者 https://www.devze.com 2022-12-31 02:50 出处:网络
I\'m working with .NET CF framework in c#, and I want to know if I can acc开发者_如何学Goess the controls somehow like this:

I'm working with .NET CF framework in c#, and I want to know if I can acc开发者_如何学Goess the controls somehow like this:

string field="txtName";
this.Controls[field];

or is this impossibile?


I think the method you're after is FindControl - you'll find that method on anything with a Controls collection.


What about using Linq?

var myControl = this.Controls.Cast<Control>().OfType<WhateverControlType>().FirstOrDefault(cont => cont.ID == "myControlId");

Something like that?


I don't see why it would be wrong, the indexer expects a string, and you're passing a string, so for me it's correct.


It is possible to reference a control in the control collection by name (stirng) or index (int). The only thing you will need to do additionally is cast the control into the type of object it is. Something like the following.

MyControl c (MyControl)this.Controls["ControlName"];

Enjoy!

0

精彩评论

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