Dynamic Control names in VB .NET Code

  • Thread starter Thread starter hans.werner
  • Start date Start date
H

hans.werner

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
 
Hello,

hans.werner said:
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/?...ngControlArraysInVisualBasicNETVisualCNET.asp
 
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
 
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
 
Hello,

Cor said:
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.

;-)
 
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
 
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
 
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
..
 
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
 
Back
Top