Solved

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

Posted on 2008-10-10
7
1,151 Views
Last Modified: 2013-11-23
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
Comment
Question by:inampudi1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 22685303
use Regular expressions
with a TMaskEdit

http://www.latiumsoftware.com/en/articles/00009.php
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 22685320
and the expression =

0*[1-9A-F][0-9A-F]*
0
 

Author Comment

by:inampudi1
ID: 22685367
is it free software. or I need to buy?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 22685413
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
 

Author Comment

by:inampudi1
ID: 22685601
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
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 22686775
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
 
LVL 38

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 22686838
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question