Go Back   Forum Care Forums > Development Reference Area > Visual Basic

Reply
 
LinkBack Thread Tools Display Modes
Simple Arrays
Old
  (#1)
Roy Riddex
Guest
 
Posts: n/a
Default Simple Arrays - 06-04-2007, 08:56 AM

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


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: Simple Arrays
Old
  (#2)
Randy Birch
Guest
 
Posts: n/a
Default Re: Simple Arrays - 06-04-2007, 08:56 AM

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" <EMAIL REMOVED> wrote in message
news:f68Cb.29947$EMAIL REMOVED...
: 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
:
:


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Simple Arrays
Old
  (#3)
Mauro
Guest
 
Posts: n/a
Default Re: Simple Arrays - 06-04-2007, 08:56 AM

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" <EMAIL REMOVED> wrote in message
news:qv9Cb.30307$EMAIL REMOVED 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" <EMAIL REMOVED> wrote in message
> news:f68Cb.29947$EMAIL REMOVED...
> : 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
> :
> :
>
>



   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Simple Arrays
Old
  (#4)
Bob Butler
Guest
 
Posts: n/a
Default Re: Simple Arrays - 06-04-2007, 08:56 AM

"Mauro" <EMAIL REMOVED> 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
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Simple Arrays
Old
  (#5)
Randy Birch
Guest
 
Posts: n/a
Default Re: Simple Arrays - 06-04-2007, 08:56 AM

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" <EMAIL REMOVED> wrote in message
news:EMAIL REMOVED m...
: "Mauro" <EMAIL REMOVED> 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


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Simple Arrays
Old
  (#6)
Steve Gerrard
Guest
 
Posts: n/a
Default Re: Simple Arrays - 06-04-2007, 08:56 AM


"Randy Birch" <EMAIL REMOVED> wrote in message
newsmHCb.10656$%EMAIL REMOVED 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?


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Simple Arrays
Old
  (#7)
Rick Rothstein
Guest
 
Posts: n/a
Default Re: Simple Arrays - 06-04-2007, 08:57 AM

> > 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


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Simple Arrays
Old
  (#8)
Roy Riddex
Guest
 
Posts: n/a
Default Re: Simple Arrays - 06-04-2007, 08:57 AM

Thanks guys, I'll give it a try.


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Simple Arrays
Old
  (#9)
Bob Butler
Guest
 
Posts: n/a
Default Re: Simple Arrays - 06-04-2007, 08:57 AM

"Rick Rothstein" <EMAIL REMOVED> wrote in message news:<IsednW_rb4cRdEaiRVn-EMAIL REMOVED>...
> > > 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!
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Simple Arrays
Old
  (#10)
Steve Gerrard
Guest
 
Posts: n/a
Default Re: Simple Arrays - 06-04-2007, 08:57 AM


"Rick Rothstein" <EMAIL REMOVED> wrote in message
news:IsednW_rb4cRdEaiRVn-EMAIL REMOVED...
> > > 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 ***uming
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?


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top