Hello JZS. Thanks for your reply. I'm using Lazarus version 1.2.4, FPC version 2.6.4, and Windows 7. The listbox is high enough to hold maybe 20 items, but in this particular case I've only put 12 items into it. There are NO blank lines, no gaps between items. (The listbox displays the contents of one of several text files. Some of the files are fairly large and fill the listbox, but others contain only a few lines. Hence the empty space. I suppose I could adjust the Listbox's height to the size of each text file, but that would be very distracting to the user and disrupt the design of the form. The Listbox is the focal point of the form and has various controls around it. I'd like to keep it a constant size.) When I click anywhere in that space, whether it's just below the last item or at the bottom of the box, the ItemIndex returned by GetIndexAtXY is that of the last item (11 in this example), not the -1 that I was expecting.
The other problem is that if I click on, say, the fourth item (ItemIndex = 3) and then click on btnClearSelections, where I specify ItemIndex := -1, the ItemIndex reported in the very next line is 3, NOT -1. It is as if the first line in the Click event handler is simply ignored, except that the highlighting is removed. (If I write "ItemIndex := 2" instead of -1, the third item is selected, as expected. The problem is the -1.)