473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Using TypeOf?

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
Nov 21 '05 #1
10 1508
"Leon" <vn*****@msn.com> schrieb:
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'?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #2
but when I remove the label control the code runs great. What could it be.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Ou**************@TK2MSFTNGP12.phx.gbl...
"Leon" <vn*****@msn.com> schrieb:
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'?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #3
"Leon" <vn*****@msn.com> schrieb:
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
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #4
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!

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eF**************@TK2MSFTNGP12.phx.gbl...
"Leon" <vn*****@msn.com> schrieb:
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
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #5
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" <vn*****@msn.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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

Nov 21 '05 #6
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
Nov 21 '05 #7
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!

"Cor Ligthert" <no************@planet.nl> wrote in message
news:uN****************@TK2MSFTNGP11.phx.gbl...
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

Nov 21 '05 #8
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

Nov 21 '05 #9
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
Nov 21 '05 #10
Thanks Frank and Cor, For I now understand.

"Cor Ligthert" <no************@planet.nl> wrote in message
news:Os****************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
8
by: Sergio Otoya | last post by:
Hi all, I need to add an input hidden field to an existing form (post). I have tried a couple things like adding the '<INPUT type=hidden name=idSelectedURL value=http://server/documents>' to...
7
by: Mark Miller | last post by:
I am using Reflection.Emit to dynamically build a class. A method of the class to be built requires a Parameter of type "Type". But I don't know how to use Emit to pass a call of "typeof()" to the...
7
by: Leon | last post by:
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...
1
by: kingster | last post by:
Hi, I have a regular dataset and all i want to do is make a pivot table display in a browser with the datasource of the pivot table to be this dataset and then the end-user will be able to do...
10
by: pedrito | last post by:
I have a library I'm using that has a lapsed listener issue. I figured I'd just sneak in and do the unsubscription myself since I don't expect it to be fixed any time soon. So, I have the...
5
by: amvoiepd | last post by:
Hi, My question is about how to use const properly. I have two examples describing my problem. First, let's say I have a linked list and from it I want to find some special node. I write the...
2
by: davidson1 | last post by:
Hai friends..for menu to use in my website..i found in one website....pl look below website.... http://www.dynamicdrive.com/dynamicindex1/omnislide/index.htm i downloaded 2 files.... ...
83
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about...
10
by: =?Utf-8?B?QnJpYW4=?= | last post by:
What is the easiest way to convert primitives to a byte array? I tried the BinaryFormatter serialization but it serializes objects so when I serialized an and int it took 54 bytes instead of 4. I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.