if (key=#13) and (edit1.text<>'') then begin query1.close; query1.sql.clear; query1.sql.add('select 部门 from 表名 where 编号='''+edit1.text+''''); query1.open; if not query1.isEmpty then edit2.text :=query1.fieldbyname('部门').asstring else //查不到 end; query1.close;
procedure TForm1.Edit1Exit(Sender: TObject); var s: string; I: Integer; begin s := Edit1.Text; for I := 0 to ListView1.Items.Count - 1 do begin if s = ListView1.Items[I].Caption then begin Edit2.Text := ListView1.Items[I].SubItems[0]; Break; end; end; end;
上面的代码在Edit1的OnExit事件中
在Edit1的keypress事件里添加 var bh,bm: string; found: boolean; loop: integer; begin if (key=#13) then begin bh := trim(edit1.text); found := false; if length(bh)=0 then begin ShowMessage('输入无效'); Exit; end; for loop := 0 to ListView1.Items.Count-1 do begin if ListView1.Items.Item[loop].Caption = bh then begin found := true; bm := ListView1.Items.Item[loop].SubItems.Strings[0]; break; end; end; if not found then begin ShowMessage('没有对应的编号'); Exit; end; edit2.text := bm; end; end;
begin
query1.close;
query1.sql.clear;
query1.sql.add('select 部门 from 表名 where 编号='''+edit1.text+'''');
query1.open;
if not query1.isEmpty then
edit2.text :=query1.fieldbyname('部门').asstring
else
//查不到
end;
query1.close;
var
s: string;
I: Integer;
begin
s := Edit1.Text;
for I := 0 to ListView1.Items.Count - 1 do
begin
if s = ListView1.Items[I].Caption then
begin
Edit2.Text := ListView1.Items[I].SubItems[0];
Break;
end;
end;
end;
var
bh,bm: string;
found: boolean;
loop: integer;
begin
if (key=#13) then
begin
bh := trim(edit1.text);
found := false;
if length(bh)=0 then
begin
ShowMessage('输入无效');
Exit;
end;
for loop := 0 to ListView1.Items.Count-1 do
begin
if ListView1.Items.Item[loop].Caption = bh then
begin
found := true;
bm := ListView1.Items.Item[loop].SubItems.Strings[0];
break;
end;
end;
if not found then
begin
ShowMessage('没有对应的编号');
Exit;
end;
edit2.text := bm;
end;
end;