if (lstbox.ItemIndex = -1) then
ShowMessage('here');
But when I select an Item then This doesn’t work when I click on the’whitespace’ of the list box. How can I detect this?
procedure TMyForm.ListBox1MouseDown(Sender: TObject;
Button : TMouseButton;
Shift: TShiftState;
X, Y: Integer);
begin
if TListbox(Sender).ItemAtPos(Point(X, Y), TRUE) <> -1 then
// item was clicked
else
//'whitespace' was clicked
end;
But this will not affect any OnClick events If you need to perform this test in OnClick, you need to get the mouse position and convert it to list box client coordinates before performing the same test:
procedure TMyForm .ListBox1Click(Sender: TObject);
var
msgMousePos: TSmallPoint;
mousePos: TPoint;
begin
// Obtain screen co-ords of mouse at time of originating message
//
// Note that GetMessagePos returns a TSmallPoint which we need to convert to a TPoint
// in order to make furthe r use of it
msgMousePos := TSmallPoint(GetMessagePos);
mousePos := SmallPointToPoint(msgMousePos);
mousePos := TListbox(Sender).ScreenToClient( mousePos);
if TListbox(Sender).ItemAtPos(mousePos, TRUE) <> -1 then
// item clicked
else
//'whitespace' clicked
end;
Note: GetMessagePos() gets the mouse position when the mouse message was recently observed (in this case, it should be the message that initiated the Click event). However, if you call it directly Click handler, the mouse position returned by GetMessagePos() may have almost nothing to do with the processing in the handler. If any such direct call may reasonably utilize the current mouse position, then this can be obtained using GetCursorPos().
GetCursorPos() can also be used directly, because it directly obtains the mouse position in the TPoint value, avoiding the need to convert from TSmallPoint:
GetCursorPos(mousePos);< /pre>Either way, the fact that your handler depends on the mouse position in any way makes it problematic to call this event handler directly, so if this is a consideration, then you might want to add the event handler Any position-independent response in Isolate into a method that can be clear if/called when needed, and has nothing to do with the interaction between the mouse and the control.
I have a tall list box , Which contains a variable number of items. It is not always full. I know when the user does not select an item through this code:
if (lstbox .ItemIndex = -1) then
ShowMessage('here');
But when I select an Item and click the'whitespace' of the list box, this does not work. How can I Found this situation?
You can do this in a variety of ways. One is to use the X and Y parameters of the event in the OnMouseDown event, which are the list that the user clicks Client coordinates in the box:
procedure TMyForm.ListBox1MouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;< br /> X, Y: Integer);
begin
if TListbox(Sender).ItemAtPos(Point(X, Y), TRUE) <> -1 then
// item was clicked
else
//'whitespace' was clicked
end;
But this will not affect the behavior in any OnClick event. If you need to perform this test in OnClick , You need to get the mouse position and convert it to list box client coordinates before performing the same test:
procedure TMyForm.ListBox1Click(Sender: TObject);
var
msgMousePos: TSmallPoint;
mousePos: TPoint;
begin
// Obtain screen co-ords of mouse at time of originating message
//
// Note that GetMessagePos returns a TSmallPoint which we need to convert to a TPoint
// in order to make further use of it
msgMousePos := TSmallPoint(GetM essagePos);
mousePos := SmallPointToPoint(msgMousePos);
mousePos := TListbox(Sender).ScreenToClient(mousePos);
if TListbox(Sender). ItemAtPos(mousePos, TRUE) <> -1 then
// item clicked
else
//'whitespace' clicked
end;
Note: GetMessagePos () Get the mouse position when the mouse message was recently observed (in this case, it should be the message that initiated the Click event). However, if the Click handler is called directly, the mouse position returned by GetMessagePos() may be the same as in the handler The processing is almost irrelevant. If any such direct call may reasonably use the current mouse position, then this can be obtained using GetCursorPos().
GetCursorPos() can also be used directly, because it directly obtains the TPoint value The mouse position in, avoiding the need to convert from TSmallPoint:
GetCursorPos(mousePos);
Either way, your processing program will be any The fact that the way depends on the mouse position makes it problematic to call this event handler directly, so if this is a consideration, then you may want to isolate any position-independent responses in the event handler into a method that can be definitive if/when needed Called when, and has nothing to do with the interaction between the mouse and the control.