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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do i make scrolling text?

Status
Not open for further replies.

RyanRamsey99

Technical User
Jan 29, 2001
7
US
hello.
i'm trying to do a marquee style label with scrolling text
but i'm not sure how i should do it
i used the copy function and i thought that would work but the text does not scroll, can someone tell me what i am doing wrong?
i appreciate any help.
thanks
ryanramsey_99@yahoo.com
 
This is just an idea to help you. Change the sleep time and the size you want to print each time (nSize) to get the efects you want. If it needs to run forever create a different thread.


var
sAux: String;
nLength: Integer;
nSize: Integer;
nPos: integer;
bLeave: Boolean;
begin
sAux := 'This is a string to help in simulating a marquee';
nLength := Length(sAux);
nSize := 5;
nPos := 1;
bLeave := false;
while not bLeave do
begin
Label1.Caption := Copy(sAux,nPos,nSize);
Sleep(100);
Application.ProcessMessages;
Inc(nPos);
if ((nPos + nSize) - nLength) > nSize then
begin
bLeave := true;
end;
end;
Label1.Caption := '';
end;

Hope this helps.
Alphonsus.
 
Here's a second idea to help you out. On your form, drop two timers, a label, and three buttons. Label your buttons, Scroll left, scroll right, and quit.
Here is the implementation:


implementation

{$R *.DFM}

procedure TForm1.Button3Click(Sender: TObject);
begin
Close;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ScrollText := ' This is a demonstration of scrolling text ';
Label1.Caption := ScrollText;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Timer2.Enabled := true;
Timer1.Enabled := false;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
NewText : AnsiString;
SLen : Integer;
Sub : AnsiString;

begin
{ get the rightmost character from scrolltext }
SLen := Length(ScrollText);
Sub := ScrollText[SLen];

{ trim the strings }
ScrollText := Copy(ScrollText, 1, SLen -1);
NewText := Sub + ScrollText;
Label1.Caption := NewText;
ScrollText := NewText;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Timer1.Enabled := true;
Timer2.Enabled := false;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
var
NewText : AnsiString;
SLen : Integer;
Sub : AnsiString;

begin
{ get the leftmost character from scrolltext }
SLen := Length(ScrollText);
Sub := ScrollText[1];

{ trim the strings }
ScrollText := Copy(ScrollText, 2, SLen -1);
NewText := ScrollText + Sub;
Label1.Caption := NewText;
ScrollText := NewText;

end;

end.
 
Previous may have a few too many components.
Simple and quite OO is...,
One timer, one edit (or any text comp. and a few vars.


procedure TForm1.Timer1Timer(Sender: TObject);
var
WrappedStr : string;
begin
if StrLength - CurrentPos >= CharsToDisplay then
begin
WrappedStr := Copy(StrToDisplay, CurrentPos, CharsToDisplay);
Inc(CurrentPos);
end
else
begin
WrappedStr := Copy(StrToDisplay, CurrentPos, StrLength - CurrentPos) +
Copy(StrToDisplay, 0, CharstoDisplay - (StrLength - CurrentPos));
if CurrentPos = StrLength then CurrentPos := 0 else Inc(CurrentPos);
end;
Edit1.Text := WrappedStr;
end;

procedure TForm1.FormCreate(Sender: TObject);
and vars.
begin
// Either define as vars and pass in in overridden creator or define as consts.

StrToDisplay := 'Test string';
CharsToDisplay := 5; // ensure lower than StrLength;

// Define as vars (properties)or whatever
StrLength := Length(StrToDisplay);
CurrentPos := 0; // init;
Timer1.Interval := 150; // could pass this in too
end;


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top