Form1上放有10个TDbedit(DBEdit1,DBEdit2,....DBEdit10):
如何实现以下功能:
procedure TForm1.DBEdit1KeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13  then DBEdit2.SetFocus;
end;procedure TForm1.DBEdit2KeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13  then DBEdit3.SetFocus;
end;procedure TForm1.DBEdit3KeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13  then DBEdit4.SetFocus;
end;........如何把它们统一起来,写成一个统一的过程或函数,上面的方法太笨了,
假如有30个TDBedit的话,那令人太难受了,
如何进行简化,如何把它们统一起来?

解决方案 »

  1.   

    procedure TFrmAddCddm.FormKeyPress(Sender: TObject; var Key: Char);
    var
      IntLoop,IntFocus:Integer;
    begin
      if Key=#13 then
      begin
        IntFocus:=1;
        for IntLoop:= 0 to ComponentCount-1 do
        begin
          if (Components[IntLoop] is TEdit) then
          begin
            if TEdit(Components[IntLoop]).Focused then
            IntFocus:=IntLoop+1;
          end
       end;
       if IntFocus=ComponentCount then
         IntFocus:=IntFocus-1;
       try
         TEdit(Components[IntFocus]).SetFocus;
       except
         Exit;
       end;
      end;
    end;
    这程序有点Bug,那就是创建组件时要按顺序来,否则还是不能达到像“管家婆“那样的轻松。
      

  2.   

    procedure TForm1.DBEdit1KeyPress(Sender: TObject; var Key: Char);
    begin
        if Key = #13 then
        PostMessage((Sender as TDBEdit).Handle,WM_KEYDOWN,VK_TAB,0);
    end;—————————————————————————————————
    宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
    —————————————————————————————————
      

  3.   

    这个不难自己定义一个procedure TForm1.DBEditKeyPress(Sender: TObject; var Key: Char);
    begin
      if Sender = (DBEdit1 as TObject) then
        ...
      else if Sender = (DBEdit2 as TObject) then
        ...
      else if Sender = (DBEdit3 as TObject) then
        ...
      else if Sender = (DBEdit4 as TObject) then
        ...
      else if Sender = (DBEdit5 as TObject) then
        ...
    end;
    当然,还有更简化的办法,嘿嘿……================================================================
    人生最大的幸福莫过于顿顿有玉米吃
    (CSDN 论坛助手,挺好使!俺们拨号上网的有福了)
      

  4.   

    procedure TForm1.DBEdit1KeyPress(Sender: TObject; var Key: Char);
    begin
        if Key = #13 then
        PostMessage((Sender as TDBEdit).Handle,WM_KEYDOWN,VK_TAB,0);
    end;然后将每个DBEdit的KeyPress事件都选择为DBEdit1KeyPress,按顺序排列每个DBEdit的TabOrder。—————————————————————————————————
    宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
    —————————————————————————————————
      

  5.   

    上面忘了说:在对象观察器里指定事件方法时,不要双击让它自动创建空方法,在下拉框里选 DBEditKeyPress 即可================================================================
    人生最大的幸福莫过于顿顿有玉米吃
    (CSDN 论坛助手,挺好使!俺们拨号上网的有福了)
      

  6.   

    先在DBEDIT1的KeyPress写上以下代码:
    procedure TForm1.DBEdit1KeyPress(Sender: TObject; var Key: Char);
    begin
        if Key = #13 then
        (Sender as TDBEdit).SETFOCUS;
    end;
    然后,在DBEDIT2..DBEDIT10中的EVENTS中的ONKEYPRESS选中DBEDIT1的ONKEYPRESS
    OK!
      

  7.   

    to lxpbuaa(桂枝香在故国晚秋):
       知音!知音!知音!知音!知音!知音!知音!知音!
    宠辱不惊,看庭前花开花落,去留无意,望天上云卷云舒。
      

  8.   

    先在DBEDIT1的KeyPress写上以下代码:
    procedure TForm1.DBEdit1KeyPress(Sender: TObject; var Key: Char);
    begin
        if Key = #13 then
        (Sender as TDBEdit).SETFOCUS;
    end;
    然后在FormCreate之类的事件中指定:
    procedure TForm1.FormCreate(Sender: TObject);
    var
      iCount : Integer;
    begin
    for iCount := 0 to ComponentCount - 1 do
      begin
       if (Components[iCount] is TDBEdit) and (Components[iCount].Tag = 1) then
        begin
          TDBEdit(Components[iCount]).OnKeyPress := DBEdit1KeyPress;
        end;
      end;
    end;