Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1174
  • Last Modified:

how to make Edit control to take only hex numbers in delphi 7?

Hi experts,

Ihave an image processing application. I need edit control which takes only Hex numbers(it should restrict special charecters and( other  then Hex )alpha  chars.  Is their any built in way?

0
inampudi1
Asked:
inampudi1
  • 5
  • 2
1 Solution
 
Geert GruwezOracle dbaCommented:
use Regular expressions
with a TMaskEdit

http://www.latiumsoftware.com/en/articles/00009.php
0
 
Geert GruwezOracle dbaCommented:
and the expression =

0*[1-9A-F][0-9A-F]*
0
 
inampudi1Author Commented:
is it free software. or I need to buy?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Geert GruwezOracle dbaCommented:
it's freeware

it's a unit you add to your application and then use in code:

downloadpage
http://www.regexpstudio.com/TRegExpr/TRegExpr.html

in English :
http://www.regexpstudio.com/Downloads/regexpr.zip

0
 
inampudi1Author Commented:
HI,

I am using  as below , but it is not working.

procedure TForm1.Edit1Change(Sender: TObject);
var
  RegExpr: TRegExpr;

begin

  RegExpr := nil;
  try
    RegExpr := TRegExpr.Create;
    if RegExpr <> nil then begin
      RegExpr.Expression := '[0-9] + [A-F]';

      if RegExpr.Exec(edit1.Text) then
        repeat
          edit1.Text := (RegExpr.Match[1]);
        until not RegExpr.ExecNext;
    end;
  except
  end;
  RegExpr.Free;
end;
0
 
Geert GruwezOracle dbaCommented:
uses RegExpr;

{$R *.dfm}

procedure TForm1.Edit1Change(Sender: TObject);
var Failed: Boolean;
  I, FailPos: Integer;
  re: TRegExpr;
begin
  if edit1.Text <> '' then
  begin
    Failed := False;
    FailPos := 0;
    re := TRegExpr.Create;
    try
      re.Expression := '0*[1-9A-F][0-9A-F]*';
      if re.Exec(Edit1.Text) then
      begin
        for I := 2 to Length(Edit1.Text) do
          if not re.Exec(I) then
          begin
            Failed := False;
            FailPos := I;
            Break;
          end;
      end else
      begin
        Failed := True;
        FailPos := 1;
      end;
    finally
      FreeAndNil(re);
    end;
    if FailPos <> 0 then
      ShowMessage('Wrong character at position ' + IntToStr(FailPos));
  end;
end;
0
 
Geert GruwezOracle dbaCommented:
My 2 cents on your code:

procedure TForm1.Edit1Change(Sender: TObject);
var
  RegExpr: TRegExpr;  
  // RegExpr is the same name as the unit RegExpr ...
  // Use an other variable
begin
  RegExpr := nil;
  // This is not necessary unless you put the create after the try
  try
    RegExpr := TRegExpr.Create;
    // Put the create before the try
    if RegExpr <> nil then
    begin
      // This is also not necessary as the variable will allways be initialised unless you have an exception
      // But when you have an exception it will be EOutOfMemory
      RegExpr.Expression := '[0-9] + [A-F]';

      if RegExpr.Exec(edit1.Text) then
        repeat
          edit1.Text := (RegExpr.Match[1]);
        until not RegExpr.ExecNext;
    end;
  except
  end;
  RegExpr.Free;
  // These last three ... If I had an exception ignore it, and even if you couldn't create the object
  // free it anyway
end;

if you want it with exceptions, see below
uses RegExpr;
 
{$R *.dfm}
 
procedure TForm1.Edit1Change(Sender: TObject);
var Failed: Boolean;
  I, FailPos: Integer;
  re: TRegExpr;
begin
  if edit1.Text <> '' then
  begin
    Failed := False;
    FailPos := 0;
    re := TRegExpr.Create;
    try
      try
        re.Expression := '0*[1-9A-F][0-9A-F]*';
        if re.Exec(Edit1.Text) then
        begin
          for I := 2 to Length(Edit1.Text) do
            if not re.Exec(I) then
            begin
              Failed := False;
              FailPos := I;
              Break;
            end;
        end else
        begin
          Failed := True;
          FailPos := 1;
        end;
      except
        // ignore exceptions, only if the object was created successfully
      end;
    finally
      FreeAndNil(re);
    end;
    if FailPos <> 0 then
      ShowMessage('Wrong character at position ' + IntToStr(FailPos));
  end;
end;

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now