type pFields=^TFields;//定义一个记录集 TFields=Record ID,CName,EName:String; end;在添加到ComboBox时如下: var pF:pFields; begin New(pF); pF.ID:=ADOQuery1.FieldByName('ID').AsString; pF.CName:=ADOQuery1.FieldByName('CName').AsString; pF.EName:=ADOQuery1.FieldByName('EName').AsString; ComboBox1.Items.AddObject(pF.CName,TObject(pF));//ComboBox1.Items.AddObject('要显示的内容',TObject(记录集(不能为nil)));调用时如下: begin if ComboBox1.ItemIndex<0 then Exit; Edit1.Text:=pFields(ComboBox1.Items.Object[ComboBox1.ItemIndex]).EName; end;
先把用户名用一个STRINGLIS保存起来,再 StrList:=Tstringlist.Create; while not adodataset eof do begin begin StrList.Add(FieldValues['用户名']); end;StrList[ComboBox1.Items.IndexOf(ComboBox1.Text)]
lwgygz(小欣)的方法是通用性最强的解法 但如果搂主只想实现ID与Value对应,可使用以下简单用发var IDArr:Array of string; RowCount:integer; procedure Add; begin SetLength(IDArr, ADOQuery1.RecordCount); RowCount := 0; while not eof do begin combobox1.Items.Strings[RowCount]:=ADOQuery1.FieldByName('Name').AsString; IDArr[RowCount]:=ADOQuery1.FieldByName('ID').AsString; Inc(RowCount); Next; end; end;使用时 IDArr[combobox1.ItemIndex] 就是你要的ID值
lwgygz(小欣)的方法是对象化后扩展性最强的解法 但如果搂主只想实现ID与Value对应,可使用以下简单用发var IDArr:Array of string; RowCount:integer; procedure Add; begin SetLength(IDArr, ADOQuery1.RecordCount); RowCount := 0; while not eof do begin combobox1.Items.Strings[RowCount]:=ADOQuery1.FieldByName('Name').AsString; IDArr[RowCount]:=ADOQuery1.FieldByName('ID').AsString; Inc(RowCount); Next; end; end;使用时 IDArr[combobox1.ItemIndex] 就是你要的ID值
pFields=^TFields;//定义一个记录集
TFields=Record
ID,CName,EName:String;
end;在添加到ComboBox时如下:
var pF:pFields;
begin
New(pF);
pF.ID:=ADOQuery1.FieldByName('ID').AsString;
pF.CName:=ADOQuery1.FieldByName('CName').AsString;
pF.EName:=ADOQuery1.FieldByName('EName').AsString;
ComboBox1.Items.AddObject(pF.CName,TObject(pF));//ComboBox1.Items.AddObject('要显示的内容',TObject(记录集(不能为nil)));调用时如下:
begin
if ComboBox1.ItemIndex<0 then Exit;
Edit1.Text:=pFields(ComboBox1.Items.Object[ComboBox1.ItemIndex]).EName;
end;
<select name="select">
<option value="www1">张三</option>
<option value="yd2">李四</option>
</select>
保存时用value
StrList:=Tstringlist.Create;
while not adodataset eof do begin
begin
StrList.Add(FieldValues['用户名']);
end;StrList[ComboBox1.Items.IndexOf(ComboBox1.Text)]
1.在combobox里加入"姓名--用户名"
2.在combobox的onchange事件里取得"用户名"("--"后的字符串可以使用pos取得)并保存
但如果搂主只想实现ID与Value对应,可使用以下简单用发var IDArr:Array of string;
RowCount:integer;
procedure Add;
begin
SetLength(IDArr, ADOQuery1.RecordCount);
RowCount := 0;
while not eof do
begin
combobox1.Items.Strings[RowCount]:=ADOQuery1.FieldByName('Name').AsString;
IDArr[RowCount]:=ADOQuery1.FieldByName('ID').AsString;
Inc(RowCount);
Next;
end;
end;使用时
IDArr[combobox1.ItemIndex] 就是你要的ID值
但如果搂主只想实现ID与Value对应,可使用以下简单用发var IDArr:Array of string;
RowCount:integer;
procedure Add;
begin
SetLength(IDArr, ADOQuery1.RecordCount);
RowCount := 0;
while not eof do
begin
combobox1.Items.Strings[RowCount]:=ADOQuery1.FieldByName('Name').AsString;
IDArr[RowCount]:=ADOQuery1.FieldByName('ID').AsString;
Inc(RowCount);
Next;
end;
end;使用时
IDArr[combobox1.ItemIndex] 就是你要的ID值