Using TypeOf?

  • Thread starter Thread starter Leon
  • Start date Start date
L

Leon

I have a program that fill 6 textbox on button click, but when using TypeOf
my program is trying to also fill label controls which throws my array out
of range. how can use TypeOf to only read textbox Control?

Piece of Code:

Dim I As Integer

For I = 0 To Me.Controls.Count - 1

If (TypeOf Controls(I) Is TextBox) Then

Dim tbox As TextBox = CType(Controls(I), TextBox)

tbox.Text = arr1(I)

End If

Next
 
Leon said:
I have a program that fill 6 textbox on button click,
but when using TypeOf my program is trying to also
fill label controls which throws my array out of range.
how can use TypeOf to only read textbox Control?

Piece of Code:

Dim I As Integer

For I = 0 To Me.Controls.Count - 1

If (TypeOf Controls(I) Is TextBox) Then

Dim tbox As TextBox = CType(Controls(I), TextBox)

tbox.Text = arr1(I)

End If

Your code will only touch textboxes. Maybe there are too few elements in
'arr1'?
 
Leon said:
but when I remove the label control the code runs great. What
could it be.

The reason is that the counter 'I' will be incremented even if the control
checked using 'TypeOf' is not a label. Try this code:

\\\
Dim Counter As Integer
For Each ctr As Control In Me.Controls
If TypeOf ctr Is TextBox Then
DirectCast(ctr, TextBox).Text = Arr1(Counter)
Counter = Counter + 1
End If
Next ctr
///
 
Thanks So Much Herfried,
That most definitely solve my problem. Could you please explain to me in
details what I was doing wrong.
Thanks again!
 
Try this:

Dim theControl as Object
For Each theControl In Me.Cotrols
If TypeOf(theControl) Is TextBox Then
Dim tbox As TextBox = CType(theControl, TextBox)
tbox.Text = arr1(I)
End If
Next

No counting involved this way.
 
Leon,

The other solutions you got, seems for me a little bit a Russian roulette.

Using more your code I would go for this.
Typed here watch typos

\\\
Dim txtBoxes As Textbox() = New Textbox() {Textbox1, Textbox2, etc}
'Above is an array wich takes the refrences
'to your textboxes that you have created on your form
Dim I As Integer
For I = 0 To txtBoxes.Length - 1
txtBoxes(I).text = Arr1(I)
Next
///

In this you do not need the typeof, that command would be when you would go
in that "for each" because than you do not know the type of the control that
is in that.

The for each loop goes to the controls in the way they are added to your
form, so when you start changing things, you can get in problem when you use
the designer for the changes as I assume you do.

I hope this helps?

Cor
 
Thanks Cor,
Your code is much better, but just for better understanding could you please
explain the following line of code:
For I = 0 To txtBoxes.Length - 1

Thanks again!
 
Hi Leon
Thanks Cor,
Your code is much better, but just for better understanding could
you please explain the following line of code:
For I = 0 To txtBoxes.Length - 1

txtBoxes is an Array filled with TextBoxes.
Dim txtBoxes As Textbox() = New Textbox() {Textbox1, Textbox2)

txtBoxes.Length give you the count of TextBoxes that are within that
Array.

In this example there are two Boxes, so the count of txtBoxes is 2.
Each Array begins with 0, means the first entry can be called through
txtBoxes(0) : <- Is TextBox1

So now you can use 'For Next' to access all entry in that array.
You must use txtBoxes.Length - 1 cause the Array-Counter is 2 (two
entries).

If you are using:
For I = 0 to txtBoxes.Length
you will get an error : Index out of range cause txtBoxes(2) isn't
defined.

txtBoxes.Length - 1, this line could also written as:

For I = 0 to 1
Next

But this way isnt good code cause if the array count changed you have
to change the 'For Next' loop too.

Try to find some documentation about VB.net using,creating,accessing
Arrays

Hope you undestand that explanation a bit.

Frank
 
Leon,

I have only to add two words to the explanation of Frank, while I think he
means the same, for the rest I have nothing to add.
txtBoxes is an Array filled with TextBoxes.
txtBoxes is an Array filled with references to TextBoxes.

Cor
 
Thanks Frank and Cor, For I now understand.

Cor Ligthert said:
Leon,

I have only to add two words to the explanation of Frank, while I think he
means the same, for the rest I have nothing to add.

txtBoxes is an Array filled with references to TextBoxes.

Cor
 

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