Mean Program
The design of a Windows program has to contend with two distinct aspects:
- The interface - the bit the user interacts with. Contains the components to activate event handlers and handles the input and output.
- The engine - the bit the user can't see. The actual code that does the work in controlling the input/output and the processing of event handlers etc.
The program has previously been written as a console application using one complete block of code:
int main(int argc, char* argv[])
{
int Count;
int Total;
int NextNumber;
float Mean;
Count = 0;
Total = 0;
NextNumber = 0;
while (NextNumber != -1)
{
NextNumber = ReadIntPr("Enter next number or -1 to stop: ");
if (NextNumber != -1)
{
Total = Total + NextNumber;
Count = Count + 1;
}
}
Mean = float(Total)/Count;
WriteFloatPr("The mean value is: ", Mean);
getchar();
return 0;
}
This block must now be broken into segments around the differing event handlers.
Interface Design
A simplified design is given here. The components used on the from will have a TabOrder; the order in which the focus of the program selects the component. A component with a TabOrder of 0 will have the cursor set to appear at that component at the start of the program.
The initial interface is the result of planning the layout of the form and its components including initial properties of the components.
The run-time interface will involve identifying the components of the form that require to change as a result of event handlers.
Interface for Mean Value Program
The code is broken into the following event handlers. The variables are set by the FormCreate with input controlled by the OKButtonClick event handler. The processing of the mean value and its output is controlled by the DoneButtonClick event handler.
void __fastcall TMVForm::OKButtonClick(TObject *Sender)
{
int NextNumber;
NextNumber = InBox->Text.ToInt();
Total = Total + NextNumber;
Count = Count + 1;
InBox->Clear();
InBox->SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TMVForm::DoneButtonClick(TObject *Sender)
{
float Mean;
Mean = float(Total)/Count;
OutBox->Visible = true;
OutBox->Text = "The mean value is " + AnsiString(Mean);
InBox->Enabled = false;
OKButton->Enabled = false;
DoneButton->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TMVForm::FormCreate(TObject *Sender)
{
Total = 0;
Count = 0;
}
Comments, suggestions, ideas to
Stuart Banner
