Delphi_ComboBox_AddObject_Items_Values

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, DB, ADODB;

type
TForm1 = class(TForm)
    cmbbzxh: TComboBox;
    bbtnShowName1: TBitBtn;
    aqrytm: TADOQuery;
    ADOConnection1: TADOConnection;
    Memo1: TMemo;
    bbtnIsExists1: TBitBtn;
    Edit1: TEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure bbtnShowName1Click(Sender: TObject);
    procedure bbtnIsExists1Click(Sender: TObject);
private
    { Private declarations }
    function FindStrPos(mStr:string; mStrings:TStrings; var nX,nY:Integer):Boolean;
public
    { Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=caFree;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
aqrytm.Close;
aqrytm.SQL.Clear;
aqrytm.SQL.Add('SELECT v_id,v_name from [v_personinfo] ');
aqrytm.SQL.Add('where v_id like :P1 order by v_id desc ');
aqrytm.Parameters.ParamByName('P1').Value:='C003%';
aqrytm.Open;
aqrytm.First;
while not aqrytm.Eof do
begin
    cmbbzxh.Items.AddObject(aqrytm.FieldByName('v_id').AsString,//Items
      TObject(NewStr(aqrytm.FieldByName('v_name').AsString)));//Value
    aqrytm.Next;
end;
aqrytm.Close;
cmbbzxh.ItemIndex:=0;
{
[Warning] ... symbol 'NewStr' is deprecated
}
end;

procedure TForm1.bbtnShowName1Click(Sender: TObject);
begin
ShowMessage('Value:('+PString(cmbbzxh.Items.Objects[cmbbzxh.ItemIndex])^+')');
end;

function TForm1.FindStrPos(mStr:string; mStrings:TStrings; var nX,nY:Integer):Boolean;
var 
iii,jjj:Integer;
begin 
Result:=False; 
for iii:=0 to Pred(mStrings.Count) do
begin 
    jjj:=Pos(mStr,mStrings[iii]);
    if jjj>0 then
    begin 
      nX:=jjj;
      nY:=iii; 
      Result:=True; 
      Break; 
    end; 
end;
end;

procedure TForm1.bbtnIsExists1Click(Sender: TObject);
var
strtm:string;
kx,ky:Integer;
begin
strtm:=trim(Edit1.Text);
if strtm='' then
begin
    Application.MessageBox('NotNull.','prompt',16);
    Edit1.SetFocus;
    exit;
end;
if FindStrPos(strtm,cmbbzxh.Items,kx,ky) then
begin
    ShowMessage('kx:('+inttostr(kx)+'); ky:('+inttostr(ky)+')');
end else begin
    ShowMessage('Not Exists');
end;
end;

end.
----------------------------------------------------------------------------------------------------------------------------------

 

 

----------------------------------------------------------------------------------------------------------------------------------

delphi中combobox键值对addobject

 

 

1.添加键值对:

    cbb1.Items.AddObject(‘显示的文字‘, TObject(NewStr(’作为值存在的字符串‘)));

2.读取选中键的值

   ShowMessage(PString(cbb1.Items.Objects[cbb1.ItemIndex])^);

实例:从文件中读取cbbEffect的键值对

IniPath := ExtractFilePath(Application.ExeName)+'./effect.ini'; //当前工程路径下的effect.ini文件
   str1 := TStringList.Create();
   str1.LoadFromFile(IniPath);
   for i:=0 to str1.Count-1 do
   begin
       cbbEffect.Items.AddObject(str1.ValueFromIndex[i],TObject(NewStr(str1.Names[i])));
   end;

其中,effect.ini的内容如下

0=图片

1=文字

2=动画

 

----------------------------------------------------------------------------------------------------------------------------------

 

转载于:https://www.cnblogs.com/lzj1981/archive/2013/04/28/3049389.html

你可能感兴趣的:(Delphi_ComboBox_AddObject_Items_Values)