Hi David,

In that case you're back to creating a data structure to perform a lookup.

But this is easy. Just add this field:

Private _buttonLookup As Dictionary(Of String, Button)

And now this code in the form load handler:

_buttonLookup = Me.GetAllControls(Me).OfType(Of 
Button)().ToDictionary(Function(b) b.Name)

Now you have a very very fast dictionary look up for buttons by name.

Cheers.

James.


-----Original Message-----
From: ozdotnet-boun...@ozdotnet.com [mailto:ozdotnet-boun...@ozdotnet.com] On 
Behalf Of David Richards
Sent: Wednesday, 27 July 2011 14:55
To: ozDotNet
Subject: Re: Set property of texbox by name

James,

That may not sound like much but what are you running your app on?  I
work mainly in mobile devices and for the sake of a few hundred bytes
of RAM in a collection or dictionary, you can have code that runs
efficiently and uses far less battery power.  Searching through every
control on a form every time I want to change the text property on one
of those controls is not something I would ever do on principle.  I
definitely wouldn't do it on a mobile device; CPU cycles cost power.
This is also true on a desktop or server.

David

"If we can hit that bullseye, the rest of the dominoes
 will fall like a house of cards... checkmate!"
 -Zapp Brannigan, Futurama




On Tue, Jul 26, 2011 at 23:32, James Chapman-Smith
<ja...@chapman-smith.com> wrote:
> Hi David,
>
> What do you mean by incredibly slow? How many buttons are we talking about?
>
> I just did a test with 1000 buttons and it took 3.47 milliseconds. With 5000 
> buttons it was 16.78 milliseconds.
>
> Did I miss something?
>
> Cheers.
>
> James.
>
> -----Original Message-----
> From: ozdotnet-boun...@ozdotnet.com [mailto:ozdotnet-boun...@ozdotnet.com] On 
> Behalf Of David Richards
> Sent: Tuesday, 26 July 2011 15:46
> To: ozDotNet
> Subject: Re: Set property of texbox by name
>
> James,
>
> This may work but it would be incredibly slow.  Better to use a
> reference to access the control you want directly. eg something like
> either Ben's or my previous post.
>
> David
>
> "If we can hit that bullseye, the rest of the dominoes
>  will fall like a house of cards... checkmate!"
>  -Zapp Brannigan, Futurama
>
>
> On Tue, Jul 26, 2011 at 16:07, James Chapman-Smith
> <ja...@chapman-smith.com> wrote:
>> I assume you wanted VB.NET. If not, I can provide a better answer in C# if
>> you need it.
>>
>>
>>
>> Try this:
>>
>>
>>
>>         For Each button In (From x In Me.GetAllControls(Me).OfType(Of
>> Button)() _
>>
>>             Where x.Name = "Button" & n _
>>
>>             Select x)
>>
>>             button.Text = t
>>
>>         Next
>>
>>
>>
>> You need to define this function:
>>
>>
>>
>>     Private Function GetAllControls(ByVal control As Control) As
>> IEnumerable(Of Control)
>>
>>         Dim r = New List(Of Control)
>>
>>         r.Add(control)
>>
>>         For Each c In control.Controls
>>
>>             r.AddRange(GetAllControls(c))
>>
>>         Next
>>
>>         Return r
>>
>>     End Function
>>
>>
>>
>

Reply via email to