In some cases developing custom projects may require expanding properties or methods of the standard form by inheriting from custom classes or interfaces. The further access to custom properties or methods requires to cast the forms to a custom class or interface. If on executing a program you cannot determine for sure whether a form can be cast to a custom class or a custom interface, use the Is operator to check the form for the ability to be cast. A standard checking option using the MDI application is given below.
Executing the example requires the main MDI form and several child MDI forms. The main MDI form contains a toolbar with two buttons. One of the child MDI forms contains an extension using the IUserInterface user interface. One property is described in the interface, the property implementation is represented in the form class. A possible form code is given below:
The main MDI form:
Class MDIMainForm: Form
Toolbar1: Toolbar;
ToolbarButton1: ToolbarButton;
ToolbarButton2: ToolbarButton;
Form1: MDIChild1Form;
Form2: MDIChild2Form;
//...
//Variables for n child MDI forms
//...
Sub MDIMainFormOnShow(Sender: Object; Args: IEventArgs);
Begin
Form1 := New MDIChild1Form.CreateForm(Self As IWin32Window);
Form2 := New MDIChild2Form.CreateForm(Self As IWin32Window);
//...
//Create n child MDI forms
//...
End Sub MDIMainFormOnShow;
Sub ToolbarButton1OnClick(Sender: Object; Args: IEventArgs);
Begin
If Self.MDIActive Is IUserInterface Then
Text := (Self.MDIActive As IUserInterface).UserProp;
End If;
End Sub ToolbarButton1OnClick;
Sub ToolbarButton2OnClick(Sender: Object; Args: IEventArgs);
Begin
If Self.MDIActive Is IUserInterface Then
(Self.MDIActive As IUserInterface).UserProp := Math.Rand.ToString;
End If;
End Sub ToolbarButton2OnClick;
End Class MDIMainForm;
The child MDI form expanded using the user interface:
Public Class MDIChild1Form: Form, IUserInterface
EditBox1: EditBox;
Sub Set_UserProp(s: String);
Begin
EditBox1.Text := s;
End Sub Set_UserProp;
Function Get_UserProp: String;
Begin
Return EditBox1.Text;
End Function Get_UserProp;
End Class MDIChild1Form;
Public Interface IUserInterface
Sub Set_UserProp(s: String);
Function Get_UserProp: String;
Public Property UserProp: String Get Get_UserProp Set Set_UserProp;
End Interface IUserInterface;
After executing the example, clicking the buttons on the toolbar of the main MDI form checks if an active child MDI form can be cast to the IUserInterface user interface. If it can be cast, the UserProp property value described in the IUserInterface interface is read or set depending on the button pressed. On reading the value is displayed to form header. A random real number is used as the value to be set.
See also: