The value of the selected item in ComboBox

zhaozj2021-02-16  101

Sometimes we need to process according to the value of the ComboBox (ListBox) selection, but the item of ComboBox in Delphi is an object of TStrings type, we can't inherit from the comboBox option class as C # or Java, create A class we need to complete the task. But carefully study Delphi's ComboBox object found the following solution:

Create a category to store the data we need:

TITEMEX = Class (TOBJECT) CAPTION: STRING; PUBLIC STRINGVALUE: STRING;

// fill comboboxfunction FillInComBoBoxWithAdoQuery adoquery values ​​used in the (:;: TComboBox; sql: objComBoBox TAdoQuery objAdoQuery string; captionFieldName: string; valueFieldName: string; noAsFirst: boolean): boolean;

// When noAsFirst is true, the first combobox is 'no' var objItemEx: TItemEx; begin objComBoBox.Clear; objComBoBox.ItemIndex: = - 1; if noAsFirst then begin objItemEx: = TItemEx.Create; objItemEx.caption: = 'None'; objItemEx.StringValue: = ''; objComBoBox.Items.AddObject (objItemEx.caption, objItemEx); objComBoBox.ItemIndex: = 0; end; objAdoQuery.Close; objAdoQuery.SQL.Clear; objAdoQuery.SQL.Add (sql); objAdoQuery.Open; objAdoQuery.First; while not objAdoQuery.Eof do begin objItemEx: = TItemEx.Create; objItemEx.caption: = objAdoQuery.FieldByName (captionFieldName) .AsString; objItemEx.StringValue: = objAdoQuery.FieldByName (valueFieldName ) .Sstring; objcomboBox.Items.addObject (objiteMex.caption, objadoquery.next; end; objadouery.close; result: = true;

// Get comboobx in the selected braking function GetComBoBoxSelectedStringValue (objComBoBox: TComboBox): string; var objItemEx: TItemEx; begin if (objComBoBox.ItemIndex> -1) then begin objItemEx: = (objComBoBox.Items.Objects [objComBoBox. ItemIndex] as titleX); Result: = ObjiteMex.StringValue; Else Begin Result: = '; END;

Listbox's solution is similar to this.

转载请注明原文地址:https://www.9cbs.com/read-14474.html

New Post(0)