Home

Examples of using Strings/Text with Delphi

Here are some interesting things you can do with strings, labels etc: 

Label1.Caption :=  'Grace and ' + Edit1.Text; 
If Edit1.Text = 'Rachel' then 
Result = Grace and Rachel


procedure TForm1.Button1Click(Sender: TObject); 
var N : String; 
begin 
   N := 'Tracy'; 
   Label1.Caption :=  'Grace and ' + N; 
end; 
Result = Grace and Tracy

procedure TForm1.Button1Click(Sender: TObject); 
var I, I2, Result : Integer; 
begin 
  Result := 0; 
  I := StrToInt(Edit1.Text); 
  I2 := StrToInt(Edit2.Text); 
  Result := I + I2; 
  Label1.Caption := IntToStr(I) + ' + ' + IntToStr(I2) + ' = ' + IntToStr(Result); 
end; 

Adding to a Memo.

Editor.Lines.Add('');//Add an empty line to the TMemo

Editor.Lines.Add('Hello world!');//Adds 'Hello world!' to a TMemo

Memo1.Lines.Add(' ' + FormatDateTime('tt  ', Now));


FormatDateTime

Here are some different ways of using FormatDateTime:
 
(FormatDateTime('d - m - yy ', Now));  16 - 11 - 98 
(FormatDateTime('dddddd, ' , Now));  Monday, November 16, 1998,
(FormatDateTime('mm - dd - yyyy ', Now)); 11 - 16 - 1998
(FormatDateTime('d/mmm/yyyy ', Now)); 16-Nov-1998

Text on a StatusBar.

This code will add text to StatusBar panels.
StatusBar1.Panels[0].Text := 'C:\Windows ';
StatusBar1.Panels[1].Text := 'Windows directory';


Messages

Showing a message. (Unit Dialogs)
[procedure ShowMessage(const Msg: string);]
else ShowMessage('User Name Not Found');


Labels

Adding the time to a Label with TimeToStr. (Unit SysUtils)
[function TimeToStr(Time: TDateTime): string;]
Label1.Caption := 'The time is  ' + TimeToStr(Time);

Entering the Time using StrToTime. (Unit SysUtils)
[function StrToTime(const S: string): TDateTime;]
procedure TForm1.Button6Click(Sender: TObject);
var TheTime : TDateTime;
begin
  TheTime := StrToTime(Edit1.Text);
  Label1.Caption := TimeToStr(TheTime);
end;

This code adds the full date to a Label. (Unit SysUtils)
[function FormatDateTime(const Format: string; DateTime: TDateTime): string;]
TheDateLabel.Caption := (FormatDateTime('dddddd. ', Now));
 
 
 
How to use IntToStr and a Label. (Unit SysUtils)
[function IntToStr(Value: Integer): string;]

procedure TForm1.Button4Click(Sender: TObject); 
var I : Integer; 
begin 
  I := 45; 
  Label1.Caption := IntToStr(I); 
end; 
.

How to use StrToInt and a Label. (Unit SysUtils)
[function StrToInt(const S: string): Integer;]

procedure TForm1.Button6Click(Sender: TObject); 
var I : Integer; Str : String; 
begin 
  Str := '321'; 
  I := StrToInt(Str); 
  Label1.Caption := IntToStr(I); 
end; 
.

How to use StrToIntDef and a Label. (Unit SysUtils)
[function StrToIntDef(const S: string; Default: Integer): Integer;]

procedure TForm1.Button6Click(Sender: TObject); 
var Str : string; I : Integer; 
begin 
  Str := Edit1.Text; 
  I := StrToIntDef(Str, 0); {0 default value} 
  Label1.Caption := IntToStr(I); 
end; 
Note: If you enter a valid number into the edit box then the label will have the same value. 
If you type in a non valid integer, then the label will have the default value of 0. 
.

Getting the Opened Files Name.
Label.Caption := Format('%s  %s', [ExtractFileName(FileName), '']); 
.
StrToDate: (Unit SysUtils)

I found that you must use a valid date with StrToDate or an error dialog box appears, informing you that the input is not a valid date. 
Combinations to try: d/m/y, m/d/y and y/m/d. 

procedure TForm1.Button1Click(Sender: TObject); 
var TheDate : TDateTime; 
begin 
    TheDate := StrToDate(Edit1.Text); 
    Label1.Caption := DateToStr(TheDate); 
end; 

Here are some of the examples that I typed into Edit1. 

01/03/1999 
1/3/1999 
1/2/2000  - The label had 1/2/00 
2/23/2010 - The label had 2/23/10 - I would have preferred 2/23/2010 

If you type only 2 numbers into Edit1 like: 

1/2 - The label has 1/2/99 - This is because (According to the online help) the 2 numbers are interpreted as m/d or d/m in the current year. 

Any year numbers (values) between 0 and 99 are assumed to be in the current century. 
See Help for more  information about StrToDate .



StringGrid

Putting text into a StringGrid.
{Set ColCount to 3} {StringGrid1.Cells[Col, Row]}

{The first 3 lines will display the column headings}
  StringGrid1.Cells[0, 0] := 'Name:';
  StringGrid1.Cells[1, 0] := 'Town:';
  StringGrid1.Cells[2, 0] := 'Country:';

  StringGrid1.Cells[0, 1] := 'James';    {Row 1}
  StringGrid1.Cells[1, 1] := 'Tokoroa';
  StringGrid1.Cells[2, 1] := 'New Zealand';

  StringGrid1.Cells[0, 2] := 'Tracy';     {Row 2}
  StringGrid1.Cells[1, 2] := 'Hamilton';
  StringGrid1.Cells[2, 2] := 'USA';

  StringGrid1.Cells[0, 3] := 'Rachel';   {Row 3}
  StringGrid1.Cells[1, 3] := 'Morecambe';
  StringGrid1.Cells[2, 3] := 'England';



 


Home

Tokoroa
North Island
New Zealand


 
Hosted by www.Geocities.ws

1