Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wahoo!! I've Learnt to count to ten, all on my own... 1

Status
Not open for further replies.

Bamben

Programmer
Jul 22, 2009
70
IT
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}



procedure TForm1.Button1Click(Sender: TObject);
var
num{, add} : Integer;

begin
num := 0;
Repeat
//1
num := num +1;
Label1.Caption := IntToStr(num);
Sleep(1000);
Application.ProcessMessages;
//2
num := num +1;
Label1.Caption := IntToStr(num);
Sleep(1000);
Application.ProcessMessages;
//3
num := num +1;
Label1.Caption := IntToStr(num);
Sleep(1000);
Application.ProcessMessages;
//4
num := num +1;
Label1.Caption := IntToStr(num);
Sleep(1000);
Application.ProcessMessages;
//5
num := num +1;
Label1.Caption := IntToStr(num);
Sleep(1000);
Application.ProcessMessages;
//6
num := num +1;
Label1.Caption := IntToStr(num);
Sleep(1000);
Application.ProcessMessages;
//7
num := num +1;
Label1.Caption := IntToStr(num);
Sleep(1000);
Application.ProcessMessages;
//8
num := num +1;
Label1.Caption := IntToStr(num);
Sleep(1000);
Application.ProcessMessages;
//9
num := num +1;
Label1.Caption := IntToStr(num);
Sleep(1000);
Application.ProcessMessages;
//10
num := num +1;
Label1.Caption := IntToStr(num);
Sleep(1000);
Application.ProcessMessages;


until num = 10;
end;

end.

I am well pleased!
 
that's great.

While your code works, it's not very efficient. The same output can be achieved with

Code:
var
  num : Integer;
begin
  for num := 1 to 10 do
  begin
    Label1.Caption := IntToStr(num);
    Application.ProcessMessages;
    Sleep(1000);
  end;
end;

 
you also you have the whole block inside a repeat until so you only need the 1
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  num{, add} : Integer;
begin
  num := 0;
  Repeat
    num := num + 1;
    Label1.Caption := IntToStr(num);
    Sleep(1000);
    Application.ProcessMessages;
  until
    num = 10;
end;

Aaron
 
Since two other more efficient suggestions have already been made, I will simply commend you for learning to program in Delphi and say welcome to the community. =)

~
“Your request is not unlike your lower intestine: stinky, and loaded with danger.” — Ace Ventura.
 
Now see what I am using this for! It works good apart from the sleep...because when the application is asleep It CANT make a phone call!!! :p

Why does delphi code ether go faster than sonic the hedgehog or complete stop? isn't there a choice of speed??

My counting method looks ok but its no good for timing actions as all the sleep does is stop the code...If i take the sleeps out then it counts 12345678910 but it does it so fast that all you see is 10 because by the time its started its already finished!!!!

I need some sort of menial task that it can do instead of pausing the whole application (like juggling for a specified number of seconds), other wise my code will never work...

any ideas?




procedure TMainForm.STARTClick(Sender: TObject);
var
Stage : Integer;
pin : String;
InDevice, OutDevice: Cardinal;

begin
Stage := 0;
Repeat
//1
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
CurrentAccount := SimpleSip1.AddAccount(a1SIPCLIENT.Text, a1ACCOUNT.Text, PASSWORDALL.Text);
CurrentAccount._AddRef;
ACTIONL.Caption := 'REGISTRATION SUCCESS!';
SimpleSip1.RegisterAccount(CurrentAccount);
Sleep(0001);
Application.ProcessMessages;

//Start to fill CallPack*1*

//2
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
InDevice := AudioInDeviceNameToDeviceID(waveInDevice.Items[waveInDevice.ItemIndex]);
OutDevice := AudioInDeviceNameToDeviceID(waveOutDevice.Items[waveOutDevice.ItemIndex]);
CurrentCall := SimpleSip1.AddCall(CurrentAccount, PhoneEdit.Text, InDevice, OutDevice); //*1
CurrentCall._AddRef;
ACTIONL.Caption := 'CALLING CurrentCall: '+PhoneEdit.Text+'...';
Sleep(4000);
Application.ProcessMessages;
//3
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
ACTIONL.Caption := 'Sending Pin';
pin := 'ben pin';
pin := ExtractFilePath(ParamStr(0))+pin+'.wav';
SimpleSip1.PlayWavFile(CurrentCall, pin);
Sleep(22000);
Application.ProcessMessages;
//4
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.HoldCall(CurrentCall);
ACTIONL.Caption := 'CurrentCall on Hold';
Sleep(4000);
Application.ProcessMessages;
//5
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
CallPack[1] := CurrentCall;
ACTIONL.Caption := 'CurrentCall Transferred to CallPack*1*';
Sleep(2000);
Application.ProcessMessages;//Now CallPack*1* has 1 call
//6
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
InDevice := AudioInDeviceNameToDeviceID(waveInDevice.Items[waveInDevice.ItemIndex]);
OutDevice := AudioInDeviceNameToDeviceID(waveOutDevice.Items[waveOutDevice.ItemIndex]);
CurrentCall := SimpleSip1.AddCall(CurrentAccount, PhoneEdit.Text, InDevice, OutDevice); //*1
CurrentCall._AddRef;
ACTIONL.Caption := 'CALLING CurrentCall: '+PhoneEdit.Text+'...';
Sleep(2000);
Application.ProcessMessages;
//7
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
ACTIONL.Caption := 'Sending Pin';
pin := 'ben pin';
pin := ExtractFilePath(ParamStr(0))+pin+'.wav';
SimpleSip1.PlayWavFile(CurrentCall, pin);
Sleep(1300);
Application.ProcessMessages;
//8
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.HoldCall(CurrentCall);
ACTIONL.Caption := 'CurrentCall on Hold';
Sleep(1300);
Application.ProcessMessages;
//9
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
CallPack[2] := CurrentCall;
ACTIONL.Caption := 'CurrentCall Transferred to CallPack*1*';
Sleep(2000);
Application.ProcessMessages;//Now CallPack*1* has 2 call (FULL)

//FULL notice *** CallPack*2*

//10
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
ACTIONL.Caption := 'CallPack*1* is FULL...';
Sleep(2000);
Application.ProcessMessages;

//Hold/UnHold CallPack*1*

//11
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.UnHoldCall(CallPack);
ACTIONL.Caption := 'UnHold CallPack*1* -3sec-';
Sleep(2000);
Application.ProcessMessages;
//12
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.HoldCall(CallPack);
ACTIONL.Caption := 'CallPack*1* on Hold';
Sleep(3000);
Application.ProcessMessages;

//CallPack*1* Transferred to BOX

//13
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
CallPack := BOX[1];
ACTIONL.Caption := 'CallPack*1* Transferred to BOX';
Sleep(2000);
Application.ProcessMessages;//Now BOX has 1 CallPack

//******************************************************************************CallPack 1 END

//Start to fill CallPack*2*

//14
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
InDevice := AudioInDeviceNameToDeviceID(waveInDevice.Items[waveInDevice.ItemIndex]);
OutDevice := AudioInDeviceNameToDeviceID(waveOutDevice.Items[waveOutDevice.ItemIndex]);
CurrentCall := SimpleSip1.AddCall(CurrentAccount, PhoneEdit.Text, InDevice, OutDevice); //*1
CurrentCall._AddRef;
ACTIONL.Caption := 'CALLING CurrentCall: '+PhoneEdit.Text+'...';
Sleep(2000);
Application.ProcessMessages;
//15
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
ACTIONL.Caption := 'Sending Pin';
pin := 'ben pin';
pin := ExtractFilePath(ParamStr(0))+pin+'.wav';
SimpleSip1.PlayWavFile(CurrentCall, pin);
Sleep(13000);
Application.ProcessMessages;
//16
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.HoldCall(CurrentCall);
ACTIONL.Caption := 'CurrentCall on Hold';
Sleep(4000);
Application.ProcessMessages;
//17
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
CallPack[1] := CurrentCall;
ACTIONL.Caption := 'CurrentCall Transferred to CallPack*2*';
Sleep(2000);
Application.ProcessMessages;//Now CallPack*2* has 1 call
//18
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
InDevice := AudioInDeviceNameToDeviceID(waveInDevice.Items[waveInDevice.ItemIndex]);
OutDevice := AudioInDeviceNameToDeviceID(waveOutDevice.Items[waveOutDevice.ItemIndex]);
CurrentCall := SimpleSip1.AddCall(CurrentAccount, PhoneEdit.Text, InDevice, OutDevice); //*1
CurrentCall._AddRef;
ACTIONL.Caption := 'CALLING CurrentCall: '+PhoneEdit.Text+'...';
Sleep(200);
Application.ProcessMessages;
//19
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
ACTIONL.Caption := 'Sending Pin';
pin := 'ben pin';
pin := ExtractFilePath(ParamStr(0))+pin+'.wav';
SimpleSip1.PlayWavFile(CurrentCall, pin);
Sleep(13000);
Application.ProcessMessages;
//20
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.HoldCall(CurrentCall);
ACTIONL.Caption := 'CurrentCall on Hold';
Sleep(4000);
Application.ProcessMessages;
//21
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
CallPack[2] := CurrentCall;
ACTIONL.Caption := 'CurrentCall Transferred to CallPack*2*';
Sleep(2000);
Application.ProcessMessages;//Now CallPack*2* has 2 call (FULL)

//FULL notice *** CallPack*2*

//22
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
ACTIONL.Caption := 'CallPack*2* is FULL...';
Sleep(2000);
Application.ProcessMessages;


//Hold/UnHold CallPack*2*

//23
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.UnHoldCall(CallPack);
ACTIONL.Caption := 'UnHold CallPack*2* -3sec-';
Sleep(2000);
Application.ProcessMessages;
//24
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.HoldCall(CallPack);
ACTIONL.Caption := 'CallPack*2* on Hold';
Sleep(3000);
Application.ProcessMessages;

//CallPack*2* Transferred to BOX

//25
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
CallPack := BOX[2];
ACTIONL.Caption := 'CallPack*2* Transferred to BOX';
Sleep(2000);
Application.ProcessMessages;//Now BOX has 1 CallPack

//******************************************************************************CallPack 1 END

//Begin UnHold/Hold BOX
//BOX 6x Hold/UnHold

//26
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.UnHoldCall(BOX);
ACTIONL.Caption := 'UnHold BOX -3sec-';
Sleep(2000);
Application.ProcessMessages;
//27
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.HoldCall(BOX);
ACTIONL.Caption := 'BOX on Hold -30sec-';
Sleep(3000);
Application.ProcessMessages;
//28
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.UnHoldCall(BOX);
ACTIONL.Caption := 'UnHold BOX -3sec-';
Sleep(30000);
Application.ProcessMessages;
//29
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.HoldCall(BOX);
ACTIONL.Caption := 'BOX on Hold -30sec-';
Sleep(3000);
Application.ProcessMessages;
//30
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.UnHoldCall(BOX);
ACTIONL.Caption := 'UnHold BOX -3sec-';
Sleep(30000);
Application.ProcessMessages;
//31
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.HoldCall(BOX);
ACTIONL.Caption := 'BOX on Hold -30sec-';
Sleep(3000);
Application.ProcessMessages;
//32
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.UnHoldCall(BOX);
ACTIONL.Caption := 'UnHold BOX -3sec-';
Sleep(30000);
Application.ProcessMessages;
//33
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.HoldCall(BOX);
ACTIONL.Caption := 'BOX on Hold -30sec-';
Sleep(3000);
Application.ProcessMessages;
//34
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.UnHoldCall(BOX);
ACTIONL.Caption := 'UnHold BOX -3sec-';
Sleep(30000);
Application.ProcessMessages;
//35
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.HoldCall(BOX);
ACTIONL.Caption := 'BOX on Hold -30sec-';
Sleep(3000);
Application.ProcessMessages;
//36
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.UnHoldCall(BOX);
ACTIONL.Caption := 'UnHold BOX -3sec-';
Sleep(30000);
Application.ProcessMessages;
//37
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.HoldCall(BOX);
ACTIONL.Caption := 'BOX on Hold -30sec-';
Sleep(3000);
Application.ProcessMessages;
//38
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.UnHoldCall(BOX);
ACTIONL.Caption := 'UnHold BOX -3sec-';
Sleep(30000);
Application.ProcessMessages;
//39
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.HoldCall(BOX);
ACTIONL.Caption := 'BOX on Hold -30sec-';
Sleep(3000);
Application.ProcessMessages;

//BOX END CALL

//40
Stage := Stage +1;
STAGEL.Caption := IntToStr(Stage);
SimpleSip1.EndCall(BOX);
ACTIONL.Caption := 'All BOX Calls Ended!';
Sleep(30000);
Application.ProcessMessages;

//END

until Stage = 39;
end;
 
add a TTimer object to your form. Set it's Interval property to 1000 (1 second). Add a global variable called TimerCount: Integer. When you perform the action that should count to 10, set TimerCount := 0, and enable the TTimer.

In it's OnTimer event, check if TimerCount < 10. If it is, disable the timer - you're done. otherwise, increment TimerCount, update the label caption, and exit.

done.
 
Thanks Griffyn, I'm not sure if I understood you correctly.
I have tried a few different versions of what 'I think' you mean and so far this version is the closes to what you say.

When I run the application I get no errors. When I press Button1 the application turns to NOT RESPONDING.

What have I done wrong?
***********************************************************

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
TimerCount: Integer;
num : Integer;

implementation

{$R *.dfm}



procedure TForm1.Button1Click(Sender: TObject);
begin
num := 0;
Repeat
//1
Label1.Caption := IntToStr(num);
TimerCount := 0;
Timer1.Enabled := True;
//2
Label1.Caption := IntToStr(num);
TimerCount := 0;
Timer1.Enabled := True;
//3
Label1.Caption := IntToStr(num);
TimerCount := 0;
Timer1.Enabled := True;
//4
Label1.Caption := IntToStr(num);
TimerCount := 0;
Timer1.Enabled := True;
//5
Label1.Caption := IntToStr(num);
TimerCount := 0;
Timer1.Enabled := True;
//6
Label1.Caption := IntToStr(num);
TimerCount := 0;
Timer1.Enabled := True;
//7
Label1.Caption := IntToStr(num);
TimerCount := 0;
Timer1.Enabled := True;
//8
Label1.Caption := IntToStr(num);
TimerCount := 0;
Timer1.Enabled := True;
//9
Label1.Caption := IntToStr(num);
TimerCount := 0;
Timer1.Enabled := True;
//10
Label1.Caption := IntToStr(num);
TimerCount := 0;
Timer1.Enabled := True;


until num = 10;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
if TimerCount < 10 then
begin
Timer1.Enabled := False;
num := num +1;
end;
end;

end.
 

This doesn't work eather:

procedure TForm1.Timer1Timer(Sender: TObject);
begin
inc(TimerCount);
if TimerCount < 10 then
begin
Timer1.Enabled := False;
num := num +1;
end;
exit;
end;
 
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
  Timer1.Enabled := True;
  TimerCount := 0;
  Timer1.Interval := 1000;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if TimerCount < 10 then  { sorry - wrong logic above)
  begin
    Label1.Caption := IntToStr(TimerCount);
    inc(TimerCount);
  end
  else
    Timer1.Enabled := False;
end;
 
wow I was making the assumption that the timer would stop at ten....what your code says is 'when it goes past 10 timer false' I get it now !!!! finally!!! thanks Griffyn
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top