Recent

Author Topic: Listbox not highlighting item 0  (Read 4260 times)

William Marshall

  • Jr. Member
  • **
  • Posts: 52
Listbox not highlighting item 0
« on: October 21, 2014, 06:59:34 pm »
 My program shows a Listbox on startup.  If I put "Listbox1.ItemIndex := 1;" (or any other number) in the FormCreate handler, the appropriate item is highlighted.  But if I have "Listbox1.ItemIndex := 0;" (as I would like to have), there is no highlighting.  Why doesn't an index of zero work?
Lazarus 1.8.0; fpc 3.0.4; Windows 10

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Listbox not highlighting item 0
« Reply #1 on: October 21, 2014, 07:51:05 pm »
Setting a listbox's ItemIndex to zero selects the first item (provided there is a first item).
Try this little example. In a new empty project replace the main form unit's code with the following, compile and run.
Perhaps it will help you to figure out what is amiss with your code.

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, Forms, StdCtrls, ExtCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    lb: TListBox;
    rg: TRadioGroup;
    procedure RGClick(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  Width:=380;
  Height:=140;

  lb:=TListBox.Create(Self);
  lb.SetBounds(10, 10, 80, 120);
  lb.Items.CommaText:='one,two,three';
  lb.Parent:=Self;

  rg:=TRadioGroup.Create(Self);
  rg.SetBounds(100, 10, 270, 120);
  rg.Items.CommaText:='-1,0,1,2';
  rg.Caption:='Click to select corresponding listbox ItemIndex';
  rg.OnClick:=@RGClick;
  rg.Parent:=Self;
end;

procedure TForm1.RGClick(Sender: TObject);
begin
  lb.ItemIndex:=rg.ItemIndex - 1;
end;

end.


William Marshall

  • Jr. Member
  • **
  • Posts: 52
Re: Listbox not highlighting item 0
« Reply #2 on: October 21, 2014, 09:13:50 pm »
Thanks, howardpc.  But my problem is this: the Listbox contains, say 9 items.  Writing "Listbox1.ItemIndex := 3;" selects the fourth item.  Fine.  So "Listbox1.ItemIndex := 0;" should select the first (top) item.  But it doesn't.  NO item is selected.
 
I just realized that's not quite true.  When the form opens, no item is highlighted, but pressing the down arrow key highlights the second item.  So the initial ItemIndex was in fact 0.  But there was no highlighting on the first item.  (Pressing the up arrow key now does highlight that first item.)  So the problem really is that - at startup - the first item (#0) is selected, but is not highlighted.  Specifying any number but zero works as expected.  How can I highlight that top item when the list first appears?  (Maybe I could press the down and up arrow keys in code somehow, but I don't know how to do that.  Still, that seems more a workaround than a solution.)
I know this is just an annoying problem, maybe even trivial (it doesn't affect how the program works).  But I'd still like to get it to work.
Lazarus 1.8.0; fpc 3.0.4; Windows 10

zeljko

  • Hero Member
  • *****
  • Posts: 1594
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Listbox not highlighting item 0
« Reply #3 on: October 21, 2014, 09:29:40 pm »
widgetset and OS ?

Never

  • Sr. Member
  • ****
  • Posts: 409
  • OS:Win7 64bit / Lazarus 1.4
Re: Listbox not highlighting item 0
« Reply #4 on: October 21, 2014, 10:02:13 pm »
@William Marshall
 you have the property multyselect to true set it to false
set the indexitem and reset to true this will solve the issue
Also if you do not want to modify the property for some reason
this
Code: [Select]
   ListBox1.Selected[0]:=True;will do the job
« Last Edit: October 21, 2014, 10:49:17 pm by Never »
Νέπε Λάζαρε λάγγεψων οξωκά ο φίλοσ'ς αραεύσε

William Marshall

  • Jr. Member
  • **
  • Posts: 52
Re: Listbox not highlighting item 0
« Reply #5 on: October 22, 2014, 02:59:43 am »
Never: Yes, MultiSelect is true.  Setting it to false gives the highlighting on the first item, but resetting it to true takes the highlighting away again.  But Listbox1.Selected[0] := true did the trick!  It also solves a few other problems I've been having with ItemIndex.  Thank you.
Lazarus 1.8.0; fpc 3.0.4; Windows 10

Never

  • Sr. Member
  • ****
  • Posts: 409
  • OS:Win7 64bit / Lazarus 1.4
Re: Listbox not highlighting item 0
« Reply #6 on: October 22, 2014, 11:36:56 am »
Glad to help @William Marshall
Code: [Select]

ListBox1.Multyselect:=False;
ListBox1.ItemIndex :=0;
ListBox1.Multyselect:=True;
Just re test it is working in my enviroment
Edit:*** it should work at yours also as long you are not using it inside the create event handler
« Last Edit: October 22, 2014, 11:46:22 am by Never »
Νέπε Λάζαρε λάγγεψων οξωκά ο φίλοσ'ς αραεύσε

 

TinyPortal © 2005-2018