I have 24 text boxes on a form called C1, C2, C3 ........


I want to do the following

For J As Integer = 1 To 24

Me.Controls("C" + J.ToString).Text = J.ToString

Next

This code is wrong of course as Controls is expecting an integer to use as
an index.

How do I make it work?

many thanks

Rod

Re: Referencing a control using code by Maqsood

Maqsood
Mon May 23 06:57:36 CDT 2005

Hello,
It'll look like this in C#.

int j = 1;
foreach(Control ctrl in this.Controls)
{
if(!ctrl.IsDisposed && (ctrl is typeof(TextBox) && ctrl.Name == "C" +
j)
((TextBox)ctrl).Name = j.ToString();
}

HTH. Cheers.
Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***

Re: Referencing a control using code by Herfried

Herfried
Mon May 23 07:08:48 CDT 2005

"Rod" <RodRodRodRod@Hotmail.com> schrieb:
>I have 24 text boxes on a form called C1, C2, C3 ........
>
>
> I want to do the following
>
> For J As Integer = 1 To 24
>
> Me.Controls("C" + J.ToString).Text = J.ToString
>
> Next
>
> This code is wrong of course as Controls is expecting an integer to use as
> an index.

Accessing controls by their names or indices
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


Re: Referencing a control using code by Rod

Rod
Mon May 23 08:42:09 CDT 2005

Thank you both.

Sorted


"Rod" <RodRodRodRod@Hotmail.com> wrote in message
news:e8gzaO4XFHA.3096@TK2MSFTNGP15.phx.gbl...
> I have 24 text boxes on a form called C1, C2, C3 ........
>
>
> I want to do the following
>
> For J As Integer = 1 To 24
>
> Me.Controls("C" + J.ToString).Text = J.ToString
>
> Next
>
> This code is wrong of course as Controls is expecting an integer to use as
> an index.
>
> How do I make it work?
>
> many thanks
>
> Rod
>
>
>
>
>
>