Passing Data

Last Updated: 9/19/2008



Sections above here:
Home  »  Development  »  Form Management

Sections below here:

    Topics in this section:
    Form Management
    User Controls
    IntraWeb Frame
    Managing Forms
    Form List
    Showing Forms
    Hiding Forms
    Destroying Forms
    Passing Data

    Search Documentation:

    Data can be passed between form just like in any normal application. Since forms are persistent information can be stored in member variables of form classes.

    For demonstration purposes we will build a simple demo with two forms. The main form will contain a button and an edit box. The other form contains a memo field and a label. When the user presses the button on the main form the text from the edit box will be added to the memo on the second form and the form will be displayed. The second form will also display how many times it has been displayed and allow the user to return to the main form.

    Complete Demo

    To see the project in action, please see the FormData.dpr project in the Demos directory of IntraWeb for Delphi, or the FormData.sln in the Demos directory of IntraWeb for Visual Studio .NET. For the Visual Studio .NET version, the demo is available in both C# and VB.NET languages. 

    Delphi example

    This is how the main form looks like at design time:

    Main Form – Source Code:

    unit Main;
    {PUBDIST}

    interface

    uses
    IWAppForm, IWApplication, IWTypes, IWCompButton, IWCompEdit, Classes,
    Controls, IWControl, IWCompLabel, Dialog, IWHTMLControls;

    type
    TformMain = class(TIWAppForm)
    IWLabel1: TIWLabel;
    editText: TIWEdit;
    butnOk: TIWButton;
    IWLink1: TIWLink;
    procedure butnOkClick(Sender: TObject);
    procedure IWAppFormCreate(Sender: TObject);
    procedure IWLink1Click(Sender: TObject);
    public
    FDialogForm: TformDialog;
    end;

    implementation
    {$R *.dfm}

    uses
    SysUtils;

    procedure TformMain.butnOkClick(Sender: TObject);
    var
    s: string;
    begin
    s := Trim(editText.Text);
    editText.Text := '';
    if s = '' then begin
    WebApplication.ShowMessage('Please enter some text.');
    end else begin
    with FDialogForm do begin
    IWMemo1.Lines.Add(s);
    Inc(FCount);
    Show;
    end;
    end;
    end;

    procedure TformMain.IWAppFormCreate(Sender: TObject);
    begin
    FDialogForm := TformDialog.Create(WebApplication);
    end;

    procedure TformMain.IWLink1Click(Sender: TObject);
    begin
    WebApplication.Terminate('Good bye!');
    end;

    end.

    IWLink1 OnClick

    This event is hooked to the link with the caption "Quit" and simply terminates the session when the user clicks the link.

    OnCreate

    The OnCreate event is called when the form is created. In this event another form is created and the reference to it is stored as a member variable of  this form so it can be accessed again later.

    butnOk.OnClick

    In the OnClick event the edit box is checked for data. If no data exists WebApplication.ShowMessage is called to display a message to the user. After the message is dismissed the main form is shown again.

    If the user did enter data, using FDialogForm (which was created in this form's OnCreate)  is used. Data is added to the memo, and a member variable of TFormDialog is updated. It is then displayed using the .Show method. As you can see, data is very easy to pass between forms and is the same as in a normal Delphi application.

    Visual Studio .NET example

    The forms are called: FormMain and FormDialog

    FormMain - Design time


    FormMain - C# source code:

    using System; 
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Data;
    using Atozed.IntraWeb.NETMain;
    using Atozed.IntraWeb.AppFormUnit;

    namespace FormData
    {

    public class FormMain : AppForm
    {
    private Atozed.IntraWeb.CompLabel.TextLabel tiwLabel1;
    private Atozed.IntraWeb.CompEdit.Edit editText;
    private Atozed.IntraWeb.CompButton.Button btnOk;
    private Atozed.IntraWeb.HTMLControls.Link lnkQuit;
    private System.ComponentModel.Container components = null;
    private FormDialog formDialog = new FormDialog();

    public FormMain()
    {
    // This call is required by the Windows Form Designer.
    InitializeComponent();

    // TODO: Add any initialization after the InitializeComponent call
    }

    ///

    /// Clean up any resources being used. ///

    protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose();
    } } base.Dispose(disposing); } #region Designer generated code ///

    /// Required method for Designer support - do not modify /// the contents of this method with the code
    editor. ///

    private void InitializeComponent() { } #endregion #region Application entry point ///

    /// The main entry point for the application. ///

    [STAThread] static void Main() { IWServerController.SetServerControllerClass(typeof(IWServerController));
    new IWLicenseKey(); FormMain.SetAsMainForm(typeof(FormMain)); Application.Run(new Main()); } #endregion
    private void btnOk_OnClick(object sender, System.EventArgs e) { string s = ""; if (editText.Text != null)
    { s = editText.Text.Trim(); } if ("" == s) { WebApplication.ShowMessage("Please enter some text."); }
    else { formDialog.GetMemoControl().Lines.Add(s); formDialog.Count += 1; formDialog.Show(); } } private
    void lnkQuit_OnClick(object sender, System.EventArgs e) { WebApplication.Terminate("Good bye!"); } } }


    FormMain - VB.NET source code

    Imports System 
    Imports System.Collections
    Imports System.ComponentModel
    Imports System.Drawing
    Imports System.Windows.Forms
    Imports System.Data
    Imports Atozed.IntraWeb.NETMain
    Imports Atozed.IntraWeb.AppFormUnit

    Public Class FormMain
    Inherits AppForm
    Private WithEvents TiwLabel1 As Atozed.IntraWeb.CompLabel.TextLabel
    Private WithEvents editText As Atozed.IntraWeb.CompEdit.Edit
    Private components As System.ComponentModel.Container = Nothing
    Private frmDialog As FormDialog = New FormDialog

    Public Sub New()
    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' TODO: Add any initialization after the InitializeComponent call
    End Sub

    '
    ' Clean up any resources being used.
    '

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
    If Not components Is Nothing Then
    components.Dispose()
    End If
    End If
    MyBase.Dispose(disposing)
    End Sub

    #Region "Designer generated code"
    '
    ' Required method for Designer support - do not modify
    ' the contents of this method with the code editor.
    '

    Private WithEvents btnOk As Atozed.IntraWeb.CompButton.Button
    Private WithEvents lnkQuit As Atozed.IntraWeb.HTMLControls.Link
    Private Sub InitializeComponent()

    End Sub
    #End Region

    #Region "Application entry point"
    '///
    '/// The main entry point for the application.
    '///

    _
    Shared Sub Main()
    Dim LLicense As IWLicenseKey = new IWLicenseKey()
    IWServerController.SetServerControllerClass(System.Type.GetType("FormData.IWServerController"))
    FormMain.SetAsMainForm(System.Type.GetType("FormData.FormMain"))
    Application.Run(new Main())
    End Sub
    #End Region

    Private Sub lnkQuit_OnClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkQuit.OnClick
    WebApplication.Terminate("Good bye!")
    End Sub

    Private Sub btnOk_OnClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.OnClick
    Dim s As String = ""
    If (editText.Text <> Nothing) Then
    s = editText.Text.Trim()
    End If
    If ("" = s) Then
    WebApplication.ShowMessage("Please enter some text.")
    Else
    frmDialog.GetMemoControl().Lines.Add(s)
    frmDialog.Count = frmDialog.Count + 1
    frmDialog.Show()
    End If
    End Sub
    End Class

    Form this example the code generated by the form designer in the InitializeComponent method was removed.  

    btnOk_OnClick

    In the OnClick event the edit box is checked for data. If no data exists WebApplication.ShowMessage is called to display a message to the user. After the message is dismissed the main form is shown again. If the user did enter data, using FDialogForm (which was created in this form's OnCreate) is used. Data is added to the memo, and a member variable of TFormDialog is updated. It is then displayed using the .Show method. As you can see, data is very easy to pass between forms and is the same as in a normal Delphi application.

    lnkQuit_OnClick

    This event terminates the session.


    Note for C++ Builder users

    Due to the way forms work in C++ Builder, the best place to initialize all automatic class instances is inside the form constructor rather than within the OnCreate event handler.

    This will avoid the problem of instances being created twice, problem that arises because of the creation order of the form elements in C++ Builder. In C++ Builder the OnCreate event handler is called before the form constructor, and the OnDestroy event handler is called after the form destructor.

    To avoid such problems, please move all the automatic class instances initialization in the form constructor, and keep in the OnCreate event handler only the dynamically allocated instances. 



    (C) 2002-2009 - Atozed Software Ltd.