Thread: Delphi help
View Single Post
  #3 (permalink)  
Old 02-25-2008, 03:15 PM
poots poots is offline
Newbie
 
Join Date: Feb 2008
Location: London
Posts: 3
Rep Power: 0
poots is on a distinguished road
Default

the problem you'll be having is that any event fired from within a tedit box will blow up if you try to clear it from inside that event.

This may not be the best solution, but it's the best that comes to mind. You need a form with a tedit and a tmemo on it. The Tmemo contents will be what you need to parse to do the maths.

unit Calcstarter;

interface

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

type
TForm1 = class(TForm)
Edit1: TEdit;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
private
Operators : Set of Char;
Numerals : set of Char;
Specials : set of Char;
public
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
KeyPreview :=true; //this ensures the form gets first look at what's typed
edit1.readonly := true;//to stop users actively entering in it
Operators := ['/','+','-','*'];
Numerals := ['0'..'9'];
Specials := ['.' , ',' , '='];
end;


procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
if Key in Operators then
begin
if length(edit1.text) <> 0 then
begin
memo1.Lines.Add(Key );
memo1.lines.Add(edit1.Text );
edit1.Text := '';
end;
end;

if ((key in Numerals) or (key in Specials)) then
begin
Edit1.Text := edit1.Text + key;
end;
end;

end.
Reply With Quote