Something tells me that the database query, or web service call before this is going to trump walking the controls on the form...
Focus on the slowest part of your program and optimize that. -----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of David Richards Sent: Tuesday, July 26, 2011 10:25 PM 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 <[email protected]> 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: [email protected] > [mailto:[email protected]] 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 > <[email protected]> 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 >> >> >> >
