SRL Archive - SRL Archives

An improved ReadLn.


Ron

2006-12-31 17:27:27

The name is very creative… ReadLn2 :)

Any way, here is the code. With this new ReadLn, you can change the Title,Pretext, and the the question text.

It has to use at least 3 global vars though. Look through the code and you'll see why.


program TestReadLn2;

var
frmReadLn2 : TForm;
ReadLn2Answer: TEdit;
ReadLn2Label1: TLabel;

procedure ReadLn2Cancel(sender: TObject);
begin
ReadLn2Answer.Text := '';
frmReadLn2.MODALRESULT := 2;
end;

procedure ReadLn2CheckEnter(sender: TObject; var key: char);
begin
if (key = Chr(13)) then
begin
frmReadLn2.modalresult := mrOk;
end;
end;

procedure ReadLn2Form;
var
Button1,Button2: TButton;
begin
frmReadLn2 := CreateForm;
with frmReadLn2 do
begin
BorderStyle := bsSingle;
ClientHeight := 90;
ClientWidth := 270;
Position := poScreenCenter;
BorderIcons := [biSystemMenu];
end;
ReadLn2Label1 := TLabel.Create(frmReadLn2);
with ReadLn2Label1 do
begin
Parent := frmReadLn2;
Left := 15;
Top := 12;
Width := 32;
end;
ReadLn2Answer := TEdit.Create(frmReadLn2);
with ReadLn2Answer do
begin
Parent := frmReadLn2;
Left := 15;
Top := 30;
Width := 230;
OnKeyPress := @ReadLn2CheckEnter;
end;
Button1 := TButton.Create(frmReadLn2);
with Button1 do
begin
Parent := frmReadLn2;
Left := 55;
Top := 60;
Caption := 'OK';
ModalResult := 1;
end;
Button2 := TButton.Create(frmReadLn2);
with Button2 do
begin
Parent := frmReadLn2;
Left := 135;
Top := 60;
Caption := 'Cancel';
OnClick := @ReadLn2Cancel;
end;
end;

procedure SafeReadLn2Form;
var
v: TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('ReadLn2Form', v);
end;

procedure ShowReadLn2FormModal;
begin
frmReadLn2.ShowModal;
end;

procedure SafeShowReadLn2FormModal;
var
v: TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('ShowReadLn2FormModal', v);
end;

function ReadLn2(Question, Title, PreText: string): string;
begin
try
SafeReadLn2Form;
ReadLn2Label1.Caption := Question;
frmReadLn2.Caption := Title;
ReadLn2Answer.Text := PreText;
SafeShowReadLn2FormModal;
Result := ReadLn2Answer.Text;
finally
ReadLn2Answer.Free;
ReadLn2Label1.Free;
FreeForm(frmReadLn2);
except
Result := '';
end;
end;

begin
ClearDebug;
WriteLn(ReadLn2('Do you like this new ReadLn?','ReadLn2 Question','Yes'));
end.

~Ron

WhiteShadow

2007-01-07 17:01:10

hey, that's cool Ron. I might use it in my script to add to the sexiness. :)

Ron

2007-01-07 20:30:09

cool, you can also set a maxlength to the function, which is what I needed to do for a different script.

To add a maxlength, which you probably know how to do, but for the people who don't, replace this:

function ReadLn2(Question, Title, PreText: string): string;
begin
try
SafeReadLn2Form;
ReadLn2Label1.Caption := Question;
frmReadLn2.Caption := Title;
ReadLn2Answer.Text := PreText;
SafeShowReadLn2FormModal;
Result := ReadLn2Answer.Text;
finally
ReadLn2Answer.Free;
ReadLn2Label1.Free;
FreeForm(frmReadLn2);
except
Result := '';
end;
end;

with this:

function ReadLn2(Question, Title, PreText: string; MaxLen : integer): string;
begin
try
SafeReadLn2Form;
ReadLn2Label1.Caption := Question;
frmReadLn2.Caption := Title;
ReadLn2Answer.Text := PreText;
ReadLn2Answer.MaxLength := MaxLen;
SafeShowReadLn2FormModal;
Result := ReadLn2Answer.Text;
finally
ReadLn2Answer.Free;
ReadLn2Label1.Free;
FreeForm(frmReadLn2);
except
Result := '';
end;
end;

~Ron