Recent

Author Topic: I just want to find the item in focus in a list box.  (Read 2442 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
I just want to find the item in focus in a list box.
« on: December 24, 2018, 06:02:17 am »
Code: Pascal  [Select][+][-]
  1. if ListBox7.ItemIndex > -1 then  begin
  2.     Issue := ListBox7.Items(ListBox7.ItemIndex);
  3.    end;    

The above code dosen't work along with  several variation. I know I could go through the entire listbox but it has 6,000 plus items.

I can't find the answer because of improper search criteria. Any help out there?
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: I just want to find the item in focus in a list box.
« Reply #1 on: December 24, 2018, 06:42:51 am »
hello,
try to use getSelectedText:
Code: Pascal  [Select][+][-]
  1.  if ListBox7.ItemIndex > -1 then  begin
  2.     NotIssue := ListBox7.getSelectedText;
  3.    end;

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

dsiders

  • Hero Member
  • *****
  • Posts: 1084
Re: I just want to find the item in focus in a list box.
« Reply #2 on: December 24, 2018, 06:43:28 am »
Code: Pascal  [Select][+][-]
  1.   if ListBox7.ItemIndex > -1 then  begin
  2.     Issue := ListBox7.Items(ListBox7.ItemIndex);
  3.   end;
  4.  

The above code dosen't work along with  several variation. I know I could go through the entire listbox but it has 6,000 plus items.

I can't find the answer because of improper search criteria. Any help out there?

Try using TListBox.GetSelectedText. That way you don't have to account for TopIndex in a ListBox that's scrollable.

JP beat me to it...
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: I just want to find the item in focus in a list box.
« Reply #3 on: December 24, 2018, 06:52:40 am »
ItemIndex should be the item which has the focus. try this code (or similar) to see if it works:
Code: Pascal  [Select][+][-]
  1. function GetCurrentItem(AListBox: TListBox): String;
  2. var
  3.   idx: Integer;
  4. begin
  5.   { The important thing: use an intermediary to
  6.     isolate calls to the list box methods }
  7.   idx := AListBox,ItemIndex;
  8.   if idx <> -1 then
  9.     Result := AListBox.Items[idx];
  10.   else
  11.     Result := '';
  12. end;

Alternatively, if you have MultiSelect set to False, you can use AListBox.GetSelectedText, which does esentially the same but internally.

If nothing works, please, be more specific: what does it do which is wrong? which steps to reproduce the fail? which OS and which version of Lazarus/FPC? (this last is in your signature, I guess)

ETA: Damn, you guys are quick! :)
« Last Edit: December 24, 2018, 06:57:37 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: I just want to find the item in focus in a list box.
« Reply #4 on: December 24, 2018, 07:05:11 am »
Code: Pascal  [Select][+][-]
  1.     function GetCurrentItem(AListBox: TListBox): String;
  2.     var
  3.       idx: Integer;
  4.     begin
  5.       { The important thing: use an intermediary to
  6.         isolate calls to the list box methods }
  7.       idx := AListBox,ItemIndex;
  8.       if idx <> -1 then
  9.         Result := AListBox.Items[idx];
  10.       else
  11.         Result := '';
  12.     end;

@Lucmar
I think it needs a comma changed to a period.
      idx := AListBox.ItemIndex;
Will it exit after the line  Result := AListBox.Items[idx]; ? I assume.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: I just want to find the item in focus in a list box.
« Reply #5 on: December 24, 2018, 08:04:18 am »
Oops! Sorry. Corrected version:
Code: Pascal  [Select][+][-]
  1. function GetCurrentItem(AListBox: TListBox): String;
  2. var
  3.   idx: Integer;
  4. begin
  5.   { The important thing: use an intermediary to
  6.     isolate calls to the list box methods }
  7.   idx := AListBox.ItemIndex;
  8.   if idx <> -1 then
  9.     Result := AListBox.Items[idx]
  10.   else
  11.     Result := '';
  12. end;

I'd just gotten out of bed  :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: I just want to find the item in focus in a list box.
« Reply #6 on: December 24, 2018, 06:12:38 pm »
Code: Pascal  [Select][+][-]
  1.   if idx <> -1 then
  2.     Result := AListBox.Items[idx]
  3.   else
  4.     Result := '';
  5. end;

I see the answer to my second question. I either get a listbox Item that is in focus or an empty string.

Simple but very elegant. Copied this out to my listbox commands and routines doc.

Thanks 
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

jamie

  • Hero Member
  • *****
  • Posts: 6131
Re: I just want to find the item in focus in a list box.
« Reply #7 on: December 24, 2018, 09:04:04 pm »
regardless, using the ItemIndex as the value for the INDEX for the items, should work..

The only way it may not work is if a selection changed and the control wasn't allowed to update.

But even that is a stretch.

I haven't looked at the code recently for the Listbox but if the itemIndex isn't getting updated when a
direct change is being made and thus waiting for a message to arrive from the widget then this could be
an issue.

 If you are not allowing the system to process messages after a change is made to the list box, this may
happen...


The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018