怎麽在數據集編輯狀態下按F9就複製一條紀錄到數組裏面,然後按F10就把數組裏面的一條紀錄粘貼到當前DBGrid!(注:數組里只存放一條紀錄,重復按F9只取最後一條)

解决方案 »

  1.   

    TApplicationEvents 组件
    里面的OnShortCut事件
      

  2.   

    procedure TForm.DBGridKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin
      
      if Key = VK_F9 then
      begin  end;  if Key = VK_F10 then
      begin
        if ADOQuery1.State <> dsInsert then
        begin
          ADOQuery1.Append;    end;  end;
    end;怎麽實現?
      

  3.   

    可以使用ActionList,新建两个Action,设置快捷键为F9和F10,然后在这两个Action的事件过程中写入代码实现你想要的功能;ActionList为非可视控件,以后如果需要加按钮或菜单也方便;
      

  4.   

    能不能在DBGridKeyDown裏面實現??高手幫我填充一下下面的代碼呀!
    procedure TForm.DBGridKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin
      
      if Key = VK_F9 then
      begin
         複製一條紀錄
        存放在數組裏面
        end;  if Key = VK_F10 then
      begin
        if ADOQuery1.State <> dsInsert then
        begin
          ADOQuery1.Append;
        粘貼到當前DBGrid裏面
        end;  end;
    end;
      

  5.   

    在当前Form类中添加private变量:
        CopyArray: array of Variant;
    代码:
    procedure TForm1.actCopyExecute(Sender: TObject);//复制Action代码
    var
      I: Integer;
    begin
      inherited;
      SetLength(CopyArray,ADOQuery1.FieldCount);
      for I := 0 to ADOQuery1.FieldCount - 1 do
        CopyArray[I] := ADOQuery1.Fields[I].Value;
    end;procedure TForm1.actPasteExecute(Sender: TObject);//粘贴Action代码
    var
      I: Integer;
    begin
      inherited;
      if CopyArray = nil then Exit;
      ADOQuery1.Append;
      for I := 0 to ADOQuery1.FieldCount - 1 do
        if not ADOQuery1.Fields[I].ReadOnly then  //此语句防止Query里存在只读列而报错
        ADOQuery1.Fields[I].Value := CopyArray[I];
      SetLength(CopyArray,0);
      CopyArray := nil;
    end;
    大概就这样,试试吧
      

  6.   

    在DBGridKeyDown裏面實現 也可以
    只是感觉放DBGridKeyDown里显得有点乱,除非必要,还是独立出来比较好
    而使用ActionList能清晰一些
    比如说想增强一下界面友好度,可以在DBGrid添加弹出菜单,引用这两个Action,
    而且当焦点不在DBGrid上也可以执行
      

  7.   

    按键事件里不要执行太多的代码
    1、用菜单项或右键菜单形式定义快捷键,你可以不显示出来菜单
    2、用ActionList
    然后在菜单项或ActionList里写代码实现你要的功能。
      

  8.   

    To-> unknwn()
    好像在粘貼時釋放字段時。其類型好像會報錯
      

  9.   

    什么错误信息?贴出来看看吧
    我给出的那段代码在发帖之前已经测试通过。
    注测试环境:Delphi6.0 Update Pack2
      

  10.   

    message"list out of bounds(22)"
    procedure TVoucherSetup01Form.DBGridKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    var
      CopyArray:array of Variant;
      i: integer;
    begin
     inherited;
      if Key = VK_F9 then
      begin
        SetLength(CopyArray,qyDetails.FieldCount);
        for I := 0 to qyDetails.FieldCount - 1 do
         CopyArray[I] := qyDetails.Fields[I].Value;
        case qyDetails.Fields[i].DataType of
          ftString:
             CopyArray[I] := qyDetails.Fields[I].AsString ;
          ftInteger:
            CopyArray[I]:= qyDetails.Fields[I].AsInteger ;
          ftBoolean:
            CopyArray[I] := qyDetails.Fields[I].AsBoolean ;
          ftFloat:
             CopyArray[I] := qyDetails.Fields[I].AsFloat ;
          ftDateTime:
             CopyArray[I]:=qyDetails.Fields[I].AsDateTime;
        end;
        beep();
      end;  if Key = VK_F10 then
      begin
        //if qyDetails.State <> dsInsert then
        if EditMode in [stAdd,stEdit] then
        begin
         if CopyArray = nil then Exit;
          qyDetails.Append;
          for I := 0 to qyDetails.FieldCount - 1 do
          begin
            if not qyDetails.Fields[I].ReadOnly then  
              begin
              case  qyDetails.Fields[i].DataType of
                ftString:
                  qyDetails.Fields[I].AsString := CopyArray[I];
                ftInteger:
                  qyDetails.Fields[I].AsInteger := Integer(CopyArray[I]);
                ftBoolean:
                  qyDetails.Fields[I].AsBoolean := CopyArray[I];
                ftFloat:
                  qyDetails.Fields[I].AsFloat := CopyArray[I];
                ftDateTime:
                  qyDetails.Fields[I].AsDateTime := CopyArray[I];
              end;
              dbgrid.RefreshDisplay;
            end;
          end;
        end;
      end;
    end;
      

  11.   

    为什么要那样写呢?
    >>在当前Form类中添加private变量:
    >>    CopyArray: array of Variant;
    之前我已经提过,CopyArray应该在Form类的private区定义;你在过程内部定义的CopyArray仅仅是局部变量;在按下F9拷贝的时候是进行了赋值,不过过程结束后CopyArray就马上被释放了;当你按下F10时会重新执行这个过程,此时的CopyArray是新创建的、未被赋值的变量,所以报错;
    另外,除非特殊情况下必须使用AsString这种赋值方式,否则用Value赋值即可
    参考代码(在之前基础上仅做了少量修改):
    procedure TForm1.actCopyExecute(Sender: TObject);
    var
      I: Integer;
    begin
      inherited;
      SetLength(CopyArray,qyDetails.FieldCount);
      for I := 0 to qyDetails.FieldCount - 1 do
      begin
        case qyDetails.Fields[I].DataType of
          ftString: CopyArray[I] := qyDetails.Fields[I].AsString;
          ftInteger: CopyArray[I] := qyDetails.Fields[I].AsInteger;
          //....
        else
          CopyArray[I] := qyDetails.Fields[I].Value;
        end;
      end;
    end;procedure TForm1.actPasteExecute(Sender: TObject);
    var
      I: Integer;
    begin
      inherited;
      //if ... then Exit //&Egrave;&ocirc;·&Ccedil;±à&frac14;&shy;×&acute;&Igrave;&not;&Ocirc;ò&Iacute;&Euml;&sup3;&ouml;//判断是否在编辑状态,自己加代码
      if CopyArray = nil then Exit; //&Egrave;&ocirc;&Icirc;&acute;&cedil;&sup3;&Ouml;&micro;&pound;&not;&Ocirc;ò&Iacute;&Euml;&sup3;&ouml;&Otilde;&sup3;&Igrave;ù
      qyDetails.Append;
      for I := 0 to qyDetails.FieldCount - 1 do
      begin
        if qyDetails.Fields[I].ReadOnly then Continue //±&Uuml;&Atilde;&acirc;&Oacute;&ouml;&micro;&frac12;&sup2;&raquo;&iquest;&Eacute;&ETH;&THORN;&cedil;&Auml;&micro;&Auml;&Aacute;&ETH;±¨&acute;í
        case qyDetails.Fields[I].DataType of
          ftString: qyDetails.Fields[I].AsString := CopyArray[I];
          //....
        else
          qyDetails.Fields[I].Value := CopyArray[I];
        end;
      end;
      SetLength(CopyArray,0);
      CopyArray := nil;
    end;
      

  12.   

    没法编辑回复?
    unknwn() 
    大哥是不是没一个字段都要判断其的字段类型?
      

  13.   

    是需要每个字段都判断字段类型,而且你给出的代码中已经这样做了。
    现在还哪儿有问题吗?
    关键是CopyArray应该在Form类的private区定义,而不能定义为局部变量,别的也没什么了。