RAD Studio
ContentsIndex
PreviousUpNext
Retrieving Data from Modeless Forms

You can easily extract information from modeless forms by calling public member functions of the form or by querying properties of the form. For example, assume an application contains a modeless form called ColorForm that contains a listbox called ColorListBox with a list of colors ("Red," "Green," "Blue," and so on). The selected color name string in ColorListBox is automatically stored in a property called CurrentColor each time a user selects a new color. The class declaration for the form is as follows:

TColorForm = class(TForm)
  ColorListBox:TListBox;
  procedure ColorListBoxClick(Sender: TObject);
private
  FColor:String;
public
  property CurColor:String read FColor write FColor;
end;

 

class TColorForm : public TForm
{
__published:    // IDE-managed Components
TListBox *ColorListBox;
void __fastcall ColorListBoxClick(TObject *Sender);
private:        // User declarations
String getColor();
void   setColor(String);
String curColor;
public:         // User declarations
virtual __fastcall TColorForm(TComponent* Owner);
__property String CurrentColor = {read=getColor, write=setColor};
};

The OnClick event handler for the listbox, ColorListBoxClick, sets the value of the CurrentColor property each time a new item in the listbox is selected. The event handler gets the string from the listbox containing the color name and assigns it to CurrentColor. The CurrentColor property uses the setter function, SetColor, to store the actual value for the property in the private data member FColor:

procedure TColorForm.ColorListBoxClick(Sender: TObject);
var
  Index: Integer;
begin
  Index := ColorListBox.ItemIndex;
  if Index >= 0 then
    CurrentColor := ColorListBox.Items[Index]
  else
    CurrentColor := '';
end;

 

void __fastcall TColorForm::ColorListBoxClick(TObject *Sender)
{
int index = ColorListBox->ItemIndex;
if (index >= 0) {
CurrentColor = ColorListBox->Items->Strings[index];
}
else
CurrentColor = "";
}
//---------------------------------------------------------------------
void TColorForm::setColor(String s)
{
curColor = s;
}

Now suppose that another form within the application, called ResultsForm, needs to find out which color is currently selected on ColorForm whenever a button (called UpdateButton) on ResultsForm is clicked. The OnClick event handler for UpdateButton might look like this:

procedure TResultForm.UpdateButtonClick(Sender: TObject);
var
  MainColor: String;
begin
  if Assigned(ColorForm) then
  begin
    MainColor := ColorForm.CurrentColor;
    {do something with the string MainColor}
  end;
end;

 

void __fastcall TResultsForm::UpdateButtonClick(TObject *Sender)
{
if (ColorForm) {
String s = ColorForm->CurrentColor;
// do something with the color name string
}
}

The event handler first verifies that ColorForm exists using the Assigned function. It then gets the value of ColorForm's CurrentColor property. 

Alternatively, if ColorForm had a public function named GetColor, another form could get the current color without using the CurrentColor property (for example, MainColor := ColorForm.GetColor;). In fact, there's nothing to prevent another form from getting the ColorForm's currently selected color by checking the listbox selection directly:

with ColorForm.ColorListBox do
  MainColor := Items[ItemIndex];

 

String TColorForm::getColor()
{
return curColor;
}

 

String s = ColorListBox->Items->Strings[ColorListBox->ItemIndex];

However, using a property makes the interface to ColorForm very straightforward and simple. All a form needs to know about ColorForm is to check the value of CurrentColor.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!