Hello World, part three¶
This part relates two classes. A product gets a category, and the product form gets a dropdown to choose it. It assumes that you have done part one and part two.
In this part you will:
- Add a Category class and associate it with Product
- Create the list and the data entry form for categories
- Add the category to the product form as a dropdown
Create the association¶
Open ProductBO. Click the + button at the bottom right of the canvas and choose New Class, as in part two.
- Name the class Category and press Enter.
- Add the attribute Name (string), then click an empty part of the canvas.
To relate the two classes, place the mouse on the border of the Product class until the cursor turns into a crosshair, then drag a line to the Category class. The Association Properties window opens.
Set the rules in words:
- A product has zero or one category. Leave the multiplicity next to Category as 0..1.
- Many products can share the same category. Click the multiplicity indicator next to Product and choose Many. Each indicator sets the rule for the opposite class.
- Check Navigable attribute on the Product side, so that
product.Categorycan be used in forms and logic. - When a product is deleted, the category is kept (Dissociate). A category that still has products cannot be deleted: set On delete for the category to Restricted.
Click Ok and save. The sentence at the bottom of each side restates the rule, so you can check it reads the way you meant. The Associations page describes every option.
Create the Category forms¶
Right-click the Category class and choose Create Master Detail Form, as you did for Product in part two. The Add new item window opens on the Data Entry and List Form template, filled in from the class: CategoryForm, CategoryList, and a menu item called Categories on the master page.
Click Add item. A Created Models window lists the two forms.
Add the category to the product form¶
Open ProductForm and select the Model panel on the right. The view model holds the Product class and its attributes, but not yet the class it is associated with.
Right-click Product and choose Add Related Classes.
The window lists the classes related to Product. Select Category and click Ok.
Category now appears under Product in the Model panel, and can be bound to controls.
Drop it on the form¶
Drag Category from the Model panel onto the form body, below Price. A menu asks which control to create:
- DropDown, a dropdown on its own.
- DropDown with Form Group, a dropdown with a label, like the other fields.
- Panel with… and Form…, which lay out the whole class.
Choose DropDown with Form Group.
Select the dropdown and open Properties. Studio has already filled in the data for you: Model Binding is Product.Category and Dataset is CategoryDataset, the list of categories to choose from.
Click Display & Value Options… to see what the dropdown shows. Display Member is Name, so people see category names. Value Member is $Value, the whole category object, which is what the Product.Category attribute needs.
If the dataset is missing
A form can only fill a dropdown from a dataset on that form. If Dataset is empty, add one from the Category class and its GetAll operation in the DataSets panel. See DataSets.
Build and run¶
Save, validate and build the application. At runtime, the product form has a Category dropdown, and the new Categories menu item opens the category list.
Remember
Remember to Save, Validate and Build the application.









