To achieve communication between a child form and a parent form, there are a variety of methods (such as the structure of the recovery of the subform, and deliver the reference to the parent form as a parameter). Below I want to introduce a method of using custom events, it can maximize the coupling between modules to fully reflect the advantages of object-oriented objects.
First show the effect map to everyone:
Here is the implementation code:
Unit2 // Subform
Type
// Declare the type of custom event (similar to the commission in C #)
TMYEVENTHANDLE = Procedure (sender: TOBJECT; Content: String) OF Object;
TFORM2 = Class (TFORM)
ComboBOBOX1: TcomboboX;
Label1: TLABEL;
Procedure ComboBox1click (Sender: TOBJECT);
Private
FonselectriTChanged: TmyeventHandle;
public
// State a custom event
Property OneSelectionChange: TmyeventHandle
Read FonselectriChanged Write FonselectriChanged;
END;
Procedure TFORM2.COMBOBOX1CLICK (Sender: TOBJECT);
Begin
// Trigger a custom event when selecting a change
IF assigned (FonseChanged) THEN
FonselectriChanged (Self, ComboBoX1.text);
END;
Unit1 // parent form
Type
TFORM1 = Class (TFORM)
btnopenform2: tbutton;
EDIT1: TEDIT;
Label1: TLABEL;
Procedure btnopenform2click (sender: TOBJECT);
Private
M_FRM2: TFORM2;
Procedure frM2_selectionchanged (sender: Tobject; Content: string);
public
Constructor Create (Aowner: Tcomponent); OVERRIDE;
DESTRUCTOR DESTROY; OVERRIDE;
END;
Constructor TFORM1.CREATE (Aowner: Tcomponent);
Begin
Inherited Create (Aowner);
M_FRM2: = TFORM2.CREATE (Self);
/ / Specify a handler when M_FRM2 occurs
m_frm2.onselectionchanged: = frm2_selectionchanged;
END;
DESTRUCTOR TFORM1.DESTROY;
Begin
m_frm2.free;
Inherited destroy;
END;
Procedure TFORM1.FRM2_SELECTIONCHANGED (Sender: Tobject; Content: String);
Begin
Edit1.Text: = Content;
END;
Procedure TFORM1.BTNOPENFORM2CLICK (Sender: TOBJECT);
Begin
m_frm2.showmodal;
END;