By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,032 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,032 IT Pros & Developers. It's quick & easy.

Dynamic Control names in VB .NET Code

P: n/a
Hi!
Since there are no control index anymore I am using now just different
names of the Controls. e.g. Textbox1 to Textbox 100
is there a way that i can use a variable in the vb.net code that I
dynamicly can work with different Textboxes.

e.g.
for a = 0 to 100
TextBox &a &.Text = "test"
next a

so it would change the Text of Textbox0 to Textbox100. VB.net doesnt
accept this

someone got an idea? or a workaround maybe?

thanks a lot
Nov 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Hello,

"hans.werner" <ha*********@meusburger.cc> schrieb:
Since there are no control index anymore I am using now
just different names of the Controls. e.g. Textbox1 to
Textbox 100 is there a way that i can use a variable in
the vb.net code that I dynamicly can work with different
Textboxes.

e.g.
for a = 0 to 100
TextBox &a &.Text = "test"
next a

so it would change the Text of Textbox0 to Textbox100.
VB.net doesnt accept this

someone got an idea? or a workaround maybe?


Creating Control Arrays in Visual Basic .NET and Visual C# .NET
http://msdn.microsoft.com/library/?u...VisualCNET.asp

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #2

P: n/a
Cor
Hans,
so it would change the Text of Textbox0 to Textbox100. VB.net doesnt
accept this


Big trouble and you use the same approach I started with but it is very easy
to handle without that
First you have to remember that this is always on the parents of the
control.
\\\\
Dim ctr As Control
For Each ctr In Me.Controls
If TypeOf ctr Is TextBox Then
ctr.text = "test"
End If
Next
/////
This for all controls direct on a form.

But when a textbox is by instance on a panel.
\\\\
For Each ctr In Me.Panel1.Controls

Next
/////
So when you put your 100 textboxes on one panel you can select them in that
way

For an approach where I use text from an xml file I have a same approach as
you, but there are the tags from the xml file the same as the textbox names.
(every textbox has another text).

I hope this helps you on the route.
Cor
Nov 20 '05 #3

P: n/a
Cor
Hi Herfried,
I did answered this because I had seen you did your round in all newsgroups
and was passed by.
Has your automatic newsreader answering system 2.0 a problem.
This was always in your standard answering system.
:-))))
Cor
Nov 20 '05 #4

P: n/a
Hello,

"Cor" <no*@non.com> schrieb:
I did answered this because I had seen you did your
round in all newsgroups and was passed by.
;-)
Has your automatic newsreader answering system
2.0 a problem. This was always in your standard
answering system.


_I_ am by automatic answering system.

;-)

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #5

P: n/a
First thanks for both answers
Hello Cor,

This works pretty good, but in this case all controls have the same
value. I need it like this: Textbox1 has value 1
textbox 100 has value 100..

the thing is, I read a database and when the recordset exists e.g. value
90 it sets the textbox90 to value 90

...

any ideas? I hope you know what i mean
thanks
Hans

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #6

P: n/a
thanks for both answers!

Cor,

The thing is, with your solution every Textbox has the same value.

what i exactly do is: I select some content off a database and those
values (integer) which exist get added to the equivalent textbox. e.g in
the db there is value 30, 35, 50, 99, 100

this means: TextBox30 has value 30, Textbox 35 has value 35 etc. and the
rest dont get changed.

in vb 6 is did this with the index textbox(index as integer)
textbox(a).value = myinteger

i hope you know what i mean.

thanks hans
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #7

P: n/a
Cor
Hans,
That I did wrote on the bottom of my last message.
Here is a piece of code I use for this problem (it is now pseudo code).
You can use of course the array of controls methode Herfried did provide
you (it looks more profesional).
I do not see so much advantage of that from what I am doing now, so I keep
it to what I do, feel free to take the other method.
This code is a mix from what you was doing and what I did suply you.
\\\\\
For Each ctr In Me.Controls
Dim indx As String = ctr.Name
If TypeOf (ctr) Is Textbox Then
ctr.Text = search in XML with indx as search item I
think your datarow index.
End If
Next
/////
I hope this helps
Cor
..
Nov 20 '05 #8

P: n/a
Hello,

"Cor" <no*@non.com> schrieb:
If TypeOf (ctr) Is Textbox Then


Better: 'TypeOf ctr Is Textbox'

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #9

P: n/a
Cor
Herfried,

Better: 'TypeOf ctr Is Textbox'

I know it, I copied from a program so I think it wil work anyway.
In my first example I did give to Hans in the good way, that was typed by
hand,
I told Hans that it was pseudo code and I did not check it.
But thanks for you attention.
Cor
Nov 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.