Solved

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

Posted on 2008-10-10
7
1,096 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 36

Expert Comment

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

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

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 36

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 36

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 36

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now