473,671 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Cou nt - 1

If (TypeOf Controls(I) Is TextBox) Then

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

tbox.Text = arr1(I)

End If

Next
Nov 18 '05 #1
7 6782
It's not a matter of TypeOf, it's the way you're using "i" that is wrong:

Dim i As Integer = 0

For j As Integer = 0 To Controls.Count - 1
Dim ctrl As Control = Controls(j)
If TypeOf ctrl Is TextBox Then

DirectCast(ctrl , TextBox).Text = arr1(i)

i += 1

End If

Next
"Leon" <vn*****@msn.co m> a écrit dans le message de
news:eR******** ******@TK2MSFTN GP10.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.Cou nt - 1

If (TypeOf Controls(I) Is TextBox) Then

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

tbox.Text = arr1(I)

End If

Next


Nov 18 '05 #2
Better:

Dim i As Integer = 0

For Each ctrl As Control In Controls

If TypeOf ctrl Is TextBox Then

DirectCast(ctrl , TextBox).Text = arr1(i)

i += 1

End If

Next

Nov 18 '05 #3
Thanks So Much Henri,
That most definitely solve my problem. Could you please explain to me in
details what I was doing wrong.
Thanks again!

"Henri" <hm********@hot mail.com> wrote in message
news:OC******** ******@TK2MSFTN GP12.phx.gbl...
Better:

Dim i As Integer = 0

For Each ctrl As Control In Controls

If TypeOf ctrl Is TextBox Then

DirectCast(ctrl , TextBox).Text = arr1(i)

i += 1

End If

Next

Nov 18 '05 #4
Ok, imagine that you have to simplify 1 textbox, 1 label, then 1 textbox in
your Controls collection.
arr1's length should be 2 if it's the same as the number of textboxes.

Your code:

Dim I As Integer
For I = 0 To Me.Controls.Cou nt - 1
If (TypeOf Controls(I) Is TextBox) Then
Dim tbox As TextBox = CType(Controls( I), TextBox)
tbox.Text = arr1(I)
End If
Next

Let's trace the for...next loop in your code:

first iteration:
i = 0
typeof controls(0) is TextBox -> tbox.Text = arr1(0)

2nd iteration:
i = 1
typeof controls(1) is Label -> do nothing

3nd iteration:
i = 2
typeof controls(2) is TextBox -> tbox.Text = arr1(2) OUT OF RANGE
EXCEPTION!

Do you understand your mistake? The index you use to loop through the
Controls collection must be separated from the one you use to access arr1.

Henri

Nov 18 '05 #5
Thanks so much Herni,
I got it:) You Have Been A Great Help!
And Thanks again!

"Henri" <hm********@hot mail.com> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
Ok, imagine that you have to simplify 1 textbox, 1 label, then 1 textbox
in
your Controls collection.
arr1's length should be 2 if it's the same as the number of textboxes.

Your code:

Dim I As Integer
For I = 0 To Me.Controls.Cou nt - 1
If (TypeOf Controls(I) Is TextBox) Then
Dim tbox As TextBox = CType(Controls( I), TextBox)
tbox.Text = arr1(I)
End If
Next

Let's trace the for...next loop in your code:

first iteration:
i = 0
typeof controls(0) is TextBox -> tbox.Text = arr1(0)

2nd iteration:
i = 1
typeof controls(1) is Label -> do nothing

3nd iteration:
i = 2
typeof controls(2) is TextBox -> tbox.Text = arr1(2) OUT OF RANGE
EXCEPTION!

Do you understand your mistake? The index you use to loop through the
Controls collection must be separated from the one you use to access arr1.

Henri


Nov 18 '05 #6
Hello Henri,

Better yet (no type checking and casting, since the foreach can do this for
you)...

foreach (TextBox ctrl in Controls)
{
ctrl.Text = arr1[i];
i++;
}

--
Matt Berther
http://www.mattberther.com
Better:

Dim i As Integer = 0

For Each ctrl As Control In Controls

If TypeOf ctrl Is TextBox Then

DirectCast(ctrl , TextBox).Text = arr1(i)

i += 1

End If

Next

Nov 18 '05 #7
Hello Matt,

But what happens if Controls also contains controls different from TextBox?

Henri

"Matt Berther" <mb******@hotma il.com> a écrit dans le message de
news:On******** ******@TK2MSFTN GP09.phx.gbl...
Hello Henri,

Better yet (no type checking and casting, since the foreach can do this for you)...

foreach (TextBox ctrl in Controls)
{
ctrl.Text = arr1[i];
i++;
}

--
Matt Berther
http://www.mattberther.com
Better:

Dim i As Integer = 0

For Each ctrl As Control In Controls

If TypeOf ctrl Is TextBox Then

DirectCast(ctrl , TextBox).Text = arr1(i)

i += 1

End If

Next



Nov 18 '05 #8

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

Similar topics

28
20310
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()', this.pinginterval); - but is there no way to do this without using the literal ObjectName? If I write 'this.methodName()' I get "Line 1 Char 1: Object doesn't support this property or method." in IE, and nothing happens in Firebird.
8
4298
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 the innerHTML of the form but it fails. ie
7
9961
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 method. The method could look like this: public void myDynamicMethod(Type type){ //.....do something with the Type passed in....... }
1
7340
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 whatever they want ... i dont need to do any special formatting just a straigh ot pivot tables usign sql server, asp.net, vb.net, OWC 10
10
4375
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 control whose event its subscribed to and it's subscribed to the VisibleChanged event and has an event handler called ControlVisibleChanged. So basically, what I want to do is:
5
1985
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 function, and then figure that the function will not be modifying the list at all, so a const qualifier seems appropriate in the parameter. So essentially I have:
2
2447
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.... menuitems.js mmenu.js
83
4170
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 it too? Once I saw a website comparing Prototype to Java and jQuery to Ruby... but now that I read more and more about Prototype, it is said that Prototype actually came from Ruby on Rails development and the creator of Prototype created it...
10
3380
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 tried something like the following but it won't compile (I admit I use generics a lot but haven't written many so this may be way off): class X<T> { public byte ToByteArray(T val) {
0
8481
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8400
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8823
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8602
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7441
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4412
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.