unit LinkEdit;interfaceuses
  Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Dialogs;type
  TLinkEdit = class(TEdit)
  private
    FActive: Boolean;
    FLink: String;
    Procedure SetActiveValue(AValue: Boolean);
    Procedure SetLinkValue(ALink: String);
    procedure WMKEYdownClick(var Msg: TWMKEYDOWN); message WM_KEYDOWN;
    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }
  published
    property Active: Boolean read FActive write SetActiveValue;
    property Link: String read FLink write SetLinkValue;
    { Published declarations }
  end;procedure Register;implementationprocedure Register;
begin
  RegisterComponents('XP', [TLinkEdit]);
end;procedure TLinkEdit.SetActiveValue(AValue: Boolean);
begin
  if FActive <> AValue then
     FActive :=Avalue;
end;procedure TLinkEdit.SetLinkValue(ALink: String);
begin
  if FLink <> Alink then
     FLink :=Alink;
end;procedure TLinkEdit.WMKEYdownClick(var Msg: TWMKEYDOWN);
var
  vEdit:TEdit;
begin
  if FActive Then
  Begin
     if Msg.CharCode = VK_RETURN then   
     begin
      if Flink <> '' then
       begin
          vEdit:=FindComponent(FLink) as TEdit;   //有问题
          vEdit.SetFocus;
       end;
     end;
  end;
end;end.这个组件TLinkEdit要实现的是:在按下回车键后把焦点转移到Link指定的Edit,大家看看吧,如果有解决方法请告诉一声,谢谢

解决方案 »

  1.   

    procedure TLinkEdit.WMKEYdownClick(var Msg: TWMKEYDOWN);
    var
      vEdit:TEdit;
      lp: integer;
    begin
      if FActive Then
      Begin
         if Msg.CharCode = VK_RETURN then   
         begin
          if Flink <> '' then
           begin
              vEdit := nil;
              for lp := 0 to TWincontrol(Owner).ControlCount - 1 do
              begin
                if TWincontrol(Owner).Controls[lp] is TEdit then
                begin
                 if UpperCase(TEdit(TWincontrol(Owner).Controls[lp]).Name) = 
                    UpperCase(Flink) then
                      vEdit := TEdit(TWincontrol(Owner).Controls[lp]);
                end;
              end;
              if Assigned(vEdit) then
                vEdit.SetFocus;
           end;
         end;
      end;
    end;
      

  2.   

    我觉得那样不好。如果只是TEDIT的话,为什么不直接用TEDIT类型呢?property Link: String read FLink write SetLinkValue;如果用TEDIT类型,还可以在设计时直接在下接框中选。也不用判断得要死。
      

  3.   

    vEdit:=FindComponent(FLink) as TEdit;   //有问题
    vEdit.SetFocus;修改为:
    vEdit:=TWincontrol(Owner).FindComponent(FLink) as TEdit;   //有问题
    if vEdit<>nil then vEdit.SetFocus;
    你错误的原因是因为当前的FindComponent是该TEdit的方法,而不是相应的Form的方法.