RadioGroup / ListBox / ComboBox

All three of these objects can be treated the same. I will use rgpChoice in the following examples but the same code will apply to the other two object.

  1. Items are the values that are visible to the user on the object to select from. To get hold of one of these values (items) use sChoice:=rgpChoice.Items[rgpChoice.ItemIndex] instead of using a case statement.

  2. Some teach the above code for ComboBoxes to be sChoice := cbmChoice.Text; This code will only work if the user selected an option. If they have not selected an option the ItemIndex will be -1 and the text property (for example Select an Option) will be stored in sChoice. It is for this reason that I do not teach this method. I always try to teach one method that will work for all solutions and since the RadioGroup does not have this option I teach my learners the code in number 1 for all three objects.

  3. Note that Items will produce a string data type.

  4. Delphi allocates an ItemIndex to each value that you enter in the I"tems property. Remember Delphi starts counting at zero, so item one will have an ItemIndex of 0.

  5. To clear the selection of the user in a Reset button you need to set the ItemIndex to -1 - rgpChoice.ItemIndex := -1;

  6. You can test if the user has selected an option by using an if statement - if rgpChoice.ItemIndex < 0 then. This condition will be true if the user did not select an option.

  7. RadioGroups have a caption, ComboBoxes has a Text property and ListBoxes have neither. These are used to provide the user with what they need to do for example "Select a Grade".

  8. Once you know loops you can make use of code to add items to one of these objects.

Populate items using code