Question

I have two Listbox on a form that are populated at runtime.

I have an OnClick event that changes the color (plus a bunch of other things) of the "Selected" listbox so that I know which one is active for the user.

It all works just fine if the user clicks an Item within a listBox - but - if they click in the white-space within the listbox, no OnClick is fired.

I thought of using the OnMouseUp and pointing to the OnClick event but the other things within that event are not wanted at that time.

How can I detect a Click within the white space of a ListBox?

Était-ce utile?

La solution 2

Hum... Listbox.ItemIndex = -1 means that you click in the empty area. With the mouseevent, it should do the trick.

Another option : Maybe you could try to check the component having the focus, instead of using the click event.

EDIT : Not having a Delphi5 available, i can tell you that this 'bug' is fixed in XE. Set a Timer and check which listbox has the focus (Listbox.Focused), if hopefully this property is available.

Autres conseils

It seems that what you are actually looking for is to detect a change of focus. You want to detect when a control gets focus, and then when it loses focus. For that you should use the OnEnter and OnExit events. The OnClick event is simply the wrong event for the task.

From the documentation for OnEnter:

Use the OnEnter event handler to cause any special processing to occur when a control becomes active.

The OnEnter event does not occur when switching between forms or between another application and the application that includes the control.

When switching between controls in separate container controls such as the TPanel and the TGroupBox controls, an OnEnter event occurs for the container before the OnEnter event of the contained control.

Similarly, an OnExit event of the container occurs after the OnExit event of the control in a container when focus moves to another control outside the container.

For example, consider a form with an OK button and a group box that contains three radio buttons, where focus is currently on the OK button. When the user clicks one of the radio buttons, an OnExit event of the button occurs, followed by an OnEnter event on the group box, and finally an OnEnter event on the radio button that was clicked. If the user then clicks on the OK button, an OnExit event for the radio button occurs followed by an OnExit event for the group box, and then the button's OnEnter event occurs.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top