Recent

Author Topic: Internal values of Combobox, Listbox  (Read 17538 times)

M[a]nny

  • Full Member
  • ***
  • Posts: 130
  • Dreamer
Internal values of Combobox, Listbox
« on: July 20, 2010, 10:44:20 am »
Hello.

Imagine MySQL database with one table which contains these columns: "UserId, UserName".
Now, we want to fill Combobox with all users in database ordered by their names.

Code: [Select]
Combobox.Clear;
Query.SQL.Text := 'SELECT * FROM users';
Query.Open;
while not Query.EOF then begin
  Combobox.Items.Add(Query.FieldByName('UserName').AsString);
  Query.Next;
end;
Query.Close;

All users are showed in items of Combobox. And the problem is... how can I recognize ID of the selected user? I can't do it by his name because of duplicate rows. Is there any way to do this easy? Thanks for helping.
Bad news: Time flies.
Good news: You are the pilot.

Don't try to be perfect, just be unique.

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2615
Re: Internal values of Combobox, Listbox
« Reply #1 on: July 20, 2010, 12:24:23 pm »
Maybe have a look at the TDBLookupCombobox.

But if you want a standalone Combobox, you can "store" the ID in the object part (this is a bit of a hack)

Code: Pascal  [Select][+][-]
  1. Combobox.Clear;
  2. Query.SQL.Text := 'SELECT * FROM users';
  3. Query.Open;
  4. while not Query.EOF do
  5. begin
  6.   Combobox.Items.AddObject(Query.FieldByName('UserName').AsString, TObject(Query.FieldByName('ID').AsInteger));
  7.   Query.Next;
  8. end;
  9. Query.Close;
  10.  

btw, if you only need ID and Username, then selecting "select id,username form..." is more efficient
« Last Edit: July 20, 2010, 12:26:07 pm by Marc »
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

M[a]nny

  • Full Member
  • ***
  • Posts: 130
  • Dreamer
Re: Internal values of Combobox, Listbox
« Reply #2 on: July 23, 2010, 09:43:52 am »
Thank you for your time and experience.
I made it by this way:

If i want to add new item to ComboBox:
Code: Pascal  [Select][+][-]
  1. UsrName := 'Chuck Norris'; // type: string
  2. UsrID := 1; // type: integer
  3. ComboBox.Items.AddObject(UsrName, TObject(UsrID));

... and if I want to check what object is selected (returns UsrID):
Code: Pascal  [Select][+][-]
  1. SelectedID := Integer(ComboBox.Items.Objects[ComboBox.ItemIndex]); // type: integer
« Last Edit: July 23, 2010, 09:49:05 am by M[a]nny »
Bad news: Time flies.
Good news: You are the pilot.

Don't try to be perfect, just be unique.

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Internal values of Combobox, Listbox
« Reply #3 on: July 23, 2010, 10:25:50 am »
Prova a seguire questo link, sono due componenti che ho fatto io. Si possono visualizzare anche più campi oltre ad avere un campo chiave nascosto.

Ha solo un piccolo difetto la combobox, quando si cercano di visualizzare più campi non riesco a incolonnarli perchè non riesco a definire il formato del testo nel popup che ti visualizza i dati disponibili per essere selezionati.

http://www.lazarus.freepascal.org/index.php?topic=8708.0

edit:
replaced ip by address
« Last Edit: July 24, 2010, 03:27:45 am by Marc »
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018