Autor Beitrag
TheEquinox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16



BeitragVerfasst: Di 09.08.11 11:15 
Hallo zusammen,

in meinem Delphi7 Projekt habe ich eine Combobox mit Einträgen aus einer Datenbank befüllt. Jeder Eintrag besteht aus einem String und einem Integer:
ausblenden Delphi-Quelltext
1:
comboBox.AddObject(FieldByName('Description').AsString, TObject(FieldByName('ID').AsInteger));					
Wird der Frame mit der Combobox jetzt aufgerufen, wird der aktuelle Datensatz aus der Datenbank gelesen, der auch eine ID enthält. Ich würde jetzt gerne in der Combobox den Eintrag vorauswählen, der diese ID hat. Wie stelle ich das an ?

Selektieren geht ja über comboBox.SelectedIndex := <zahl>

Allerdings sind die IDs nicht von 1-x durchnummeriert, sondern können beliebige (große) Zahlen enthalten.

Bsp. ID des aktuellen Datensatz ist 123456, jetzt möchte ich in der Combobox das Item mit der ID 123456 auswählen.
Ich suche so etwas wie (Pseudocode) comboBox.SelectedIndex := "combobox.GetItemIndexByValue(123456)";

Moderiert von user profile iconNarses: Delphi-Tags hinzugefügt
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10184
Erhaltene Danke: 1259

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 09.08.11 11:32 
Moin!

Das gibt´s AFAIR nicht fertig, schreib´s halt schnell selbst: :idea: ;)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
function GetItemByObject(AStrings: TStrings; const AValue: TObject): Integer;
begin
  for Result := 0 to AStrings.Count-1 do
    if (AStrings.Objects[Result] = AValue) then
      Exit;
  Result := -1;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ShowMessage(IntToStr(GetItemByObject(ComboBox1.Items, NIL)));
end;
cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
TheEquinox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16



BeitragVerfasst: Di 09.08.11 11:39 
Schade, aber war wohl ein zu offensichtlicher Anwendungsfall, um in der Combobox realisiert zu werden :-(
Danke Dir !