469,271 Members | 1,466 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Simple Arrays

2nd post of the day! I'm just learning about Arrays at College and have met
a problem. I have 5 text boxes for number input, a command button to add the
numbers to the array, and a command button which displays the array contents
in 5 labels. My program works fine. The problem is that I use a simple If
statement to check for any empty boxes. I've to cut the IF statement and use
a For Each...Next statement to check for empty text boxes but I'm struggling
to apply this method. The book also tells me to declare a variable 'Dim
MyTextBox As TextBox', I've never came across this variable type before.
I'll post my working version of the program and hopefully someone can help
me out.

Option Explicit

Dim Numbers(1 To 5) As Integer

Private Sub cmdAddToArray_Click()
Dim Index As Integer
If (txtNumbers(1).Text = "") Or (txtNumbers(2).Text = "") Or
(txtNumbers(3).Text = "") Or (txtNumbers(4).Text = "") Or
(txtNumbers(5).Text = "") Then
MsgBox "You have not entered 5 numbers"
Else
For Index = 1 To 5
Numbers(Index) = txtNumbers(Index).Text
Next Index
End If
End Sub

Private Sub cmdDisplayArray_Click()
Dim Index As Integer
For Index = 1 To 5
lblNumbers(Index).Caption = Numbers(Index)
Next Index
End Sub
Jul 17 '05 #1
13 4474
off the top of my head ...

dim ctl as textbox

for each ctl in me

if typeof ctl is textbox then

if len(ctl.text) = 0 then
msgbox "what part of 'enter 5 numbers' don't you understand?"
ctl.setfocus
exit for
end if
end if
next ctl

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"Roy Riddex" <ro**************@blueyonder.co.uk> wrote in message
news:f6*******************@news-binary.blueyonder.co.uk...
: 2nd post of the day! I'm just learning about Arrays at College and have
met
: a problem. I have 5 text boxes for number input, a command button to add
the
: numbers to the array, and a command button which displays the array
contents
: in 5 labels. My program works fine. The problem is that I use a simple If
: statement to check for any empty boxes. I've to cut the IF statement and
use
: a For Each...Next statement to check for empty text boxes but I'm
struggling
: to apply this method. The book also tells me to declare a variable 'Dim
: MyTextBox As TextBox', I've never came across this variable type before.
: I'll post my working version of the program and hopefully someone can help
: me out.
:
: Option Explicit
:
: Dim Numbers(1 To 5) As Integer
:
: Private Sub cmdAddToArray_Click()
: Dim Index As Integer
: If (txtNumbers(1).Text = "") Or (txtNumbers(2).Text = "") Or
: (txtNumbers(3).Text = "") Or (txtNumbers(4).Text = "") Or
: (txtNumbers(5).Text = "") Then
: MsgBox "You have not entered 5 numbers"
: Else
: For Index = 1 To 5
: Numbers(Index) = txtNumbers(Index).Text
: Next Index
: End If
: End Sub
:
: Private Sub cmdDisplayArray_Click()
: Dim Index As Integer
: For Index = 1 To 5
: lblNumbers(Index).Caption = Numbers(Index)
: Next Index
: End Sub
:
:
Jul 17 '05 #2
Of course, since ctl was declared as TextBox, no other object really should
be enumerated in the loop, so the line 'if typeof ctl is textbox then' could
really be dropped. However, if you were enumerating all objects and
declared ctl as object, then it would be necessary.
Mauro
"Randy Birch" <rg************@mvps.org> wrote in message
news:qv*******************@news02.bloor.is.net.cab le.rogers.com...
off the top of my head ...

dim ctl as textbox

for each ctl in me

if typeof ctl is textbox then

if len(ctl.text) = 0 then
msgbox "what part of 'enter 5 numbers' don't you understand?"
ctl.setfocus
exit for
end if
end if
next ctl

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"Roy Riddex" <ro**************@blueyonder.co.uk> wrote in message
news:f6*******************@news-binary.blueyonder.co.uk...
: 2nd post of the day! I'm just learning about Arrays at College and have
met
: a problem. I have 5 text boxes for number input, a command button to add
the
: numbers to the array, and a command button which displays the array
contents
: in 5 labels. My program works fine. The problem is that I use a simple If : statement to check for any empty boxes. I've to cut the IF statement and
use
: a For Each...Next statement to check for empty text boxes but I'm
struggling
: to apply this method. The book also tells me to declare a variable 'Dim
: MyTextBox As TextBox', I've never came across this variable type before.
: I'll post my working version of the program and hopefully someone can help : me out.
:
: Option Explicit
:
: Dim Numbers(1 To 5) As Integer
:
: Private Sub cmdAddToArray_Click()
: Dim Index As Integer
: If (txtNumbers(1).Text = "") Or (txtNumbers(2).Text = "") Or
: (txtNumbers(3).Text = "") Or (txtNumbers(4).Text = "") Or
: (txtNumbers(5).Text = "") Then
: MsgBox "You have not entered 5 numbers"
: Else
: For Index = 1 To 5
: Numbers(Index) = txtNumbers(Index).Text
: Next Index
: End If
: End Sub
:
: Private Sub cmdDisplayArray_Click()
: Dim Index As Integer
: For Index = 1 To 5
: lblNumbers(Index).Caption = Numbers(Index)
: Next Index
: End Sub
:
:

Jul 17 '05 #3
"Mauro" <mb******@hotmail.com> wrote in message news:<_8rCb.681270$pl3.655010@pd7tw3no>...
Of course, since ctl was declared as TextBox, no other object really should
be enumerated in the loop, so the line 'if typeof ctl is textbox then' could
really be dropped. However, if you were enumerating all objects and
declared ctl as object, then it would be necessary.


try it with at least 1 label on the form... the for each construct
enumerates all controls regardless of the declared type of the loop
variable in VB

The only thing I'd change in Randy's code is explicitly stating teh
collection name instead of relying on the default:
For Each ctl In Me.Controls
Jul 17 '05 #4
yep .. that's why it was air code. <g>

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"Bob Butler" <bu*******@earthlink.net> wrote in message
news:fa*************************@posting.google.co m...
: "Mauro" <mb******@hotmail.com> wrote in message
news:<_8rCb.681270$pl3.655010@pd7tw3no>...
: > Of course, since ctl was declared as TextBox, no other object really
should
: > be enumerated in the loop, so the line 'if typeof ctl is textbox then'
could
: > really be dropped. However, if you were enumerating all objects and
: > declared ctl as object, then it would be necessary.
:
: try it with at least 1 label on the form... the for each construct
: enumerates all controls regardless of the declared type of the loop
: variable in VB
:
: The only thing I'd change in Randy's code is explicitly stating teh
: collection name instead of relying on the default:
: For Each ctl In Me.Controls
Jul 17 '05 #5

"Randy Birch" <rg************@mvps.org> wrote in message
news:pm******************@twister01.bloor.is.net.c able.rogers.com...
yep .. that's why it was air code. <g>
off the top of my head ...

dim ctl as textbox

for each ctl in me


If you declare ctl as TextBox, not Control, shouldn't this code break if
encounters a control that is not a TextBox?
Jul 17 '05 #6
> > yep .. that's why it was air code. <g>
off the top of my head ...

dim ctl as textbox

for each ctl in me


If you declare ctl as TextBox, not Control, shouldn't this code break if
encounters a control that is not a TextBox?


Correct... for the code Randy posted, ctl needs to be declared as a Control.
However, the OP showed that he was using a control array of TextBoxes. That
means, the For Each can be made to enumerate only the control array
elements. Here is Randy's code modified to do that (txtNumbers was the name
of the control array the OP posted)...
Rick - MVP

Private Sub Command1_Click()

Dim ctl As TextBox

For Each ctl In txtNumbers

If Len(ctl.Text) = 0 Then
MsgBox "What part of 'Enter 5 numbers' don't you understand?"
ctl.SetFocus
Exit For
End If

Next ctl

End Sub
Jul 17 '05 #7
Thanks guys, I'll give it a try.
Jul 17 '05 #8
"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message news:<Is********************@comcast.com>...
yep .. that's why it was air code. <g>

> off the top of my head ...
>
> dim ctl as textbox
>
> for each ctl in me
>


If you declare ctl as TextBox, not Control, shouldn't this code break if
encounters a control that is not a TextBox?


Correct... for the code Randy posted, ctl needs to be declared as a Control.


dang, can't believe I missed that!
Jul 17 '05 #9

"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message
news:Is********************@comcast.com...
yep .. that's why it was air code. <g>

Correct... for the code Randy posted, ctl needs to be declared as a

Control. However, the OP showed that he was using a control array of TextBoxes. That means, the For Each can be made to enumerate only the control array
elements. Here is Randy's code modified to do that (txtNumbers was the name of the control array the OP posted)...

Private Sub Command1_Click()

Dim ctl As TextBox

For Each ctl In txtNumbers

...


You know, I am pretty sure I would have tried some sort of For n = 0 to
UBound(txtNumbers), but VB won't recognize a control array as an array
in this context. And if I hard code the upper limit, I am still assuming
that the indexes are sequential, which does not have to be the case.
I would not have thought to try a For Each on a control array, which I
like better. Doesn't this all mean that actually what you have is a
collection, not an array?
Jul 17 '05 #10
"Steve Gerrard" <no*************@comcast.net> wrote
Dim ctl As TextBox

For Each ctl In txtNumbers


You know, I am pretty sure I would have tried some sort of For n = 0 to
UBound(txtNumbers), but VB won't recognize a control array as an array
in this context. And if I hard code the upper limit, I am still assuming
that the indexes are sequential, which does not have to be the case.
I would not have thought to try a For Each on a control array, which I
like better. Doesn't this all mean that actually what you have is a
collection, not an array?

No, you have some unknown hybrid type. Intellisense will list LBound
and UBound as well as Count and Item. LBound and UBound are usually
associated with arrays, but Count and Item are associated with collections.

For the example above, type; txtNumbers.
(Where the list will show up in the code window after you type the dot.)
LFS


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 17 '05 #11
sure it will. But since the controls - array or not - are all part of the
Controls collection, For Each can be easier.

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"Steve Gerrard" <no*************@comcast.net> wrote in message
news:EK********************@comcast.com...
:
: "Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message
: news:Is********************@comcast.com...
: > > > yep .. that's why it was air code. <g>
: >
: > Correct... for the code Randy posted, ctl needs to be declared as a
: Control.
: > However, the OP showed that he was using a control array of TextBoxes.
: That
: > means, the For Each can be made to enumerate only the control array
: > elements. Here is Randy's code modified to do that (txtNumbers was the
: name
: > of the control array the OP posted)...
: >
: > Private Sub Command1_Click()
: >
: > Dim ctl As TextBox
: >
: > For Each ctl In txtNumbers
: >
: > ...
:
: You know, I am pretty sure I would have tried some sort of For n = 0 to
: UBound(txtNumbers), but VB won't recognize a control array as an array
: in this context. And if I hard code the upper limit, I am still assuming
: that the indexes are sequential, which does not have to be the case.
: I would not have thought to try a For Each on a control array, which I
: like better. Doesn't this all mean that actually what you have is a
: collection, not an array?
:
:
Jul 17 '05 #12

"Larry Serflaten" <Ab***@SpamBusters.com> wrote in message
news:3f********@corp.newsgroups.com...
"Steve Gerrard" <no*************@comcast.net> wrote
Doesn't this all mean that actually what you have is a collection, not an array?

No, you have some unknown hybrid type. Intellisense will list LBound
and UBound as well as Count and Item. LBound and UBound are usually
associated with arrays, but Count and Item are associated with collections.
For the example above, type; txtNumbers.
(Where the list will show up in the code window after you type the dot.)


I didn't know that. Interesting that for a control array, UBound is a
method - I think this is the only case where that is true. It still
seems more like a collection to me now, with some array features tacked
on.
Jul 17 '05 #13
Thanks for your help. You all started to get me a wee bit confused, lol. Had
a bit of trouble with the Exit For statement at first, it should have been
Exit Sub.
It works fine now. Below is a code listing. Thanks again.

Dim Numbers(1 To 5) As Integer

Private Sub cmdCheckForBlanks_Click()
Dim ctl As TextBox
Dim Index As Integer
For Each ctl In txtNumbers
If Len(ctl.Text) = 0 Then
MsgBox "What part of 'enter 5 numbers' don't you understand?"
ctl.SetFocus
Exit Sub
End If
Next ctl
For Index = 1 To 5
Numbers(Index) = txtNumbers(Index).Text
Next Index
End Sub

Private Sub cmdDisplayArray_Click()
Dim Index As Integer
For Index = 1 To 5
lblNumbers(Index).Caption = Numbers(Index)
Next Index
End Sub
Jul 17 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jedispy | last post: by
2 posts views Thread by Westcoast Sheri | last post: by
31 posts views Thread by da Vinci | last post: by
7 posts views Thread by jmac | last post: by
51 posts views Thread by Alan | last post: by
9 posts views Thread by NewInTheGame | last post: by
4 posts views Thread by learnfpga | last post: by
24 posts views Thread by Michael | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.