M
Hallo,
ich habe das Problem anders herum gelöst.
1. In die Items werden nur die Zahlen (1,2,...) geschrieben => diese werden Ohne Probleme in das zugeordnete Feld eingetragen.
2. Die Informationen ("1; Klaus; Müller; Berlin",...) werden als Objekte in die Items eingetragen.
3. Der Style wird auf csOwnerDrawFixed gesetzt
4. Im OnDrawItem wird dann unterschieden, ob man
a) im Eingabe/Editierfeld ist => Anzeige der Zahl, oder
b) im DropDownfeld ist => Anzeige der Information
nun noch ein bisschen Code:
1. Eintragen der Items/Informationen
int _id;
AnsiString _text;
TObject *_obj;
// ... Die Werte hole ich aus einer ADOQuery
_id = 1;
_text = IntToStr(_id).Trim() + "; vorname; name; plz; ort";
// ...
_obj = (TObject *)new AnsiString(_text);
DBComboBox1->AddObject(IntToStr(_id).Trim(),_obj);
}
2. Zeichen der Combobox
void __fastcall Form1::DBComboBox1DrawItem(TWinControl *Control,
int Index, TRect &Rect, TOwnerDrawState State)
{
AnsiString _text;
if (DBComboBox1->DroppedDown) // im DropDoenfeld
_text = *(String *)DBComboBox1->Items->Objects[Index];
else // im Editfeld
_text = DBComboBox1->Items->Strings[Index];
DBComboBox1->Canvas->FillRect(Rect);
DBComboBox1->Canvas->TextOutA(Rect.left, Rect.top, _text);
}
3. Ist auch Interessant
Anpassung der Breite des DropDownFeldes
(hab ich auch hier gefunden und ein wenig angepasst)
void Form1::OptimaleDropDownbreite(TComboBox *Box)
{
int TextWidth;
int TextMaxWidth = 0;
AnsiString _text;
// damits auch bei jeder Textgroesse klappt:
Box->Canvas->Font->Size = Box->Font->Size;
for(int i = 0; i < Box->Items->Count; i++)
{
if (Box->ClassNameIs("TDBComboBox")) // Speziell für Meine DBComboBox Sonst nur den "else-teil" verwenden
_text = *(String *)Box->Items->Objects[i];
else
_text = Box->Items->Strings[i];
TextWidth = Box->Canvas->TextWidth(_text) +
GetSystemMetrics(SM_CXVSCROLL) + 10;
if(TextMaxWidth < TextWidth)
{
TextMaxWidth = TextWidth;
}
}
Box->Perform(CB_SETDROPPEDWIDTH, TextMaxWidth, 0);
}
Gruß Mario