Home All Groups Group Topic Archive Search About

Loop thru WinForm Controls

Author
10 Apr 2010 3:53 PM
johnb
Hi All
What is the code to loop thru all textbox controls on a form?

TIA
johnb

Author
10 Apr 2010 4:18 PM
Armin Zingler
Am 10.04.2010 17:53, schrieb johnb:
> Hi All
> What is the code to loop thru all textbox controls on a form?

Inside the Form itself only or also inside other containers on the Form?
The former (it's shorter ;-) ):

'assuming option infer on
for each c in controls
   if typeof c is textbox then
       with directcast(c, textbox)
          .property = value
       end with
   end if
next


--
Armin
Author
14 Apr 2010 3:29 PM
johnb
Hi Guys

Thank you for the help. I've now cracked it.

johnb

Show quoteHide quote
"Armin Zingler" wrote:

> Am 10.04.2010 17:53, schrieb johnb:
> > Hi All
> > What is the code to loop thru all textbox controls on a form?
>
> Inside the Form itself only or also inside other containers on the Form?
> The former (it's shorter ;-) ):
>
> 'assuming option infer on
> for each c in controls
>    if typeof c is textbox then
>        with directcast(c, textbox)
>           .property = value
>        end with
>    end if
> next
>
>
> --
> Armin
> .
>
Author
10 Apr 2010 4:29 PM
PvdG42
"johnb" <jo***@discussions.microsoft.com> wrote in message
news:A2474CF4-69FB-4320-B080-CD45BDE16757@microsoft.com...
> Hi All
> What is the code to loop thru all textbox controls on a form?
>
> TIA
> johnb

The Form class (which derives from Control) has a Controls property:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls.aspx

which is a collection of type Control.

The Control class has a GetType() method to allow you to determine if the
current control is a TextBox.

http://msdn.microsoft.com/en-us/library/system.object.gettype(v=VS.90).aspx
Author
10 Apr 2010 5:24 PM
Cor Ligthert[MVP]
Peter,

I've seen that the getType is currently more popular, but why not the way we
here in this newsgroup has done it forever.

If Typeof object Is Classname?

http://www.vb-tips.com/ControlBorder.aspx

Is this a kind of verCSharping (I don't know if you knows a tiny little bit
Dutch, then you understand it)

It means something as: using C# ways, where there is a very good VB
alternative.

:-)

Cor

Show quoteHide quote
"PvdG42" <pvd***@toadstool.edu> wrote in message
news:OIbe1rM2KHA.1016@TK2MSFTNGP02.phx.gbl...
>
> "johnb" <jo***@discussions.microsoft.com> wrote in message
> news:A2474CF4-69FB-4320-B080-CD45BDE16757@microsoft.com...
>> Hi All
>> What is the code to loop thru all textbox controls on a form?
>>
>> TIA
>> johnb
>
> The Form class (which derives from Control) has a Controls property:
>
> http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls.aspx
>
> which is a collection of type Control.
>
> The Control class has a GetType() method to allow you to determine if the
> current control is a TextBox.
>
> http://msdn.microsoft.com/en-us/library/system.object.gettype(v=VS.90).aspx
>
>
>
Author
11 Apr 2010 1:39 AM
PvdG42
Show quote Hide quote
"Cor Ligthert[MVP]" <Notmyfirstn***@planet.nl> wrote in message
news:#0gPtKN2KHA.4752@TK2MSFTNGP02.phx.gbl...
> Peter,
>
> I've seen that the getType is currently more popular, but why not the way
> we here in this newsgroup has done it forever.
>
> If Typeof object Is Classname?
>
> http://www.vb-tips.com/ControlBorder.aspx
>
> Is this a kind of verCSharping (I don't know if you knows a tiny little
> bit Dutch, then you understand it)
>
> It means something as: using C# ways, where there is a very good VB
> alternative.
>
> :-)
>
> Cor
>
I'm not trying to start anything, just trying to offer the OP a way the
"learn to fish".
Your way is certainly just as good, and certainly more VB-traditional.
Author
11 Apr 2010 10:09 AM
Armin Zingler
Am 11.04.2010 03:39, schrieb PvdG42:
>>
> I'm not trying to start anything,

But you did. :-)

> just trying to offer the OP a way the
> "learn to fish".
> Your way is certainly just as good,

I'm not a native English speaker, so what does "*just* as good" mean
in _this_ case? :) "ok, use it if you want", or "as good as", or
"better than"?

I say TypeOf is to be preferred because it's "just quicker".
About 3.5x quicker (in my personal, I predict that one will say
disputable, tests). (yes, and I do know that both methods are not the same
because TypeOf includes inherited classes). And, BTW, TypeOf is 1:1
translated to the "isinst" IL keyword.

> and certainly more VB-traditional.

Curly braces in C are also traditional. AND THAT DAMNED
CASE SENSITIVITY TOOOOO! Ummm... sorry, Sunday morning... ;)


--
Armin
Author
11 Apr 2010 5:58 PM
PvdG42
Show quote Hide quote
"Armin Zingler" <az.nospam@freenet.de> wrote in message
news:#Z7Zs9V2KHA.1708@TK2MSFTNGP05.phx.gbl...
> Am 11.04.2010 03:39, schrieb PvdG42:
>>>
>> I'm not trying to start anything,
>
> But you did. :-)
>
>> just trying to offer the OP a way the
>> "learn to fish".
>> Your way is certainly just as good,
>
> I'm not a native English speaker, so what does "*just* as good" mean
> in _this_ case? :) "ok, use it if you want", or "as good as", or
> "better than"?
>
> I say TypeOf is to be preferred because it's "just quicker".
> About 3.5x quicker (in my personal, I predict that one will say
> disputable, tests). (yes, and I do know that both methods are not the same
> because TypeOf includes inherited classes). And, BTW, TypeOf is 1:1
> translated to the "isinst" IL keyword.
>
>> and certainly more VB-traditional.
>
> Curly braces in C are also traditional. AND THAT DAMNED
> CASE SENSITIVITY TOOOOO! Ummm... sorry, Sunday morning... ;)
>
>
> --
> Armin

Then, I should have said "you way is better". I stand corrected.
Thanks, Armin. I was not aware of the significant performance difference.
Author
11 Apr 2010 9:49 AM
johnb
Hi Guys

Thank you for your useful comments. Yeah I'm learning to fish with this one
but good have more than one rod in the bag.

Thanks once more

johnb

Show quoteHide quote
"Cor Ligthert[MVP]" wrote:

> Peter,
>
> I've seen that the getType is currently more popular, but why not the way we
> here in this newsgroup has done it forever.
>
> If Typeof object Is Classname?
>
> http://www.vb-tips.com/ControlBorder.aspx
>
> Is this a kind of verCSharping (I don't know if you knows a tiny little bit
> Dutch, then you understand it)
>
> It means something as: using C# ways, where there is a very good VB
> alternative.
>
> :-)
>
> Cor
>
> "PvdG42" <pvd***@toadstool.edu> wrote in message
> news:OIbe1rM2KHA.1016@TK2MSFTNGP02.phx.gbl...
> >
> > "johnb" <jo***@discussions.microsoft.com> wrote in message
> > news:A2474CF4-69FB-4320-B080-CD45BDE16757@microsoft.com...
> >> Hi All
> >> What is the code to loop thru all textbox controls on a form?
> >>
> >> TIA
> >> johnb
> >
> > The Form class (which derives from Control) has a Controls property:
> >
> > http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls.aspx
> >
> > which is a collection of type Control.
> >
> > The Control class has a GetType() method to allow you to determine if the
> > current control is a TextBox.
> >
> > http://msdn.microsoft.com/en-us/library/system.object.gettype(v=VS.90).aspx
> >
> >
> >
> .
>
Author
11 Apr 2010 6:01 PM
PvdG42
"johnb" <jo***@discussions.microsoft.com> wrote in message
news:B80896B0-AC66-4C80-B685-3D065334E578@microsoft.com...
> Hi Guys
>
> Thank you for your useful comments. Yeah I'm learning to fish with this
> one
> but good have more than one rod in the bag.
>
> Thanks once more
>
> johnb
>
Pay close attention to Armin's latest post. Note the significant improvement
difference using TypeOf.