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:

<Window
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   x:Class="Project1.TestForm"
   x:Name="Window"
   Title="TestForm"
   Width="475" Height="630" Icon="Application.ico" ResizeMode="CanMinimize" Background="#FFFFFFCC">
   <Grid x:Name="LayoutRoot">
   </Grid>
</Window>

As you see the main grid “LayoutRoot” – is empty.

  public TextBlock AddTextBlock(Grid root, string Label, string TBName)
  {
      TextBlock L = new TextBlock();
      L.Margin = new Thickness(0, 50, 300, 0); 
      L.TextAlignment = TextAlignment.Right;
      L.HorizontalAlignment = HorizontalAlignment.Right;
      L.VerticalAlignment = VerticalAlignment.Top;
      L.TextWrapping = TextWrapping.Wrap;
      L.FontFamily = new System.Windows.Media.FontFamily("Verdana");
      L.FontWeight = (FontWeight)new FontWeightConverter().ConvertFromString("Bold");
      L.Text = Label;
      root.Children.Add(L);
      return L;
  }

In the main code:

  TextBox L = AddTextBlock(LayoutRoot, "TestLabel", "L");
  L.OnClick = delegate {  
      Message.Show(L.Text);
  } 

JavaScript

Using <div id=“div1”> … </div> block in HTML we could to add later another HTML-code into it.

Something like:

  Document.GetElementByID("div1").innerHTML = "<h1>Hello world</h1>";

JavaScript is very powerful language. Using it it's very simple to create any powerful webpage. But it's needed to know what is needed and how to do it simpler and multibrowserly…

To be continued…

polyform.txt · Last modified: 2011/09/30 11:21 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki