Recent

Author Topic: How to count a specific item in a ListBox  (Read 6085 times)

Aeliner123

  • Newbie
  • Posts: 5
How to count a specific item in a ListBox
« on: October 01, 2016, 01:40:27 pm »
Hi!, im new in Pascal-Lazarus and I'm working on a SuperMarket software, I'm getting information from a database which is showed in a TDBGrid, after that, I use a button to add the name from the Item that is being showed in the TDBGrid into a ListBox, but if I do it several times the name starts to stack over and over again, and I'm trying to count how many times the item that I'm adding is written in the ListBox and just add a numer at it's left, for example, if 'Sugar' is written 4 times in the ListBox, what I want is to write '4 Sugar', but I can't figure out how to do that, it would be great if u guys can help me.

PD: Sorry for my bad english, I'm a Spanish speaker and I'm not very good at English :P

zeljko

  • Hero Member
  • *****
  • Posts: 1596
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: How to count a specific item in a ListBox
« Reply #1 on: October 01, 2016, 02:03:10 pm »
Nobody have crystal ball to see your code, so nobody cannot help you without your code example.

Aeliner123

  • Newbie
  • Posts: 5
Re: How to count a specific item in a ListBox
« Reply #2 on: October 01, 2016, 02:07:11 pm »
I'm sorry, here is the code that I use to transfer the name from the TDBGrid to the ListBox

Code: Pascal  [Select][+][-]
  1. procedure TForm1.comprarClick(Sender: TObject);
  2. var
  3. begin
  4.   query.close;
  5.   query.SQL.text:= 'select imagen from productos where codigo='+codigo.Text;
  6.   query.open;
  7.   query.close;
  8.   query.SQL.text:= 'select nombre, precio from productos where codigo='+codigo.Text;
  9.   query.open;
  10.   query.First;
  11.   lista2.Items.Add(query['nombre'] );
  12.   query.Next;

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: How to count a specific item in a ListBox
« Reply #3 on: October 01, 2016, 02:14:47 pm »
Why not use the SQL Count function?

Also: are you sure this is right?
Code: Pascal  [Select][+][-]
  1. query.close;
  2. query.SQL.text:= 'select imagen from productos where codigo='+codigo.Text;
  3. query.open;
you just query the DB then discard the result by the very next query.
« Last Edit: October 01, 2016, 02:17:37 pm by shobits1 »

Aeliner123

  • Newbie
  • Posts: 5
Re: How to count a specific item in a ListBox
« Reply #4 on: October 01, 2016, 02:39:34 pm »
I forgot to delete that, it was part of another code to load images from the DB, also, what's the 'count' function from SQL?

ASerge

  • Hero Member
  • *****
  • Posts: 2240
Re: How to count a specific item in a ListBox
« Reply #5 on: October 01, 2016, 06:52:11 pm »
...what's the 'count' function from SQL?
Standard function for databases. 'select count(*) from View where ...', or 'select Count(Field) from View where ...' if you prefer exclude null from counting.
And among other things, stop building conditions for the where clause of a query. It is bad practice. Because the database will have each time a new analyze query. And besides, it is very unsafe. Use the parameters.

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: How to count a specific item in a ListBox
« Reply #6 on: October 01, 2016, 10:18:57 pm »
what's the 'count' function from SQL?
this tutorial will help you (I hope): https://www.techonthenet.com/sql/count.php

See 'Example - Using GROUP BY with the COUNT Function', I think thats what you'll need.
« Last Edit: October 01, 2016, 10:20:32 pm by shobits1 »

Aeliner123

  • Newbie
  • Posts: 5
Re: How to count a specific item in a ListBox
« Reply #7 on: October 02, 2016, 12:10:12 am »
The problem is that I don't need to count from anywhere but the ListBox, how many times a specific word is written in there. Maybe I missunderstanded but isn't the count from SQL to count from the DB?

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: How to count a specific item in a ListBox
« Reply #8 on: October 02, 2016, 02:20:24 pm »
The problem is that I don't need to count from anywhere but the ListBox, how many times a specific word is written in there. Maybe I missunderstanded but isn't the count from SQL to count from the DB?
What is in the ListBox? I assume it has everything that you queried from DB too, so then you can ask the count from DB.

But yeah you can make a for-loop that goes through the items and matching strings.
Code: Pascal  [Select][+][-]
  1. counter:=0;
  2. for k:=0 to listbox.items.count-1 do begin
  3.   if (pos('hello', listbox.items[k]) > 0) then begin
  4.     inc(counter);
  5.   end;
  6. end;

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: How to count a specific item in a ListBox
« Reply #9 on: October 02, 2016, 03:17:27 pm »
This does what you want, probably not the fastest solution, especially if the list has many items.

Code: Pascal  [Select][+][-]
  1. procedure AddToOrIncrementInList(AList: TStrings; AValue: String);
  2. var
  3.   i: Integer;
  4.   S, RS, LS: String;
  5.   Counter: Longint;
  6. begin
  7.   for i := 0 to AList.Count - 1 do
  8.   begin
  9.     S := AList.Strings[i];
  10.     if (S = AValue) then
  11.     begin
  12.       S := '2 ' + AValue;
  13.       AList.Strings[i] := S;
  14.       Exit;
  15.     end;
  16.     RS := RightStr(S, Length(AValue));
  17.     LS := TrimRight(Copy(S, 1, Length(S) - Length(AValue)));
  18.     if (RS = AValue) and TryStrToInt(LS, Counter) then
  19.     begin
  20.       Inc(Counter);
  21.       S := IntToStr(Counter) + #32 + AValue;
  22.       AList.Strings[i] := S;
  23.       Exit;
  24.     end;
  25.   end;
  26.   //it's not in the list yet
  27.   AList.Add(AValue);
  28. end;
  29.  

Call it like:

Code: Pascal  [Select][+][-]
  1.   AddToOrIncrementInList(ListBox1.Items, SomeText);
  2.  

Note that SomeText should not start with a space.
It will also probably crash if you try to add > MaxLongInt times the same SomeText  8-)

Bart
« Last Edit: October 02, 2016, 03:19:17 pm by Bart »

Aeliner123

  • Newbie
  • Posts: 5
Re: How to count a specific item in a ListBox
« Reply #10 on: October 05, 2016, 03:52:32 pm »
I tried your code and it seemed ok, but when I tried to compile it threw this error

'unit1.pas(67,15) Error: Forward declaration not solved "AddToOrIncrementInList(TStrings;AnsiString);"'

Being the line 67

Code: Pascal  [Select][+][-]
  1. procedure AddToOrIncrementInList(Alist: Tstrings; AValue: String);

Declarated as a private declaration.

Help me please

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: How to count a specific item in a ListBox
« Reply #11 on: October 05, 2016, 04:53:06 pm »
I did not make it a member of the Form!
It's just a plain procedure in th eimplementation sectio of whichever unit needs it.

Bart

 

TinyPortal © 2005-2018