程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 檢查特殊字符的簡單VCL

檢查特殊字符的簡單VCL

編輯:Delphi

 

這是個很簡單的組件,編寫它的目的也很單純,就是想解決數據錄入時的特殊字符檢查問題。一開始要寫函數實現,不過覺得麻煩,後來就想到寫一個簡單的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寫的。

:)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved