Delphi ComboBox addobject

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.