FindControl in .Net Compact Framework

A

Alain Dekker

I'm trying to use this code:

control myButton = Control.FindControl("btn3");
if (myButton != null)
myButton.Focus();

but this doesn't work in the .NET compact framework. Is there another way to
do this if FindControl is not supported?

Thanks!
Alain
 
J

Jeff Johnson

I'm trying to use this code:

control myButton = Control.FindControl("btn3");
if (myButton != null)
myButton.Focus();

but this doesn't work in the .NET compact framework. Is there another way
to do this if FindControl is not supported?

Do containers have a Controls property? If so, it sounds like a recursive
search might be your only hope.
 
A

Alain Dekker

Yes, thats it! By using the "foreach (control c in this.MyPanel.Controls)"
construct I was able to find the control easily enough by comparing the name
of the control required with the name of the control in the loop. I suppose
this wouldn't work so well on a form / panel with a lot of controls but then
you could create an array of the control names or a hashtable or something.
Fortunately I've only got a couple of controls and your suggestion works
great.

Thanks again!
Alain
 
J

Jeff Johnson

Yes, thats it! By using the "foreach (control c in this.MyPanel.Controls)"
construct I was able to find the control easily enough by comparing the
name of the control required with the name of the control in the loop. I
suppose this wouldn't work so well on a form / panel with a lot of
controls but then you could create an array of the control names or a
hashtable or something. Fortunately I've only got a couple of controls and
your suggestion works great.

I think that's what FindControl() does. It's known to not be very fast.
 
M

Marcel Müller

I think that's what FindControl() does. It's known to not be very fast.

Additionally it is overridden inside INamingContainer Controls like
Grids. Otherwise you won't find anything, once the naming container has
done its job and assigned a unique name to each control instance.


Marcel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top