textbox question


G

Guest

Hi all,

I would like to create some textbox by coding...such as ..I would like
to create 10 textbox during form load. Is it use for loop to do ? ...on the
other hand, is vb.net can do as the following..

dim str as string
dim tmp as string
dim x a sinteger
str = "textbox"
for x = 1 to 10
tmp = str + cstr(x)
' and then assign value in tmp '
next x

Thanks
 
Ad

Advertisements

C

Cor Ligthert

Edmond,

This is simple

dim textboxes(9) as textbox
for x as integer = 0 to 9
dim mytexbox as new textbox
mytextbox.top = 'etc
mytextbox.name = mytextboxname, x.tostring
textboxes(x) = mytextbox 'to get the values when typed in back
controls.add(mytextbox)
next

I hope this helps,

Cor
 
M

Mythran

Cor Ligthert said:
Edmond,

This is simple

dim textboxes(9) as textbox
for x as integer = 0 to 9
dim mytexbox as new textbox
mytextbox.top = 'etc
mytextbox.name = mytextboxname, x.tostring
textboxes(x) = mytextbox 'to get the values when typed in back
controls.add(mytextbox)
next

I hope this helps,

Cor

And a shorter way:

Dim textBoxes(9) As TextBox

For x As Integer = 0 To 9
textBoxes(x) = New TextBox()
textBoxes(x).Top = y
textBoxes(x).Name = myTextBoxName, x.ToString() ' comma a tab? Can't
remember.
controls.Add(textBoxes(x))
Next

w00t shorter by 1 whole line!!!! :p

hth ;)

Mythran
 
C

Cor Ligthert

Mythran,

I did want to show it in its shortest way without the array. However when I
did not showed the array than it would (be when there was not databinding in
the sample) imposible to get the or set the values. Therefore I added it at
the last moment and was to lazy to do it real in the best way.

However

Dim textBoxes(9) As TextBox
For x As Integer = 0 To textBoxes.count -1
textBoxes(x) = New TextBox()
textBoxes(x).Top = y
controls.Add(textBoxes(x))
Next

The name is in my opinion not real needed when it is in an array.

:)

Cor
 
M

Mythran

Cor Ligthert said:
Mythran,

I did want to show it in its shortest way without the array. However when
I did not showed the array than it would (be when there was not
databinding in the sample) imposible to get the or set the values.
Therefore I added it at the last moment and was to lazy to do it real in
the best way.

However

Dim textBoxes(9) As TextBox
For x As Integer = 0 To textBoxes.count -1
textBoxes(x) = New TextBox()
textBoxes(x).Top = y
controls.Add(textBoxes(x))
Next

The name is in my opinion not real needed when it is in an array.

:)

Cor

Cor,

I was simply showing a shorter way, using your example as a base, and
throwing in some sarcastism about myself :p Wasn't meant to be taken that
your way was too long, just showing that it "can" be shortened...I didn't
want to be too anal retentive and do it all on one line, but at the same
time, rid it of some unnecessary additional code just for kicks. Either way
will work, and I have written code often enough using a variable that really
wasn't necessary for all practical purposes :)

But in any sense, sorry that I didn't make myself clear enough about why I
posted the shorter version. It wasn't to tickle anybody's fancy (darn),
just to broaden the minds of others....like that ever happens with my
posts...

Mythran
 
Ad

Advertisements

G

Guest

thanks Mythran

Mythran said:
And a shorter way:

Dim textBoxes(9) As TextBox

For x As Integer = 0 To 9
textBoxes(x) = New TextBox()
textBoxes(x).Top = y
textBoxes(x).Name = myTextBoxName, x.ToString() ' comma a tab? Can't
remember.
controls.Add(textBoxes(x))
Next

w00t shorter by 1 whole line!!!! :p

hth ;)

Mythran
 
G

Guest

hi Cor,

when I try to type mytextbox.name = mytextboxname, x.tostring. it
hightlight....look likes is wrong type..
if I try to type these..is it ok ? mytextbox.name = "textbox" +
cstr(x). Later can use textbox1 to do something or not ?

Thanks
Edmond
 
C

Cor Ligthert

Mythran,

You was completely clear, see my :) I was friendly smiling when I saw your
message.

I was giving the same back, maybe should I not have done that.

:)

Cor
 
C

Cor Ligthert

Edmond,

That is probably a typo in Mythrans code which I saw as well, one of the
reasons I deleted that part in my second sample, than I had not to tell
that.

However you don't need it. You can use your textboxes with

textboxes(0).text = "This is my first textbox"
dim secondtexbox = textbox(1).text

Although before I forget it, you have to set for that your "dim
textboxes(9) as textbox" global, what is outside the procedure, therefore
nicer "private textboxes(9) as textbox"

Otherwise it can be something as
mytextbox.name = "MyUsedName" & x.ToString

I hope this helps,

Cor
 
G

Guest

Cor,
thx first, however... If I want to click textbox1 and then show some
message..it is can do that ?

Thanks

Edmond
 
Ad

Advertisements

Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top