Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 11 total
Thread TCombobox - need to add an ID to the items
Wed, Jul 2 2014 6:16 AMPermanent Link

Uli Becker

In Delphi I usually use AddObject to add a unique ID to the item of a
combobox - is it possible to do something similar in EWB?

What I need is to populate a combobox with string values from a table,
but also need the ID of the selected record.

Uli
Wed, Jul 2 2014 7:16 AMPermanent Link

Matthew Jones

Uli Becker wrote:

> In Delphi I usually use AddObject to add a unique ID to the item of a
> combobox - is it possible to do something similar in EWB?
>
> What I need is to populate a combobox with string values from a
> table, but also need the ID of the selected record.

I don't think it is. I keep a separate list in parallel to the combo
list, built at run-time, and then I can use the same ItemIndex to do
the reference.

--

Matthew Jones
Wed, Jul 2 2014 8:20 AMPermanent Link

Uli Becker

Matthew,

> I don't think it is. I keep a separate list in parallel to the combo
> list, built at run-time, and then I can use the same ItemIndex to do
> the reference.

Good workaround - thanks.

Uli
Tue, Dec 16 2014 10:06 AMPermanent Link

Petter Topp

"Matthew Jones" wrote:

Uli Becker wrote:

> In Delphi I usually use AddObject to add a unique ID to the item of a
> combobox - is it possible to do something similar in EWB?
>
> What I need is to populate a combobox with string values from a
> table, but also need the ID of the selected record.

I don't think it is. I keep a separate list in parallel to the combo
list, built at run-time, and then I can use the same ItemIndex to do
the reference.

--

Matthew Jones

Hi Matthew.

Coud you share your solution?

Br
Petter
Tue, Dec 16 2014 10:51 AMPermanent Link

Uli Becker

Petter,

> Coud you share your solution?

I'm not Matthew Smile but here's some code (untested):

IDList := TStringList.create;
....
IDList.clear;
Comobox1.items.clear;
....
MyDataset.first;
while not MyDataset.EOF do
begin
   Combobox1.items.add(MyDataset.columns['MyValue'].asString);
   IDList.add(MyDataset.columns['MyID'].asString);
   MyDataset.next;
end;

Chosen value:

MyValue := Combobox1.items[ComboBox1.itemindex];
MyIDValue := StrToInt(ComboBox1.itemindex);

Hope that helps.

Uli
Tue, Dec 16 2014 11:55 AMPermanent Link

Matthew Jones

Uli Becker wrote:

>
> I'm not Matthew Smile but here's some code (untested):
>

That's the way it's done. Only issue to be aware of is sorting - if you
let the combo sort itself, you break your matched lists. Just sort them
yourself if needed, or sort them before you add.


--

Matthew Jones
Tue, Dec 16 2014 9:09 PMPermanent Link

Stephen Barker

Uli Becker wrote:

MyValue := Combobox1.items[ComboBox1.itemindex];
MyIDValue := StrToInt(ComboBox1.itemindex);

--------------------------------------

I think that second line above should read:

MyIDValue := StrToInt(IDList[ComboBox1.itemindex]);


cheers,
Steve
Wed, Dec 17 2014 1:04 AMPermanent Link

Petter Topp

Uli Becker wrote:

Petter,

> Coud you share your solution?

I'm not Matthew Smile but here's some code (untested):

IDList := TStringList.create;
....
IDList.clear;
Comobox1.items.clear;
....
MyDataset.first;
while not MyDataset.EOF do
begin
   Combobox1.items.add(MyDataset.columns['MyValue'].asString);
   IDList.add(MyDataset.columns['MyID'].asString);
   MyDataset.next;
end;

Chosen value:

MyValue := Combobox1.items[ComboBox1.itemindex];
MyIDValue := StrToInt(ComboBox1.itemindex);

Hope that helps.

Uli

Thanks Uli - should have solved this my self, I'm getting real lazy...

Petter
Wed, Dec 17 2014 3:45 AMPermanent Link

alexza

Given the function:

function RPad(const Value: String; PadLen: Integer; PadChar: Char=' '): String;  // from Pad, in Webcore.wbs
begin
 Result := Value;
 while (Length(Result) < PadLen) do
   Result := (Result + PadChar);
end;

another solution, which allows for sorting, is:

TempStrings: array of String;
....

Combobox1.Items.Clear;
MyDataset.First;
while not MyDataset.EOF do
begin
 Combobox1.Items.Add(RPad(MyDataset.Columns['MyValue'].AsString, 50, ' ') + '#' +
                                   MyDataset.Columns['MyID'].AsString);
 MyDataset.Next;
end;

Chosen values:

TempStrings := Split(Combobox1.Items[ComboBox1.Itemindex], '#');   // Split, in Webcore.wbs

MyValue   := Trim(TempStrings[0]);
MyIDValue := StrToInt(Trim(TempStrings[1]));

The value 50 (length of resulting string so as the combobox shows only the descriptive part and not the IDs) and the separator char '#' can be chosen based on data to be handled.
Wed, Dec 17 2014 5:38 AMPermanent Link

Uli Becker

Stephen,

> I think that second line above should read:
>
> MyIDValue := StrToInt(IDList[ComboBox1.itemindex]);

You are right - that's why I wrote "untested", didn't have much time Smile

Uli
Page 1 of 2Next Page
Jump to Page:  1 2
Image