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!

New to delphi and need some help please

Status
Not open for further replies.

scotnew

Programmer
Nov 21, 2003
7
GB

Hiya all,

Im new to delphi and have been creating a compent to work out the average speed for a person in a race. The compent works fine but im wanting to put in error messages with in the code that pop up a show message display.

What im trying to do is say the user enters a number into the edit box instead of a letter it will pop up a show message box saying 'Only enter letters please' This is for edit box1 in my code.

For the other edit boxs 2 and 3 the message will say ' Only enter numbers please'

Hope this makes you understand what im trying to do. All but if there is a way only to let edit boxes take numbers or text that would be great to.

Thanks for you help looking forward to your replies, a new delphi user. I know it sounds simple but having difficulties thanks.

Code below

-----------------------------------------------------
unit CustomPanel2;

interface

uses
SysUtils, Classes, Controls, ExtCtrls, StdCtrls;

type
TDisplayAverage = class(TCustomPanel)

fButton1: TButton;

private
fLabel1: Tlabel;
fLabel2: Tlabel;
fLabel3: Tlabel;
fLabel4: Tlabel;
fLabel5: Tlabel;
fEdit1: TEdit;
fEdit2: TEdit;
fEdit3: TEdit;
FOnClick_FButton1: TNotifyEvent;



{ Private declarations }

protected
procedure Click_fButtonAverage(Sender: TObject); Virtual; {TNotifyEvent}
property Label1: Tlabel read fLabel1 write fLabel1;
property Label2: Tlabel read fLabel2 write fLabel2;
property Label3: Tlabel read fLabel3 write fLabel3;
property Label4: Tlabel read fLabel4 write fLabel4;
property Label5: Tlabel read fLabel5 write fLabel5;
property Edit1: TEdit read fEdit1 write fEdit1;
property Edit2: TEdit read fEdit2 write fEdit2;
property Edit3: TEdit read fEdit3 write fEdit3;
{ Protected declarations }

public
constructor Create(AOwner: TComponent); override;
// destructor destory; override;
{ Public declarations }

published

property OnClick_fButton1: TNotifyEvent read FOnClick_fButton1 write FOnClick_fButton1;
{ Published declarations }
end;
var
time, distance: integer;
result: real;

procedure Register;

implementation

procedure TDisplayAverage.Click_fButtonAverage(Sender: TObject);
begin
time:= StrToInt (Edit2.Text);
distance:= StrToInt (Edit3.Text);
result:= time / distance;
Label5.caption:= floattostr(result);
if assigned(FOnClick_fButton1) then
FOnClick_fbutton1(Self);
end;

constructor TDisplayAverage.Create(AOwner: TComponent);

begin
inherited Create (AOwner);
bevelinner := bvlowered;
Name :='o';
height :=180;
width :=340;

// Bevelinner sets the bevel on the panel and the height and width set the
// panel size.

fEdit1 := TEdit.Create(self);
with fEdit1 do
begin
left := 150;
top :=20;
Parent:= self;
end;

fEdit2 := TEdit.Create(self);
with fEdit2 do
begin
left := 150;
top :=60;
Parent:= self;
end;

fEdit3 := TEdit.Create(self);
with fEdit3 do
begin
left := 150;
top :=100;
Parent:= self;
end;

fLabel1 := TLabel.Create(Self);
with flabel1 do
begin
Caption := 'Enter your name:';
left := 10;
top :=20;
Parent := Self;
end;

fLabel2 := TLabel.Create(Self);
with flabel2 do
begin
Caption := 'Enter your finishing time:';
left := 10;
top :=60;
Parent := Self;
end;

fLabel3 := TLabel.Create(Self);
with flabel3 do
begin
Caption := 'Enter Distance:';
left := 10;
top :=100;
Parent := Self;
end;

fLabel4 := TLabel.Create(Self);
with flabel4 do
begin
Caption := '=';
left := 230;
top := 145;
Parent := Self;
end;

fLabel5 := TLabel.Create(Self);
with flabel5 do
begin
Caption := '';
left := 235;
top := 140;
font.size := 16;
Parent := Self;
end;

fButton1:= TButton.create(Self);
with fButton1 do
begin
width:= 200;
height:= 25;
left := 10;
top := 140;
Caption:= 'Calculate Average Speed for Race';
Parent:= Self;
OnClick := Click_fButtonAverage;
end;
end;


procedure Register;
begin
RegisterComponents('Samples', [TDisplayAverage]);
end;

end.
 
you need to use TMaskEdit components instead of TEdit...
 
Thanks DazzleD will clean up my code by using TMaskEdit, but im still not show how to enter the code to make a Show message box pop to display an error if a number is entered instead of a letter. Thanks in advance for your help.
 
A maskedit will prolly prevent the user from entering unwanted characters thus negating the need for a showmessage, I would definitely advice *against* showing the messagebox from a GUI point of view, its highly irritating to get messageboxes while typing, you're not gonna please your users with this approach.

just my two cents...
 
The first thing in the onclick envent of the button should be code like this.

var
i : integer;
begin
try
i := StrToInt(edit1.text);
// Process input here
except
showmessage('You must enter an Integer');
end;
end;

If what has been entered into the edit1 box on the form is not an integer, the StrToInt conversion will fail, and an error message will be displayed.

 
Thanks petertickle but when i enter that code in at the button i get an error which i entered in at the showmessage part in the code below. the error is [Error] CustomPanel2.pas(69): Undeclared identifier: 'ShowMessage'

Thanks for your help

----------------------------------------

implementation

procedure TDisplayAverage.Click_fButtonAverage(Sender: TObject);
begin
time:= StrToInt (Edit2.Text);
distance:= StrToInt (Edit3.Text);
result:= time / distance;
Label5.caption:= floattostr(result);
try
input := StrToInt(edit1.text);
// Process input here
except
ShowMessage('You must enter an Number'); //[Error] CustomPanel2.pas(69): Undeclared identifier: 'ShowMessage'
if assigned(FOnClick_fButton1) then
FOnClick_fbutton1(Self);
end;
end;
 
ScotNew, I have just seen your message. LesPaul has given you the correct advice. "Showmessage" is part of the Dialogs unit, and therefore "Dialogs" needs to be included in the uses clause at the top of your program.

Peter
 
In this case I think prevention is better than detection (so I agree with Buck's comments above), a well-designed GUI should negate the need for messages of the type you are trying to generate. If you do need some kind of feedback for incorrect input then the use of a sound is more appropriate e.g. when the user types a letter in a "numbers only" edit box, it bleeps and doesn't allow the letter as input.

Here is a code example of an edit box that only accepts numeric input:
Code:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
const
  ValidInput = [ '0'..'9', #8];
begin
  if not ( key in ValidInput ) then
  begin    
    key := #0;
    Beep;
  end;
end;
This ensures users can only enter keys 0 to 9 and the backspace key. You can omit the beep if you wish.

Hope this helps!

Clive [infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top