Hi
Attached project, that you may find useful.
You can use the tag property to select the range you wish to use, the ranges are set in in an array; so you can modify/add at will.
you can set the normal and error colors globally; these used when error detected.
It is just a follow on from previous post, but you now have a project to play with. You will notice the same onchange routine is called for each tedit control (using the Tedit(Sender)). The code
If Sender is TEdit then is a failsafe, it makes sure that the sending component is actually a tedit; just incase for some reason the event gets called from some other component by mistake. "catch possible errors and handle them before they happen"
As you pogress; you will learn that you can ceate your own components ( derived from another); this allows you to use a standard component (tedit) but add your extra properties like; min, max, error color; normal color etc as properties for the 'NEW' control, or use 3rd party components
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
procedure Edit1Change(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
public
end;
// a record structure to hold the min and max values
TMyRange=Record
Min,Max:LongInt;
End;
var
Form1: TForm1;
// colors for normal and error value
okcolor:tcolor=clSkyBlue;
errcolor:tcolor=clred;
// create array of 3 possible ranges
MyRanges:Array[1..3] of TMyRange;
implementation
{$R *.lfm}
{ TForm1 }
function IsInRange(const AValue,ALow, AHigh: Integer): Boolean;
begin
Result := (AValue >= ALow) and (AValue <= AHigh);
end;
procedure TForm1.Edit1Change(Sender: TObject);
Var MyValue:LongInt;
Ok:Boolean;
begin
If Sender is TEdit then
Begin
Ok:=TryStrToInt(TEdit(Sender).Text,MyValue);
If Ok then
Begin
// check if tag is used that it is range of available ranges
if IsInRange(TEdit(Sender).tag,Low(MyRanges),High(MyRanges)) Then
Begin
// now check if value is within the ranges of the tag
If IsInRange(MyValue,MyRanges[TEdit(Sender).tag].Min,MyRanges[TEdit(Sender).tag].max) then tedit(sender).color:=okcolor
else ok:=false;
end;
end;
If Ok=false then tedit(sender).color:=errcolor;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
// Set the Ranges options
MyRanges[1].Min:=1;
MyRanges[1].Max:=12;
MyRanges[2].Min:=3;
MyRanges[2].Max:=100;
MyRanges[3].Min:=101;
MyRanges[3].Max:=1000;
// setting label text
label1.Caption:='Tag 0 No Range';
label2.Caption:='Tag 1 Range '+inttostr(MyRanges[1].Min)+' to '+inttostr(MyRanges[1].Max);
label3.Caption:='Tag 2 Range '+inttostr(MyRanges[2].Min)+' to '+inttostr(MyRanges[2].Max);
label4.Caption:='Tag 3 Range '+inttostr(MyRanges[3].Min)+' to '+inttostr(MyRanges[3].Max);
end;
end.