hello
I'm working with .NETCF framwork in c#, and I want to know if I can access the controls somehow like this:
string field="txtName";
this.Controls[field];
or is this impossibile?
hello
I'm working with .NETCF framwork in c#, and I want to know if I can access the controls somehow like this:
string field="txtName";
this.Controls[field];
or is this impossibile?
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.
I think the method you're after is FindControl
- you'll find that method on anything with a Controls
collection.
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!
As I can see, on Compact Framework there is no such method.
Please enlighten me if I'm wrong.
What about using Linq?
var myControl = this.Controls.Cast<Control>().OfType<WhateverControlType>().FirstOrDefault(cont => cont.ID == "myControlId");
Something like that?