1. Home
  2. Computing & Technology
  3. Delphi Programming
RTL reference|Glossary|Tips/Tricks|FREE App/VCL|Essentials|Books|Link Back
 
Communicating Between Forms
Page 2: Getting the values from another form
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 More of this Feature
• Page 1: Finding out how a modal form was closed
• Page 3: Moving records of data between two forms
 Join the Discussion
"Post your views and comments to this chapter of the free Delphi Programming Course"
Discuss!
 Related Resources
• A Beginner's Guide to Delphi Programming.TOC

• Delphi Project file (dpr)
• Working with forms
• Delphi forms - Life Cycle

Now, knowing whether the user wants to accept or reject what occurred on the child form, the calling form can react by using the information from the child form or ignoring it. Let's see how...

Events to look at for ModalResult (in Forms.pas):

function TCustomForm.ShowModal: Integer;
procedure TCustomForm.CloseModal;
function TCustomForm.CloseQuery: Boolean;
procedure TCustomForm.Close;

ModalResult Constants from Controls.pas

const
  mrNone = 0;
  mrOk = idOk;
  mrCancel = idCancel;
  mrAbort = idAbort;
  mrRetry = idRetry;
  mrIgnore = idIgnore;
  mrYes = idYes;
  mrNo = idNo;
  mrAll = mrNo + 1;
  mrNoToAll = mrAll + 1;
  mrYesToAll = mrNoToAll + 1;

Sometimes you will need to get more than one piece of information back from a form, but let's start simple. The following example shows how to get the value stored in a TEdit (Name : 'Edit1') Text property if the user pressed the Ok button on the child form (Name: 'frmSimpleString').

// Code for main form
procedure TfrmMain.ModalResultdemo2Click(Sender: TObject);
var
  f:TfrmSimpleString ;
begin
  f := TfrmSimpleString.Create(nil);
  try
    if f.ShowModal = mrOk then
    ShowMessage(f.Edit1.Text);
  finally
    f.Release;
  end;
end;

To keep the example short we are just checking for an Ok response and ignoring negative responses. When the user presses the OK button (its ModalResult is set to mrOk) the following code reads the value of an edit control, Edit1, on the called form and then destroys the form. In a real application you might need information from several components on the called form. We could have streamlined the code a little more by adding code to let the user choose whether to continue with closing the form or aborting back into the called form:

// Code for main form
procedure TfrmMain.ModalResultdemo2Click(Sender: TObject);
var
  f:TfrmSimpleString ;
  S:String ;
begin
  f := TfrmSimpleString.Create(nil);
  try
    f.GetThatString(S);
    ShowMessage(s);
  finally
    f.Release;
  end;
end;

 
// Code for the called (child) form

type
TfrmSimpleString = class(TForm)
  Button1: TButton;
  Button2: TButton; 
  Edit1: TEdit;

private
{ Private declarations }
public
{ Public declarations }
  procedure GetThatString(var aString: string);
end;

var
  frmSimpleString: TfrmSimpleString;

implementation

{$R *.DFM}

procedure TfrmSimpleString.GetThatString(var aString: string);
begin 
  case ShowModal of
    mrOk: aString := 'OK' ;
    mrCancel: aString := 'Cancel' ;
  end;
end;

end.

On the called form we add a procedure GetThatString to interrogate the user modally to find out whether the user wants to continue or abort the current operation. For elegance, the Case statement is used rather than a chain of nested IF..THEN..ELSE statements, i.e.

If ShowModal = mrOk then
...
else if ShowModal = mrCancel then
... 
else if ShowModal = mrRetry then
...

Retrieving a string is simple enough but, often, you will need much more than a simple string. Methods for getting a lot of information back from a called form include using an array, a TList, a StringList, or records. The logic used to determine if the user wants to return information remains the same, only the means of storing it varies. To show how this might be done, I will use a record to store user input for retrieval once the user presses OK in the called form. To make things interesting, we will use an array of records so that multiple pieces of information can be returned from the called form!

Before digging into an example, lets go through a short lesson of record types for those programmers that may have not worked with records: "Records in Delphi".

Next page > Moving records of data between two forms > Page 1, 2, 3

A Beginner's Guide to Delphi Programming: Next Chapter >>
>> Creating flat (non-relational) databases with no database components

All graphics (if any) in this feature created by Zarko Gajic.

 More Delphi
· Learn another routine every day - RTL Quick Reference.
· Download free source code applications and components.
· Talk about Delphi Programming, real time.
· Link to the Delphi Programming site from your Web pages.
· Tutorials, articles, tech. tips by date: 2003|2002|2001|2000|1999|1998 or by TOPIC.
 Stay informed with all new and interesting things about Delphi (for free).
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?
Explore Delphi Programming
About.com Special Features

Reader's Choice Award Winners

What are the best instant messengers, apps, editors and more? You told us, for our 2010 technology awards program. More >

iPad Central

Is Apple's new tablet computer impractical, a must-have -- or both? We'll help you figure it out. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2010 About.com, a part of The New York Times Company.

All rights reserved.