檢查特殊字元的簡單VCL (轉)

worldblog發表於2007-12-12
檢查特殊字元的簡單VCL (轉)[@more@]

 

這是個很簡單的,編寫它的目的也很單純,就是想解決資料錄入時的特殊字元檢查問題。一開始要寫實現,不過覺得麻煩,後來就想到寫一個簡單的VCL來遍歷Form上所有的元件的方法。這個VCL目前只是檢查所有的TEdit和TComboBox元件,有興趣的朋友可以自己擴充功能。

我想這個VCL對於編寫的人員來說還是有一點幫助的,比如對單引號的Check。

想要檢查什麼符號只要在屬性中設定一下就搞定,而且執行時只需要一個Checking函式,他就會自動檢查咯。

它的另一個作用就是可以作為編寫VCL的簡單例程。

 

unit SymbolChecker;

interface

uses
  , 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); overr;
  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.


最後要說明一點的是它是用6寫的。

:)


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-992014/,如需轉載,請註明出處,否則將追究法律責任。

相關文章