Solved

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

Posted on 2008-10-10
7
1,119 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to make AngularJS IntelliSense working in VS 2012 4 175
git pull --rebase :always complain unstaged files 11 203
selectallvieditor 6 115
regex. How to include lines between 10 84
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.
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 columnThat will then direct you to their download page.From that page s…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

831 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