Recent

Author Topic: TListbox: makeCurrentVisible does not work  (Read 3218 times)

malabarista

  • Newbie
  • Posts: 4
TListbox: makeCurrentVisible does not work
« on: March 25, 2017, 11:46:37 am »
I have a modal form with one Tedit and two TListboxes.
After I set itemindex to nearly then end of the listbox MakeCurrentVisible does not work: the item is marked but the listbox is not scrolled.

Any ideas ?

Here is the code:
Code: Pascal  [Select][+][-]
  1. unit testlboxu;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   modalu;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     procedure destroy0(Sender: TObject);
  18.     procedure modaltest(Sender: TObject);
  19.   private
  20.     { private declarations }
  21.   public
  22.     { public declarations }
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.modaltest(Sender: TObject);
  35.  
  36. var
  37.   ruck:integer;
  38.  
  39. begin
  40.   If Not Assigned(f2_v) then
  41.      begin
  42.      f2_v:=TForm2.Create(Self);
  43.      end;
  44. f2_v.init(800,400);
  45. f2_v.start(10,25);
  46. ruck:=f2_v.Showmodal;
  47. if ruck=mrOK then
  48.    begin
  49.    //fertig
  50.  
  51.    end;
  52. end;
  53.  
  54. procedure TForm1.destroy0(Sender: TObject);
  55. begin
  56.     If Assigned(f2_v) then f2_v.free;
  57. end;
  58.  
  59. end.
  60.  
modalu.pas:
Code: Pascal  [Select][+][-]
  1. unit modalu;
  2. {$mode objfpc}{$H+}
  3.  
  4. interface
  5.  
  6. uses
  7.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  8.  
  9. type
  10.  
  11.   { TForm2 }
  12.  
  13.   TForm2 = class(TForm)
  14.     Button1: TButton;
  15.     Button2: TButton;
  16.     Edit1: TEdit;
  17.     box1: TListBox;
  18.     box2: TListBox;
  19.     procedure abbruch(Sender: TObject);
  20.     procedure close0(Sender: TObject; var CloseAction: TCloseAction);
  21.     procedure speichern(Sender: TObject);
  22.   private
  23.     { private declarations }
  24.   public
  25.     { public declarations }
  26.     procedure init(b,h:integer);
  27.     procedure start(i1,i2:integer);
  28.   end;
  29.  
  30. var
  31.   f2_v: TForm2;
  32.  
  33. implementation
  34.  
  35. {$R *.lfm}
  36.  
  37. { TForm2 }
  38. var
  39.   ok:integer;
  40.  
  41. procedure TForm2.init(b,h:integer);
  42. begin
  43. ok:=0;
  44. width:=b;
  45. height:=h;
  46. end;
  47.  
  48. procedure TForm2.start(i1,i2:integer);
  49. begin
  50. ok:=0;
  51. box1.itemindex:=i1;
  52. box1.MakeCurrentVisible;
  53. box2.itemindex:=i2;
  54. box2.MakeCurrentVisible;
  55. end;
  56.  
  57. procedure TForm2.close0(Sender: TObject; var CloseAction: TCloseAction);
  58. begin
  59.   ModalResult:=mrCancel;
  60.   if ok>0 then ModalResult:=mrOk;
  61. end;
  62.  
  63. procedure TForm2.abbruch(Sender: TObject);
  64. begin
  65.   ok:=0;
  66.   close;
  67. end;
  68.  
  69. procedure TForm2.speichern(Sender: TObject);
  70. begin
  71.   ok:=1;
  72.   close;
  73. end;
  74.  
  75. end.
  76.  

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: TListbox: makeCurrentVisible does not work
« Reply #1 on: March 25, 2017, 12:47:20 pm »
I can't reproduce this with a simple example:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ToggleBox1Change(Sender: TObject);
  2. begin
  3.  Listbox1.ItemIndex:= 998;
  4.  Listbox1.MakeCurrentVisible;
  5.  Listbox2.ItemIndex:= 999;
  6.  Listbox2.MakeCurrentVisible;
  7. end;
  8.  
  9. procedure TForm1.FormCreate(Sender: TObject);
  10. var i:integer;
  11. begin
  12.   for i:=0 to 999 do
  13.   begin
  14.     Listbox1.Items.Add(inttostr(i));
  15.     Listbox2.Items.Add(inttostr(i));
  16.   end;
  17. end;
     
Specialize a type, not a var.

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: TListbox: makeCurrentVisible does not work
« Reply #2 on: March 25, 2017, 12:52:25 pm »
In the attachment there's another project which you directly can run to show you that your error cannot be reproduced. The error can be at so many places, not just in the two code fragments that you post. Please remember: always try to write a complete compilable demo which shows the issue. Pack the pas, lfm, lpi and lpr files into a common zip and upload it here. Also, specifiy your operating system and Lazarus/fpc version, TListbox highly depends on widgetset, therefore the error will not show up in the environment of the reader of your post.

malabarista

  • Newbie
  • Posts: 4
Re: TListbox: makeCurrentVisible does not work
« Reply #3 on: March 25, 2017, 05:45:28 pm »
I think the problem is the modalform. If you modify the call to .show instead of .showmodal it works.

Included is the complete project.
The system is Linux mint 18.1 64-bit

Lazarus 1.6.2

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: TListbox: makeCurrentVisible does not work
« Reply #4 on: March 25, 2017, 06:33:34 pm »
If you put the box1.MakeCurrentVisible and box2.MakeCurrentVisible into the OnShow event handler of Form2 then it works (tested on Windows). Probably MakecurrentVisible does not work correctly if the form and thus the listbox is not visible.

malabarista

  • Newbie
  • Posts: 4
Re: TListbox: makeCurrentVisible does not work
« Reply #5 on: March 25, 2017, 10:23:54 pm »
no, under Linux this does not work.

But I think, that this is the correct direction.
Something must be done in the OnShow-Event.

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: TListbox: makeCurrentVisible does not work
« Reply #6 on: March 25, 2017, 10:36:28 pm »
Looks like OnShow is too early as well. OnActivate works under both Windows and Linux.

Code: Pascal  [Select][+][-]
  1. procedure TForm2.FormActivate(Sender: TObject);
  2. begin
  3.   box1.MakeCurrentVisible;
  4.   box2.MakeCurrentVisible;
  5. end;

malabarista

  • Newbie
  • Posts: 4
Re: TListbox: makeCurrentVisible does not work
« Reply #7 on: March 27, 2017, 09:28:51 am »
Yes, that works! Great ! Thanks!

 

TinyPortal © 2005-2018