We want our Delete button to delete whatever item the user has selected in the list.

Let's go back to our Window Layout script and modify our WM_CLICK_NameDelete subroutine. We first need to check what item, if any, the user has selected in the list. Then if he has selected an item, we need to delete it from the list.

This requires us to actually write some REXX instructions. (What? You thought that Programmer Center was going to write your entire script for you? Talk about lazy!).

To get the current value of some control in our window, we call the function GuiGetCtlValue passing it the variable name we associated with the control. Remember that for the list control, we chose NameList. Reginald will set the associated variable to the control's value. For a list control, that means the currently selected string. Or if no string is selected, then Reginald DROPs the variable. So to get the string that the user selected in the list box, let's add the following instructions to WM_CLICK_NameDelete:

WM_CLICK_NameDelete:

   /* Get the list's selected string. Note: We quote the variable name. */
   GuiGetCtlValue("NameList")

   /* Check if the user actually selected something. */
   IF EXISTS("NameList") THEN

      /* Display the string. */
      GuiSay(NameList)

   ELSE
      GuiSay("Nothing selected")

   RETURN
If you run the script, select either Johnny or Suzy in the list, and then click the Delete button, you should see the correct selection displayed. If you run the script, and do not select any name in the list before you click the Delete button, you'll see "Nothing selected" displayed.

Note: For more information about what Gui functions can be used with a particular type of control, see that control's description under "Controls Reference".

Now we need to delete the string from the list box. We can do this with a call to GuiRemoveCtlText. We pass the REXX variable name associated with the list, and also pass the string to delete. So here is our completed subroutine:

WM_CLICK_NameDelete:

   /* Get the list's selected string. Note: We quote the variable name. */
   GuiGetCtlValue("NameList")

   /* Check if the user actually selected something. */
   IF EXISTS("NameList") THEN

      /* Delete this string. */
      GuiRemoveCtlText("NameList", NameList)

   RETURN
Actually, passing an empty string causes GuiRemoveCtlText to delete the currently selected item, so we can reduce our code to:
WM_CLICK_NameDelete:

   /* Delete the currently selected string. */
   GuiRemoveCtlText("NameList", "")

   RETURN
After making the above change to the script, let's try running it (via the Run -> Execute script menu command). Click on Johnny's name to highlight it in the list box, and then click on the Delete button.

Once again, you can close the "List of Names" window to end the script.