VB.net Please help

  • Thread starter Thread starter J. Freeman
  • Start date Start date
J

J. Freeman

On a winform I have 30 text boxes, named txtType0 though txtType29.
An array to hold info, Dim infoType(29) as String,
I would like to assign the text in each text box to its corresponding
variable. I know there has to be a better way to do it than this.
infoType(0) = txtType0.Text

infoType(1) = txtType1.Text

infoType(2) = txtType2.Text

This is what I am trying but cant find a way to get it to work.

Sub looper()

Dim loopnumber As Integer

For loopnumber = 0 To 29

infoType(loopnumber) = txtType& loopnumber & .Text
'also tried = txtType & (loopnumber) & .Text and
other Combinations

Next loopnumber

End Sub

Thank you very much, any help will be greatly appreciated
 
J. Freeman said:
On a winform I have 30 text boxes, named txtType0 though txtType29.
An array to hold info, Dim infoType(29) as String,
I would like to assign the text in each text box to its corresponding
variable. I know there has to be a better way to do it than this.
infoType(0) = txtType0.Text

infoType(1) = txtType1.Text

infoType(2) = txtType2.Text

\\\
Private m_TextBoxes() As TextBox
..
..
..

' In the form's constructor after the call to 'InitializeComponent'.
m_TextBoxes = _
{ _
Me.TextBox1, _
Me.TextBox2, _
... _
}
..
..
..
For i As Integer = 0 To m_TextBoxes.Length - 1
InfoType(i) = m_TextBoxes(i).Text
Next i
///
 
J,

Although I almost forever give the solution as Herfried does now as well, do
I think that in this case there can be an alternative that needs less typing
(not actual speed that is a little bit less however needs probably
milleniums to be able to see that)

When you have only your textboxes direct on one container, by instance the
form, than you can use as well something.

\\\Snippet typed in this message not tested.
for each ctr as control in me.controls
if ctr.name.substring(0,7) = "txtType" then
infoType(Cint(ctr.name.substring(7)) = ctr.text
end if
next
///

I hope this give more idea's

Cor
 
Humm

When I enter your example I get a syntax error on line:
m_TextBoxes = {Me.TextBox1, Me.TextBox2} "expression expected"

the first bracket is underlined
 
mark said:
When I enter your example I get a syntax error on line:
m_TextBoxes = {Me.TextBox1, Me.TextBox2} "expression expected"

Sorry... 'm_TextBoxes = New TextBox() {Me.TextBox1, ...}'.
 
Thanks to all for the help I really appreciate it, got it to work both ways.
You have gotten me farther along with my program, but more importantly You
taught me stuff I could not find, or maybe did not understand from the
books. Again THANK YOU ALL SO MUCH.
 

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

Back
Top