How to make DBlookUpComboBox, my way is to make a combination control, below it (of course, it can be above) plus a small edit box, I have completed 80% of the code, I inherited from TCUSTomControl, in After my code, I will explain why I inherit from this class, not from TwinControl, please see the code first:
.h file
/ / -------------------------------------------------------------------------------------------- ---------------------------
#ifndef kingkingh # define kingkingh // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ --------------------------------- # include
/ / -------------------------------------------------------------------------------------------- --------------------------- Class Package Tkingking: Public TcustomControl {Private: TDBlookUpComboBox * flookup; tedit * fdit; tdataroup * fdataSource; tdataroup * Ansistring fkeyfield; ansistring fkeyfield; int fbaudrate;
protected: virtual void __fastcall Paint (); virtual __fastcall ~ TKingKing (); virtual void __fastcall CreateWnd (); public: __fastcall TKingKing (TComponent * Owner); void __fastcall SetDataSource (TDataSource * DataSource); void __fastcall SetListSource (TDataSource * DataSource) ; void __fastcall SetKeyField (AnsiString Field); void __fastcall SetDataField (AnsiString Field); void __fastcall SetListField (AnsiString Field); void __fastcall SetBaudRate (int BaudRate); void __fastcall DoMyJob (TObject * Sender, WORD & Key, TShiftState Shift);
__published: __property TDataSource * DataSource = {read = FDataSource, write = SetDataSource}; __property TDataSource * ListSource = {read = FListSource, write = SetListSource}; __property AnsiString KeyField = {read = FKeyField, write = SetKeyField}; __property AnsiString ListField = {read = FListField, write = SetListField}; __property AnsiString DataField = {read = FDataField, write = SetDataField}; __property int BaudRate = {read = FBaudRate, write = SetBaudRate};}; class TDataOfFieldProperty: public TStringProperty {public: TPropertyAttributes __fastcall GetAttributes () {return TPropertyAttributes () << paValueList;} void __fastcall GetValues (TGetStrProc Proc) {Proc ( "None"); TStrings * pList = new TStringList () ;; TKingKing * pDCBX = dynamic_cast
}; // List Fieldclass TListOfFieldProperty: public TStringProperty {public: TPropertyAttributes __fastcall GetAttributes () {return TPropertyAttributes () << paValueList;} void __fastcall GetValues (TGetStrProc Proc) {Proc ( "None"); TStrings * pList = new TStringList ( ); Tkingking * pdcbx = Dynamic_cast
}; // KeyFieldclass TKeyOfFieldProperty: public TStringProperty {public: TPropertyAttributes __fastcall GetAttributes () {return TPropertyAttributes () << paValueList;} void __fastcall GetValues (TGetStrProc Proc) {Proc ( "None"); TStrings * pList = new TStringList () ; Tkingking * pdcbx = Dynamic_cast
/ / -------------------------------------------------------------------------------------------- --------------------------- # Endif
.cpp file
/ / -------------------------------------------------------------------------------------------- ---------------------------
#include
#include "kingking.h" #pragma package (smart_init) // --------------------------------- ---------------------------------------- // ValidcTrcheck Is Used to assocure what Created Do Not Have // Any Pure Virtual Functions.//static inline void validctrcheck (tkingking *) {new tkingking (null);} // ------------------- -------------------------------------------------- ------ TTypeInfo * AnsiStringInfo () {TTypeInfo * pTypeInfo = new TTypeInfo; pTypeInfo-> Kind = tkLString; pTypeInfo-> Name = "AnsiString"; return pTypeInfo;} TTypeInfo * IntInfo () {TTypeInfo * pTypeInfo = new TTYPEINFO; PTYPEINFO-> Kind = tkinteger; ptypeinfo-> name = "integer"; return ptypeinfo;}
__fastcall TKingKing :: TKingKing (TComponent * Owner): TCustomControl (Owner) {FLookup = new TDBLookupComboBox (this); FEdit = new TEdit (this); FEdit-> OnKeyDown = DoMyJob; Width = 150; Height = 46;
} // ----------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------- void __fastcall tkingking :: createwnd () {tcustomControl :: CreateWnd (); flookup-> DataSource = DataSource FLOOKUP-> ListSource = listSource; flookup-> datafield = datafield; flookup-> listfield = listfield; flookup-> keyfield = keyfield;
} // ----------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------- void __fastcall tkingking :: Domyjob (Tobject * Sender, Word & Key, TshiftState Shift) {ix == 13) {flookup-> keyfield = fedit-> text;}} // --------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------
Void __fastcall tkingking :: Paint () {width = 150; Height = 46; flookup-> parent = this; flookup-> left = 0; flookup-> TOP = 0; flookup-> width = 145; flookup-> height = 21; fedit-> parent = this; fedit-> left = 0; fedit-> TOP = flookup-> height; fedit-> width = 80; fedit-> height = 21;} void __fastcall tkingking :: setDataSource (tdataSource * DataSource) {fdataSource = DataSource;} // ------------------------------------------------------------------------------------------------------------------------------------------------ ---------------------------------- Void __fastcall tkingking :: setListSource (tdataroup * Datasource) {flistSource = DataSource; } // ----------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------- void __fastcall tkingking :: setKeyfield (ANSISTRING FIELD) {fkeyfield = field;} void __fastcall tkingking :: setListfield Ansistring Field) {flistfield = field;} void __fastcall tkingking :: setdatafield {fdatafield = field;} void __fastcall tkingking :: setbaudrate (int baudrate) {FBAU Drate = Baudrate;} __ fastcall tkingking :: ~ tkingking () {if (flookup) {delete flookup;}} (fedit) {delete fedit;}
} Namespace Kingking {void __fastcall PACKAGE Register () {TComponentClass classes [1] = {__classid (TKingKing)}; RegisterPropertyEditor (AnsiStringInfo (), __ classid (TKingKing), "DataField", __ classid (TDataOfFieldProperty)); RegisterPropertyEditor (AnsiStringInfo () , __ classid (TKingKing), "ListField", __ classid (TListOfFieldProperty)); RegisterPropertyEditor (AnsiStringInfo (), __ classid (TKingKing), "KeyField", __ classid (TKeyOfFieldProperty)); RegisterPropertyEditor (IntInfo (), __ classid (TKingKing), "BaudRate ", __ classId (tbaudrateproperty); Registercomponents (" AKING ", Classes, 0);}} // --------------------------- ------------------------------------------------ I don't inherit from TwinControl, and inherit from TCUSTOMControl, I will see this problem from the level of the class:
I compare two components Discovery: TDBLookUpComboBox and TDBCOMBOBOX found, first of all, their inheritance class hierarchy, where TDBCOMBOBOX's inheritance hierarchy has TCustomComboBox (this class is Tcombobox's parent class), and TDBLOOKUPCOMBOBOX's most direct parent class is TCUSTomControl This is precisely that TDBlookUpComboBox is placed in the Paint function. And my error code is inherited from TwinControl, and the biggest feature of the parent class is TwinControl control is that it has been able to draw from, and TCUSTOMControl needs self-drawn (PAINT method), which can judge why TDBLOOKUPCOMBOBOX is in design The reason that already has a handle is the process of inheriting from TCUSTomControl, TDBLOOKUPCOMBOBOX not only overloads the PAINT method, but also overloads the CREATEWND method. And if you inherit from TwinControl, you will generate a TDBLOOKUPCOMBOBOX will have an error prompt "The control does not have a parent window." My code is basically completed. The only part is the code in the onkeyDown of the edit box, you can do it as needed.