-
Let's say I wanted to display the numbers between 1 and 1000. In qbasic I would do the following:
---------------
For X = 1 to 1000
Print X
Next X
---------------
It continually updates and prints X.
In vb, if I want basically the same thing, I type this:
-------------
For X = 1 to 1000
Label1.Caption = X
Next X
-------------
Problem here is that it doesn't print 1-2-3 etc, it just goes until it's finished and prints 1000.
Solution? Timers. Or are they?
To create the effect I want I have always had to do this:
------------
In the General Declarations:
Dim X as Integer
In a command button:
X = 0
Timer1.Enabled = True
In timer1:
X = X + 1
Label1.Caption = X
If X = 1000 Then Timer1.Enabled = False
-------------
This seems extremely inefficient, and those timer intervals appear to be sort of inconsistent. There's got to be an alternative, what is it?
-
try this:
Code:
for x = 1 to 100
label1.caption = label1.caption & x
next x
-
Uhh...how should I put this? No. That makes the caption = "123456789101112..." and I just want it to go "1", "2", "3", etc.
-
is this what you want:
Code:
for x = 1 to 100
label1.caption = label1.caption & x & ", "
next x
-
Oyi! I must be really bad at explaining this. First of all, I'm not really trying to make this X loop, it was just an example I could use if I had an alternative to the timer junk.
AS for the X loop example- you know how a digital timer that counts up would first display 1, then it would erase that one and display a 2, then it would erase that two and display a 3??? It's that simple. I was just trying to find an alternative way of doing that, because it seems like the timer is not the best way.
-
Do you mean display the number for a second and then erase it and display a new number?
Code:
Private Declare Sub Sleep Lib "kernel32" _
(ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
For x = 0 To 100
Me.Caption = x
Call Sleep(1000)
DoEvents
Next x
End Sub
-
Try:
Code:
For x = 0 To 100
Me.Caption = x
DoEvents 'duration of a millisecond.
Next x
or:
Code:
For x = 0 To 100
Me.Caption = x
Me.Refresh 'repaints the form.
Next x
However, both of these examples will work extremely quickly, so if you're aiming to use a more timed interval, Matthew Gates solution will work well.
-
Well Phobic has done it. I don't understand the DoEvents in the first example, but the second example was what I was looking for. It works much faster than a timer set to intervals of 1 ms. Thanks, Phobic!
-
Use DoEvents()!
DoEvents tell windows to go and do what it needs to do, stuff like update the screen, write stuffs to disk that has been waiting for a while.
Basically it transfers control to windows and waits until it returns control to your program when it's done.
I use doevents in tight loops, to keep my programs 'user friendly' during long calculations, that way a user will always be able to click cancel or close the program AND the calculation will be performed at the fastest possible speed instead of at intervals when using a timer.
Gerco Dries.
-
The simplest solution (as always) would be this:
Code:
for x=1 to 1000
label1.caption=x
label1.refresh
next x
which saves a DoEvents ;)
-
Yes, but IMHO DoEvents is more user/system friendly.
Have you ever tried this:
Code:
do
x=x+1
if x=10000 then x=0
loop
Your system will lock up unless you insert a DoEvents in there and you won't even be able to close your program with the taskmanager (unless ur using Windows NT).
a label1.refresh only works for GUI elements, not for expensive calculations.
Gerco Dries.
[Edited by Gerco on 09-18-2000 at 02:02 AM]
-
P.S:
You can also use a timer, but you said that intervals seems like inconsistent.
all right!
just try to modify timer.interval, that is the number of milliseconds between each activation of the timer.
In fact, tri tu use timer.interval= 200
that wold work with no problem,and it's easy, without writing any loop.
Regards
Gvu
-
You example of using Qbasic will output the results on the screen as follows:
1
2
3
4
5
...
998
999
1000
To do this in VB, you can use the Print method (just like QB)
Code:
For I = 1 To 1000
Print I
Next I
-
You could also specify the object, like a picturebox:
Code:
For x = 1 to 1000
OBJECT.print x
Next