Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

ON OFF SHAPE

Status
Not open for further replies.

electron

Electrical
Dec 6, 2000
18
0
0
PR
HI THERE:

I JUST WANT YO HAVE THIS SHAPE (CIRCLE) TO BE ON AND OFF 20 TIMES USING THE FOLLOWING CODE:
(WHEN THIS ROUTINE IS RAN THE SHAPE GOES ON (BLACK DEFAULT) AND NEVER GOES OFF. HELP, WHAT I AM DOING WRONG????
Private Sub cmdOnoff_click()
dim b
dim t
For b = 1 to 20
shpOnoff.visible = true
For t = 1 to 3000
picTimer.cls
picTimer.print t
next t
shpOnoff.visible = false
next b
end sub
THANKS IN ADVANCE. RAFAEL Rafael
 
Replies continue below

Recommended for you

You have a delay loop after you turn the shape on, you need another delay loop after the line
"shpOnoff.visible = false"
because the next thing that happens is your loop turns the shape back on. The loop is executing so quickly that the shape never appears to turn off.
 
Slight aside from the actual question, but the FOR loop to create the time delay doesn't seem the best to me. During execution, it will "hog" the processor.

Why don't you use a timer, that is enabled by cmdOnOff_Click
 
HI THERE

TO COWSKI:

FOR B = 1 TO 20
SHPONOFF.VISIBLE = TRUE (IT IS FALSE IN DEFAULT PROP.)
FOR T = 1 TO 3000
NEXT T
SHPONOFF.VISIBLE = FALSE
FOR H = 1 TO 3000
NEXT H
NEXT B

First routine I sent had shponoff.visible = true in default properties and the shape was shown and never went off like you said it needed a delay loop at the end of shponoff.visible = false. So I made the above changes. Now the shape never show up. I am sure I am not considering something else here. Tomatge made the point to use a timer (will discussed later) but I still have the curiosity of why is not working with the For Next loops. Any help from you on this matter will be very appreciated.

To Tomatge:

I have tried this with a timer with this code

Private Sub Command1_Click()
Timer1.Enabled = True
End Sub

Private Sub Command2_Click()
End
End Sub

Private Sub Timer1_Timer()
Shape1.Visible = True
Timer1.Enabled = False
Timer2.Enabled = True

End Sub

Private Sub Timer2_Timer()
Shape1.Visible = False
Timer2.Enabled = False
Timer1.Enabled = True
End Sub

I am trying to enable timer1 with the command button but once the form is loaded the timers start executing. Obviously I have something wrong in the way I am tryin to do this. Your hep again will be very appreciated.

Thanks to both in advance or whoever wants to help me on this.

Rafael Rafael
 
Rafael,

You only need a single timer - turn it on with Command1 and off with Command2. The code in the timer function will be relatively simple - if the shape is visible, make it invisible and vice versa.

To keep track of the number of changes, you will need to define a variable as static within the function. Every time the shape is switched on, increment that variable.

 
electron,

Depending on the speed of your computer
For T=1 to 3000
next T
may not be a very long loop. You may need to increase this number (maybe try 30000 - getting close to the limit of an integer type, or switch to long type and go even higher). Also, you may want to put a DoEvents command in your loop as this will allow your computer to process other windows messages while you wait for your loop to finish.

Using the timer is an excellent idea, it will simplify things and the length of the delay will not be dependent on the speed of the computer that you run your program on.
 
electron,

I finally got a chance to test the loop code. It seems you need to have that DoEvents in the pause loops:
For T = 1 To 3000
DoEvents
Next T
as this will allow the form to update properly.

good luck
 
Would this work?
Copy and Paste into Code window to view comments

Private Sub cmdOnOff_Click()

'this will change the caption on the command button
'and be used as a flag to stop the timer prematurely
If cmdOnOff.Caption = "On" Then
cmdOnOff.Caption = "Off"
Timer1.Enabled = True
Else
cmdOnOff.Caption = "On"
Timer1.Enabled = False
End If
'If the premature stop is not desired then remove
'the timer1.enabled = true and
'timer1.enabled = false and uncomment the line below
'This will cause the timer to oscillate
'each time the command is pressed

'Timer1.Enabled = Not Timer1.Enabled

End Sub

Private Sub Timer1_Timer()
Static icount As Integer

icount = icount + 1
'This will count how many times the timer fires
'and will stop after showing the shape 20 times
'The timer fires both when the shape is visible
'and when it is not.
If icount >= 39 Then
'disable the timer and reset the counter
Timer1.Enabled = False
icount = 0
End If

'Each time the timer fires the shape will change from
'being visible to NOT being visible
shpOnOff.Visible = Not shpOnOff.Visible

End Sub
 
Status
Not open for further replies.
Back
Top