Create and Use a Dialog Box

Create a Dialog Box

Select new from the File Menu and from the New Items dialog, Dialogs tag select the first Dialog With Help.


You have created a new form that has the form of a dialog(below). This dialog must be named and its caption set in the Object Inspector. You can now add any of the features of a window. For an example click here.


For our simple example we will create a new grocery item from data collected by our dialog. We need to gather the Name, Id, Unit Cost and Number of Items. We will use the standard Edit box to get text. To insert an edit box just select the textbox icon and drop it on the form. Next shape and position as desired. Rename it in the Object Inspector and delete Text property (empty string). I will use a group box to label the text box. First I select the group box icon . Then I place it over the edit box. The edit box disappears, so I select the right-button menu, and choose send to the back. The group box is now behind the edit box. Change the caption to reflect the contents of the edit box. Most of what we need is found under the standard tab of the component bar.

After adding the edit boxes for each item we need the dialog looks like this.

Using a Dialog Box

Builder causes any form created in a project to be created automatically when the program runs. This can be changed by the programmer by selecting the Options choice from the Project Menu. The Forms Tab in the Options dialog lists all forms that are autocreate (the default). If you want to allocate memory for your own dialog you must select the dialog and press the right pointing single arrow. From this point on you are responsible for creating and deleting the dialog.

In this example, the add grocery item was chosen. Each text box has a Text property that can be set or dereferenced. First set each to empty, then call on the new grocery dialog to show itself by calling ShowModal. Usually we check to see if the ok button was pressed before continuing the process. Once the date is retrieved it can be converted from String type to the appropriate type and in this case a new grocery item is created. Paint redraws the listbox after an item has been added to the list.

Click here to see a video about creating a menu handler.
void __fastcall TSDIAppForm::AddItem1Click(TObject *Sender)
{
    NewGroceryItem->Description->Text ="";
    NewGroceryItem->Id->Text ="";
    NewGroceryItem->CostPerItem->Text ="";
    NewGroceryItem->Quantity->Text ="";

    if(NewGroceryItem->ShowModal()==IDOK)
    {
        double cost = NewGroceryItem->CostPerItem->Text.ToDouble();
        int quantity =NewGroceryItem->Quantity->Text.ToInt();;
        GroceryItem g(NewGroceryItem->Description->Text, NewGroceryItem->Id->Text,cost,quantity);
        _groceries.addItem(g);
        paint();
    }
}

When you try to run this you will probably get an error that the program doesn't know what NewGroceryItem is. You need to include the item. This is fairly simple. Just select Include Unit Hdr... from the File menu. A dialog will appear that lists all headers not already included in the main. Just select the header for NewGroceryItem and press the OK button.

Allocating Dialogs

Dialogs used infrequently in a large program should not be autocreated since they take a lot of room. Once you have moved a dialog out of the autocreate list, you must create the dialog before using it and delete it after using it. First notice the type of the dialog box is the name you gave the box with T appended to the front. All you need to do is allocate the box using new and delete using delete after you are done with it.

void __fastcall TSDIAppForm::AddItem1Click(TObject *Sender)
{
    TNewGroceryItem *NewGroceryItem = new TNewGroceryItem(this);
    NewGroceryItem->Description->Text ="";
    NewGroceryItem->Id->Text ="";
    NewGroceryItem->CostPerItem->Text ="";
    NewGroceryItem->Quantity->Text ="";

    if(NewGroceryItem->ShowModal()==IDOK)
    {
        double cost = NewGroceryItem->CostPerItem->Text.ToDouble();
        int quantity =NewGroceryItem->Quantity->Text.ToInt();;
        GroceryItem g(NewGroceryItem->Description->Text, NewGroceryItem->Id->Text,cost,quantity);
        _groceries.addItem(g);
        paint();
    }
   delete NewGroceryItem;
}