检查特殊字符的简单VCL (转)

本文介绍了一个简单的VCL组件——SymbolChecker,用于检查输入字段中的特殊字符,适用于Delphi数据库应用程序开发。该组件能够遍历窗体上的TEdit和TComboBox控件,并验证输入是否包含预定义的非法字符。
检查特殊字符的简单VCL (转)[@more@]

 

这是个很简单的组件,编写它的目的也很单纯,就是想解决数据录入时的特殊字符检查问题。一开始要写函数实现,不过觉得麻烦,后来就想到写一个简单的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写的。

:)


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-992014/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-992014/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值