We all know that tcombobox addobject is a good thing.
Recently, due to database structure design issues (many such as Dictionary tables) such as the country code table language table, tcombobox needs to store two columns of data, one code and one value.
For example, China's Chi, and these values are the addobject that the database reads and fills in, the criticism also reveals that, after a day's effort to write a class
Unit MCCB; interfaceuses ADODB, stdctrls; Type Tarr = array of string; Type tmycbb = Class (tobject) public procedure additem (adoquery1: tadoquery; asql: string; CCB: tcombobox; vaR SS: Tarr); end; implementation {tmycbb} procedure tmycbb. additem (adoquery1: tadoquery; asql: string; CCB: tcombobox; var SS: Tarr); var I: integer; s: string; begin CCB. items. clear (); s: = 'bd '; CCB. items. addobject ('indefinite ', tobject (s); adoquery1.close (); adoquery1. SQL. text: = asql; adoquery1.open (); setlength (SS, adoquery1.recordcount); for I: = 0 to adoquery1.recordcount-1 do begin ss [I]: = adoquery1.fields [0]. asstring; CCB. items. addobject (adoquery1.fields [1]. asstring, tobject (ss [I]); adoquery1.next (); end; adoquery1.close (); end.
Initialize the combo box
procedure TForm1.Button9Click(Sender: TObject);var dd: TMycbb; sqls: string;begin sqls := 'select lang_code,lang_name from language_code'; dd := TMycbb.Create; try dd.Additem(ADOQuery1, sqls, ComboBox1, ss); finally dd.Free; end;end;
Read drop-down value
procedure TForm1.Button7Click(Sender: TObject);var A_Value: string;begin A_Value := string(ComboBox1.Items.Objects[ComboBox1.ItemIndex]); ShowMessage(A_Value);
end;
Note that you need to test the definition of the global variable SS on the main form to ensure correct reading.
var ss: Tarr;
After the checkmem. Pas test, no memory leakage makes it easy to use and reduces the number of database reads from dictionary tables.