检查特殊字符的简单VCL

类别:Delphi 点击:0 评论:0 推荐:

 

这是个很简单的组件,编写它的目的也很单纯,就是想解决数据录入时的特殊字符检查问题。一开始要写函数实现,不过觉得麻烦,后来就想到写一个简单的VCL来遍历Form上所有的组件的方法。这个VCL目前只是检查所有的TEdit和TComboBox组件,有兴趣的朋友可以自己扩充功能。

我想这个VCL对于编写数据库程序的人员来说还是有一点帮助的,比如对单引号的Check。

想要检查什么符号只要在属性中设置一下就搞定,而且运行时只需要执行一个Checking函数,他就会自动检查咯。

它的另一个作用就是可以作为编写VCL的简单入门例程。

 

unit SymbolChecker;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls,StdCtrls,Dialogs,StrUtils;

type
  TCheckControl = (cTEdit, cTComboBox);
  TCheckControls = set of TCheckControl;

  TSymbolChecker = class(TComponent)
  private
    { Private declarations }
    FActive: boolean;
    FCheckControls:TCheckControls;
    FCheckString:String;
    procedure SetActive(Value: boolean);
    procedure SetCheckControls(Value: TCheckControls);
    procedure SetCheckString(Value: String);
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
  published
    { Published declarations }
    procedure Checking();
    property Active: boolean read FActive write SetActive default false;
    property CheckControls: TCheckControls read FCheckControls write SetCheckControls default [cTEdit, cTComboBox];
    property CheckString: String read FCheckString write SetCheckString;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('MyVCL', [TSymbolChecker]);
end;

constructor TSymbolChecker.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  CheckControls := [cTEdit, cTComboBox];
end;

{ Set property Active Value }
procedure TSymbolChecker.SetActive(Value: boolean);
begin
  if Value = FActive then exit;
  FActive := Value;
end;

{ Set property CheckControls Value }
procedure TSymbolChecker.SetCheckControls(Value: TCheckControls);
begin
  if Value = FCheckControls then exit;
  FCheckControls := Value;
end;

{ Set property CheckString Value }
procedure TSymbolChecker.SetCheckString(Value: String);
begin
  if Value = FCheckString then exit;
  FCheckString := Value;
  if Trim(FCheckString) = '' then SetActive(false);
end;

procedure TSymbolChecker.Checking();
var
   I,J:integer;
begin
   { property Active=true then execute }
   if FActive then
   begin
      { property CheckTEdit=true then execute }
         for I:= 0 to owner.ComponentCount - 1 do
         begin
            { Check TEdit }
            if (owner.Components[I] is TEdit) and (cTEdit in CheckControls) then
            begin
               for J :=1 to Length(Trim(FCheckString)) do
               begin
                  if pos(MidStr(Trim(FCheckString),J,1),TEdit(owner.Components[I]).Text)>0 then
                  begin
                     ShowMessage('error symbol!');
                     TEdit(owner.Components[I]).SetFocus;
                     exit;
                  end;
               end;
            end;
            { Check TComboBox }
            if (owner.Components[I] is TComboBox) and (cTComboBox in CheckControls) then
            begin
               for J :=1 to Length(Trim(FCheckString)) do
               begin
                  if pos(MidStr(Trim(FCheckString),J,1),TComboBox(owner.Components[I]).Text)>0 then
                  begin
                     ShowMessage('error symbol!');
                     TComboBox(owner.Components[I]).SetFocus;
                     exit;
                  end;
               end;
            end;
         end;
   end;
end;

end.


最后要说明一点的是它是用Delphi6写的。

:)

本文地址:http://com.8s8s.com/it/it5527.htm