OnSelect-Ereignis von DBComboBox !?
-
Hallo,
irgendwie bin ich wohl auf dem Holzweg ?
Ich benutze eine DBComboBox Style = csDropDownList und füttere die Items
aus einer Select-Abfrage.
Die Items sehen dann etwa so aus:1; Klaus; Müller; Berlin 2; Horst; Meayer; Bochum . . .
Nun soll aber aber nur die Zahl für das Feld benutzt werden, ist ja auch kein Problem, die aus dem String herauszulösen.
ABER
Wenn der Style = csDropDownList ist dann wird OnChange nicht ausgeführt !
Laut Hilfe :
Hinweis:
Das Ereignis OnChange wird nicht ausgelöst, wenn Style den Wert csDropDownList hat. Verwenden Sie für solche Kombinationsfelder statt dessen OnSelectDieser Hinweis kommt nur in der CLX-Hilfe da dort OnSelect in der TCustomComboBox enthalten ist.
In der VCL-Version ist OnSelect jedoch in der TCustomCombo enthalten.Wie bekomme ich dieses Ereignis wieder sichtbar ?
Gruß und
Mario
-
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 Informationnun noch ein bisschen Code:
1. Eintragen der Items/Informationenint _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