VB6 programming

M

Thread Starter

mahmoud fekry

Dears, I design an electronic circuit to control a motor through PC parallel port and using vb-6. The project composed of one form and two command buttons called (start) and (stop). When I click the start button the program enter a DO-Loop. My question is if there is a method to exit from the DO-Loop when I click the stop button at any moment.
 
In general, do loops are not recommended in a control system.

In General.

That being said, I'm pretty sure all you have to do is create a flag variable, say StopButton. Set to 0 before starting the loop, and check for change at end of loop. Make sure you have a DoEvents within your loop to process the stop button.

In your start button routine, change your StopButton flag to 1.

(Warning: My VB is a little rusty)

Rufus
 
R

Ron Calligan

Mahmoud Fekry,
Create a boolean variable in the form. When start is clicked set the variable equal to true and use the variable in the do -loop as this
While variable equals true do when stop is clicked set the variable equal to false.
 
C

Curt Wuollet

Perhaps something like : Do while stop button is not pressed?
Or: Do until stop button is pressed. You need a conditional,
a test, in your loop.
Regards

cww
 
You will need a DoEvents in your loop

Dim bolStop as Boolean

Private Sub CommandStop_Click()
bolStop = True
End Sub

Private Sub CommandStart_Click()

bolStop = False
Do While bolStop <> 1

'Your code
DoEvents

Loop

End Sub
 
bolStop = False
Do While bolStop <> 1

Let's not mix variable types when we can help it. Better use:

bolStop = False
Do While bolStop <> True

Although I usually prefer:

Do While bolStop = False

In case bolStop assumes an illegal value for some reason, you'll still exit.

(For the same reason it's usually better, caution-wise, to end counter loops
with > or < rather than <> or =)
 
Not sure if this code works. While the program is constantly running inside the loop, will it scan the stop button being pressed?
 
It will work if there is a DoEvents in the loop (as was originally shown).

Another note: in VB6, TRUE = -1 (not 1).
 
<p>To make your form responsive you have to call DoEvents inside the loop
<pre>
Private Stop as boolean

private sub startmotor
while stop=false
'whatever stuff
DoEvents
wend
end sub

private command1_click()
stop=true
end sub
</pre>
<p>that's all
 
N
Hmm, in my opinion, though I haven't been programing with VB6 much. Is that you could simply make a variable to stop the button. Use some of the above codes but make it a function in a Module so it doesn't look slopy. Then the just be like:

End Exitloop 'or what ever you name the function.

::::::::::::
--Nameless--
::::::::::::
 
put a label onto your form
change the text inside the label to "1"
then when u define your loop put do until label1.text = "2"
and get your stop button to change the label text to 2

...sorry if this doesn't work, i don't use vb6 very much and don't have it here so i can't check for myself.
 
Put in the command button

if command1=true then
command2=false
end if

change command1 and 2
for the name of your command buttons.
 
<pre>
Dim xc As Integer
xc = 0
While adf.Recordset.EOF
xx = adf.Recordset.Fields(1).Value
yy = adf.Recordset.Fields(2).Value
cod2 = adf.Recordset.Fields(4).Value
LABEL(xc).Left = xx / 38.25
LABEL(xc).Top = yy / 39.354
LABEL(xc).Caption = cod2
LABEL(xc).ZOrder 0
adf.Recordset.MoveNext
xc = xc + 1
Wend </pre>
 
Top