|
-
Sep 5th, 2001, 06:39 AM
#1
Thread Starter
Lively Member
For Loops
I am writing a program which asks the user to input a no and based on the number displays a msg those many no of times.
The code is
Private Sub txtNumber_Change()
intNumber = CInt(txtNumber.Text)
For i = 1 To intNumber Step 1
lblMessage.Caption = "VB is Fun!!!"
Next
End Sub
Pl.help
-
Sep 5th, 2001, 06:42 AM
#2
Take the code out of the text box Change event, and put it into a command buttons click event.
-
Sep 5th, 2001, 06:44 AM
#3
Code:
Private Sub CommandButton1_Click()
'declare 2 variables
Dim intCounter as Integer, intNumber
'Display an inputbox which'll prompt the user for a number
intNumber = Inputbox("Please enter a number")
'was a number entered ?
If IsNumeric(intNumber) Then
For intCounter = 1 To intNumber
'the vbcrlf bit inserts a new line at te end too
lblMessage.Caption = "VB is Fun!!!" & VBCrlf
Next intCounter
End If
End Sub
-
Sep 5th, 2001, 06:48 AM
#4
Thread Starter
Lively Member
Dim intNumber As Integer
Dim i As Integer
Private Sub cmdClick_Click()
intNumber = CInt(txtNumber.Text)*
For i = 1 To intNumber Step 1
lblMessage.Caption = "VB is Fun!!!"
Next
End Sub
the problem still persists *gives error
-
Sep 5th, 2001, 07:23 AM
#5
What error does it give you? If txtNumber.Text is > 32767 or contains non-numeric characters, CInt will have a problem. The quick & dirty solution would be to use Val instead of CInt; to do it the "right" way, you could be more stringent in the data entry process when the user enters the number. Also, you could put error-handling in the Sub.
"It's cold gin time again ..."
Check out my website here.
-
Sep 5th, 2001, 07:23 AM
#6
Try this in cmdClick_Click:
Dim intNumber As Integer
Dim i As Integer
intNumber = CInt(Text1.Text)
For i = 0 To intNumber
lblMessage.Caption = ""
lblMessage.Caption = "VB is Fun!!! "
Next
F8 through the sub so you can actually see the loop happening. If you just run it, it appears to only go through one time.
-
Sep 5th, 2001, 07:25 AM
#7
PowerPoster
Hi
The Cxxx conversion functions usually fall over if the passed data is not in the expected format. So u should add a little error check at the beginning. This check will by no means cover every eventuality but i will leave to you to add more.
VB Code:
Dim intNumber As Integer
Dim i As Integer
Private Sub Command1_Click()
If IsNumeric(txtnumber.Text) Then 'Added this check
intNumber = CInt(txtnumber.Text)
'removed Step 1 as that is default step
For i = 1 To intNumber
Changed next line for concatenation ( adding strings )
lblMessage.Caption = lblMessage.Caption & "VB is Fun!!!" & vbCrLf
Next
End If
End Sub
Regards
Stuart
-
Sep 5th, 2001, 07:36 AM
#8
I am writing a program which asks the user to input a no and based on the number displays a msg those many no of times.
The Cxxx conversion functions usually fall over if the passed data is not in the expected format
My code not good enough for you lot then ?!?!?!?
-
Sep 5th, 2001, 07:40 AM
#9
PowerPoster
Originally posted by alex_read
My code not good enough for you lot then ?!?!?!?
Welllll actually. Your code wouldnt work cos of this line
VB Code:
lblMessage.Caption = "VB is Fun!!!" & VBCrlf
and also u used the dreaded Input Box !! 
I only responded cos he said was still having errors 
Regards
Stuart
-
Sep 5th, 2001, 07:50 AM
#10
Welllll actually. Your code wouldnt work cos of this line
The VBCrlf works with labels - put this in a new project & run :
Private Sub Form_Load()
Label1.Caption = "1" & vbCrLf & "2"
End Sub
Anyway, I'll let you off then 
Anyone know what you declare an inputbox as ?
I always use a variant (I know I shouldn't) if I need to use it, there must be a proper way though. Cheers !
-
Sep 5th, 2001, 07:53 AM
#11
PowerPoster
Originally posted by alex_read
The VBCrlf works with labels  -
LOL of course it does and u will see that i used it butttttttttt u forgot to add the caption to itself ie
VB Code:
'You had
lblMessage.Caption = "VB is Fun!!!" & VBCrlf
'Should be
lblMessage.Caption = [b]lblMessage.Caption &[/b] "VB is Fun!!!" & VBCrlf
Regards
Stuart
-
Sep 5th, 2001, 08:05 AM
#12
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|