سلام؛
فكر كنم مسئله را به صورت زير مطرح كنم، مشخصتر باشه:
به عنوان مثال، كد زير را در نظر بگيريد كه داراي يك فيلد از نوع Edit هست كه به Edit روي فرم متصل ميشه و يك فيلد از نوع String به نام TextNeed.
قراره اين كامپوننت در صورتي كه متن Edit برابر متن فيلد TextNeed بود، يه پيام نشون بده.
در عين حال، قراره در هنگامي كه درون Edit هم متني تايپ ميشه، به عنوان مثال متن عيناً در يك Lable نمايش داده بشه.
حالا اگر دو تا از اين كامپوننت روي فرم استفاده كنيم و هر دو را هم به يك Edit كه روي فرم قرار داره، فقط كامپوننت دوم، رويداد OnChange مربوط به Edit را هندل ميكنه و ساير كامپوننتها و حتي خود رويداد OnChange مربوط به Edit، اجرا نميشه.
كد مربوط به كامپوننت:
unit MyComponentTest;
interface
uses
SysUtils, Classes, Windows, StdCtrls, ComCtrls, Forms, Messages, Graphics, Controls, Dialogs;
type
TMyComponentTest = class(TComponent)
private
{ Private declarations }
FEditBox: TEdit;
FTextNeed: String;
procedure SetEditBox(AEditBox: TEdit);
procedure SetTextNeed(ATextNeed: String);
protected
{ Protected declarations }
procedure Notification(AComponent: TComponent; Operation: TOperation);
override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure OnMyChange(Sender: TObject);
published
{ Published declarations }
property EditBox: TEdit read FEditBox write SetEditBox;
property TextNeed: String read FTextNeed write SetTextNeed;
end;
procedure Register;
implementation
uses StrUtils;
procedure Register;
begin
RegisterComponents('My Component', [TMyComponentTest]);
end;
constructor TMyComponentTest.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FTextNeed := '';
end;
destructor TMyComponentTest.Destroy;
begin
inherited Destroy;
end;
procedure TMyComponentTest.Notification(AComponent: TComponent; Operation: TOperation);
begin
if Operation = opRemove then
begin
if AComponent = FEditBox then FEditBox := nil;
end;
end;
procedure TMyComponentTest.OnMyChange(Sender: TObject);
var
TempString: String;
begin
inherited;
if EditBox.Text = FTextNeed then
ShowMessage(Self.Name + ': ' + FTextNeed);
end;
procedure TMyComponentTest.SetEditBox(AEditBox: TEdit);
begin
FEditBox := AEditBox;
if FEditBox <> nil then
FEditBox.OnChange := OnMyChange;
end;
procedure TMyComponentTest.SetTextNeed(ATextNeed: String);
begin
FTextNeed := ATextNeed;
end;
end.
كد مربوط به فرم:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, MyComponentTest;
type
TForm1 = class(TForm)
MyComponentTest1: TMyComponentTest;
Edit1: TEdit;
Label1: TLabel;
MyComponentTest2: TMyComponentTest;
procedure Edit1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Edit1Change(Sender: TObject);
begin
Label1.Caption := Edit1.Text;
end;
end.
و فرم به صورت متني:
object Form1: TForm1
Left = 336
Top = 149
Width = 979
Height = 563
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 440
Top = 160
Width = 32
Height = 13
Caption = 'Label1'
end
object Edit1: TEdit
Left = 440
Top = 128
Width = 121
Height = 21
TabOrder = 0
Text = 'Edit1'
end
object MyComponentTest1: TMyComponentTest
EditBox = Edit1
TextNeed = 'Test'
Left = 376
Top = 120
end
object MyComponentTest2: TMyComponentTest
EditBox = Edit1
TextNeed = 'Ali'
Left = 376
Top = 160
end
end
كه البته اين كامپوننت فقط يك مثال هست.
حالا ميخوام، وقتي درون Edit متني وارد ميشه، هر دو تا كامپوننت بتونند متن را چك كنند، و در عين حال، متن به Label هم منتقل بشه.
باز هم از همه دوستان، عليالخصوص آقاي كشاورز تشكر ميكنم.
خيلي ممنون... يا علي... موفق باشيد...