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
Solved

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

Posted on 2008-10-10
7
1,122 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
  • 5
  • 2
7 Comments
 
LVL 37

Expert Comment

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

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

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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 37

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 37

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 37

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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…

837 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