====== Polyform ======
Today we'll talk about polymorphic forms. I call it "polyform". Ok, what is it and how to do it in different programming languages. This form contains the controls are created "on-a-fly" -- when the program is running.
I like those forms because the form could have any set of controls depending on the data is processing.
===== Delphi =====
My favourite language. So the main panel should be a parent for any new control:
procedure TForm1.NewTLabel(X,Y: integer; S: string);
var L: TLabel;
begin
L:=TLabel.Create(self);
L.Left:=X;
L.Top:=Y;
L.Caption:=S;
L.Parent:=self
end;
It's enough to see the new one control on the form.
Events? Ok, when user will click on the Tlabel -- it should to show its caption.
procedure TForm1.AnyLabelClick(Sender: Object);
begin
if Sender is TLabel then //to prevent an error if its not TLabel
ShowMessage(TLabel(Sender).Caption);
end;
... add to the end of TForm1.NewTLabel:
L.onClick:=AnyLabelClick;
Similar methods for all others. Also we could use .Tag property to store some values in it.
===== Silverlight (C#) =====
The similar method we will use here.
Ok, our XAML-file contains next: