Recent

Author Topic: ComboBox: Autocomplete returns wrong Itemindex when Autodropdown=True  (Read 6209 times)

CM630

  • Hero Member
  • *****
  • Posts: 1091
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/

I have a form containing a combobox and a label and
the following code:


procedure TForm1.FormCreate(Sender: TObject);
  var
  i:integer;
  iname: integer=0;
begin
  ComboBox1.Clear;
  for i:=0 to 50 do
  begin
    iname:=iname+ 1+ trunc(Random* 8) ;
    ComboBox1.Items.Add(IntToStr(iname) + '(' + IntToStr(i) + ')');
  end; //for
  ComboBox1.ItemIndex:=0;


  ComboBox1.AutoComplete:=True;
  ComboBox1.AutoCompleteText:=[cbactEnabled,cbactSearchAscending];
  ComboBox1.AutoDropDown:=True;
end;


procedure TForm1.ComboBox1EditingDone(Sender: TObject);
begin
  Label1.Caption:=IntToStr(ComboBox1.ItemIndex);
end;




When I execute the code, I start typing in the combobox for example 145.
If there is a value „145(35)‟ in the combobox I expect that when I hit Enter I will get Label1 showing „35‟, because the itemindex of entry „145(35)‟ is 35.
And that is what I get if ComboBox1.AutoDropDown:=False;
But when ComboBox1.AutoDropDown:=True; instead of 145, I get for example 0, because the first entry in the combo, starting with 1 has ItemIndex=0. 


Or if there is a value „217(43)‟ in the combobox I expect that when I hit enter I will get Label1 showing „43‟. But it shows something like „2‟, because the first entry starting with „2‟ is „2‟ or „25‟ or something like this.
Again, if ComboBox1.AutoDropDown:=False; I get 43 (as expected), since the ItemIndex of entry „217(43)‟ is 43.


Is this some bug in Lazarus, and if not, is there a way to overcome this behaviour?
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

Handoko

  • Hero Member
  • *****
  • Posts: 5153
  • My goal: build my own game engine using Lazarus
Re: ComboBox: Autocomplete returns wrong Itemindex when Autodropdown=True
« Reply #1 on: August 07, 2017, 07:56:32 pm »
I cannot test your issue. Because running your code I got another issue. For example, there is an item '10(1)' in the the combo box. When I type the first character '1', the dropdown opened automatically. But then I can't type anything, except press esc or up/down arrow.

if I remove this line:
ComboBox1.AutoDropDown:=True;
then I can type freely. But I get one another issue. If I type the complete text, I always got result := -1. I have to type less than the whole text and let the system autocompletes the text, then I can get the correct result.

Tested on Lazarus 1.6.4 Gtk2 Linux64.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: ComboBox: Autocomplete returns wrong Itemindex when Autodropdown=True
« Reply #2 on: August 07, 2017, 09:11:16 pm »
Don't feel offended but there is *a lot* wrong with your code:
    iname:=iname+ 1+ trunc(Random *  8 ) ;
Why call trunc on an integer multiplication?
Why not simply:
Code: Pascal  [Select][+][-]
  1.   iname := iname+Random(8)+1;{Range of Random(8) is 0..7 and is an integer range}

Your bugs are caused by you: real programmers count from zero, so e.g. the 35th item has index 34... not 35...
Same here: Are you really sure you want 51 items, instead of 50? So for i := 0 to 49... instead of 51 items, because that is what for i := 0 to 50 means......  8-) >:D
Unless you really mean 51....Which for some reason I don't believe..... :o :D

What you do is writing compilable, syntactically correct nonsense. {$RANGECHECKS ON} may help you solve half of it... The other half is learning to count from zero.
Some more e.g:
Quote
Again, if ComboBox1.AutoDropDown:=False; I get 43 (as expected), since the ItemIndex of entry „217(43)‟ is 43.
no it's not! it is 42
You defined (xx) as iname, remember? And that is itemindex + 1! See your  random code....
« Last Edit: August 07, 2017, 09:42:53 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

CM630

  • Hero Member
  • *****
  • Posts: 1091
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: ComboBox: Autocomplete returns wrong Itemindex when Autodropdown=True
« Reply #3 on: August 09, 2017, 01:55:11 pm »
I cannot test your issue. Because running your code I got another issue. For example, there is an item '10(1)' in the the combo box. When I type the first character '1', the dropdown opened automatically. But then I can't type anything, except press esc or up/down arrow.
...
Tested on Lazarus 1.6.4 Gtk2 Linux64.
Thanks for the feedback. Possibly behaviour in GTK is different.
In Win7 I can write anything.
But, if you type 1, you could possibly type „11‟ or „12‟ or „13‟, because there will be entries in the list 11x, 12y (one-hundred and *teen; one-hundred twenty-something...)? I suppose you can see the existing values by scrolling down.

If behaviour of Combobox is system specific, I should use something else.
Any recommendation for a replacement component is welcome.

@Thaddy, I saw nothing in your post, related with my issue.
Anyway „random‟ without a parameter is extended. And about the second issue: 0 to 50, etc... I still do not see a mistake in my code, but I would not bother if I had.

Edit: For the time being I have solved the issue this way, which seems to work fine:
Code: [Select]
//Searches if the text of the combobox corresponds to an item from the Items property.
//Returns -1 if the item is not found.
function CboFindIndex (var ComboBox:TComboBox; CaseSensitive: Boolean= False): integer;
var
  i: integer;
  text: String;
begin
  Result:=-1;
  if ComboBox.Items.Count>0 then
  begin
    text:= ComboBox.Text;
    for i:=0 to ComboBox.Items.Count-1 do
      if StringCompare(text,ComboBox.Items,CaseSensitive)=0 then
      begin
        Result:=i;
        break;
      end; //if found
  end; //if
end;

function StringCompare(S1, S2: String; CaseSensitive: Boolean= false): integer;
begin
  if CaseSensitive=True
    then Result:=UTF8CompareStr(s1,s2)
    else Result:=UTF8CompareText(s1,s2);
end;


procedure TForm1.ComboBox1EditingDone(Sender: TObject);
begin
  Label1.Caption:=IntToStr(ComboBox1.ItemIndex);
end;

procedure TForm1.ComboBox1EditingDone(Sender: TObject);
var
  ItemIndex: integer;
begin
  ItemIndex:=CboFindIndex (ComboBox1);
  if ItemIndex>-1 then
  begin
    ComboBox1.ItemIndex:=ItemIndex;
    Label1.Caption:=ComboBox1.Items[ItemIndex];
End;
« Last Edit: August 09, 2017, 02:30:17 pm by CM630 »
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: ComboBox: Autocomplete returns wrong Itemindex when Autodropdown=True
« Reply #4 on: August 09, 2017, 02:26:26 pm »
Your sample works as expected with laz trunk and fpc trunk on windows 10 64bit.
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: ComboBox: Autocomplete returns wrong Itemindex when Autodropdown=True
« Reply #5 on: August 10, 2017, 06:11:54 pm »
Is this some bug in Lazarus, and if not, is there a way to overcome this behaviour?
Confirm bug.
A simpler example:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ComboBox1EditingDone(Sender: TObject);
  2. begin
  3.   Label1.Caption:=IntToStr(ComboBox1.ItemIndex);
  4. end;
  5.  
  6. procedure TForm1.FormCreate(Sender: TObject);
  7. begin
  8.   ComboBox1.AutoDropDown := True;
  9.   ComboBox1.AutoComplete := True;
  10.   ComboBox1.Items.Append('1 - 0');
  11.   ComboBox1.Items.Append('11 - 1');
  12. end;
When typing "11" and "Enter", "0" is displayed, but if line 8 is commented out, it shows "1", as expected.

CM630

  • Hero Member
  • *****
  • Posts: 1091
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: ComboBox: Autocomplete returns wrong Itemindex when Autodropdown=True
« Reply #6 on: August 17, 2017, 12:54:17 pm »
Thanks for the feedback, I will try to report it in the bucktracker in a couple of weeks.
« Last Edit: September 04, 2017, 08:36:12 am by CM630 »
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

Michl

  • Full Member
  • ***
  • Posts: 226
Re: ComboBox: Autocomplete returns wrong Itemindex when Autodropdown=True
« Reply #7 on: August 19, 2017, 11:12:43 pm »
Fixed in Lazarus trunk revision 55704 and merge requested for upcomming Lazarus 1.8.
Code: [Select]
type
  TLiveSelection = (lsMoney, lsChilds, lsTime);
  TLive = Array[0..1] of TLiveSelection;

 

TinyPortal © 2005-2018