Accessing directly to a control?

falled

Member
Joined
Sep 16, 2006
Messages
20
Programming Experience
Beginner
Hi every One!

I've created controls on my form dinamycally, so they ara called btn1, btn2,btn3, etc...

If I create them with the wizard I only must put the name and I have the access but how can I do it to access to the dinamically created?

To access them I use a For loop

VB.NET:
For m=0 to Me.controls.count step 1
if (Me.controls.item(m).Name = "btn" + m) then
...
I'd like to find a instruction to access them directly

me.("btn1") for example.

does exists?

Thank you all and sorry for my bad writting
 
Last edited by a moderator:
Direct access when you know the control exist:
VB.NET:
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Controls([/SIZE][SIZE=2][COLOR=#800000]"GChart" [/COLOR][/SIZE][SIZE=2]& i.ToString).BackColor = Color.Black
[/SIZE]
or cast when you need to access control specific properties:
VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]DirectCast[/COLOR][/SIZE][SIZE=2][COLOR=#000000]([/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2][COLOR=#000000].Controls([/COLOR][/SIZE][SIZE=2][COLOR=#800000]"dGrid"[/COLOR][/SIZE][SIZE=2][COLOR=#000000] & i.ToString), DataGridView).ColumnCount = 1[/COLOR]
[/SIZE][/COLOR][/SIZE]
 
VB.NET:
[SIZE=2][COLOR=#008000][COLOR=black]For x = 0 To nButtons - 1 Step 1[/COLOR][/COLOR][/SIZE][COLOR=#008000]
[/COLOR][SIZE=2]For y = 0 To Me.Controls.Count - 1 Step 1[/SIZE]
[SIZE=2]If (Me.Controls.Item(y).Name.Equals("btn" & x)) Then[/SIZE]
[SIZE=2]...[/SIZE]
[SIZE=2]endif[/SIZE]
[SIZE=2]next[/SIZE]
[SIZE=2]next[/SIZE]

changed for:

VB.NET:
For x = 0 To nButtons - 1 Step 1
[SIZE=2][COLOR=#008000][COLOR=#0000ff]Me[/COLOR][SIZE=2][COLOR=#000000].Controls([/COLOR][/SIZE][SIZE=2][COLOR=#800000]"btn" [/COLOR][/SIZE][SIZE=2][COLOR=#000000]& x).BackColor = Color.Black[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#000000]next[/COLOR][/SIZE]
[/COLOR][/SIZE]



i'M not feeling so much clever... xD

Thank you a Lot!!!!! works perfectly is what I need

I'll tell it to santa :)
merry xmas to all!!!!
 
Back
Top