用stringgrid,我想前面4列不能做修改,后面列可以修改
所以就在onselectsell事件中写
if stringgrid.col<4 then
  stringgrid.options:=stringgrid.options-[onediting]
else
  stringgrid.options:=stringgrid.options+[onediting];就是想直接判断当前在哪列里,然后修改‘可编辑’的属性
但在onselectsell事件中太慢了
是点了后才判断前面这个要写在哪个事件里比较好,正好可以点下去就可以判断了 ???

解决方案 »

  1.   

    我是在onkeydown事件里判断。。
    严格一点,通过鼠标右键跳出菜单里的复制、粘贴也要禁止
      

  2.   

    可以自已继承一个Grid:
    unit MyGrid;interfaceuses
      Classes, Controls, Grids;type
      TMyGrid = class(TStringGrid)
      private
        function CalcCoordFromPoint(X, Y: Integer;
          const DrawInfo: TGridDrawInfo): TGridCoord;
      protected
        procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
          X, Y: Integer); override;
      public
      end;implementation{ TMyGrid }procedure TMyGrid.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
      Y: Integer);
    var
      CellHit: TGridCoord;
      DrawInfo: TGridDrawInfo;
      MoveDrawn: Boolean;
    begin
      CalcDrawInfo(DrawInfo);
      CellHit := CalcCoordFromPoint(X, Y, DrawInfo);
      if CellHit.X < 4 then
        Options := Options - [goEditing];  inherited;  if (Button = mbLeft) and (CellHit.X >= 4) then
      begin
        Options := Options + [goEditing];
        ShowEditor;
      end;
    end;function TMyGrid.CalcCoordFromPoint(X, Y: Integer;
      const DrawInfo: TGridDrawInfo): TGridCoord;  function DoCalc(const AxisInfo: TGridAxisDrawInfo; N: Integer): Integer;
      var
        I, Start, Stop: Longint;
        Line: Integer;
      begin
        with AxisInfo do
        begin
          if N < FixedBoundary then
          begin
            Start := 0;
            Stop :=  FixedCellCount - 1;
            Line := 0;
          end
          else
          begin
            Start := FirstGridCell;
            Stop := GridCellCount - 1;
            Line := FixedBoundary;
          end;
          Result := -1;
          for I := Start to Stop do
          begin
            Inc(Line, AxisInfo.GetExtent(I) + EffectiveLineWidth);
            if N < Line then
            begin
              Result := I;
              Exit;
            end;
          end;
        end;
      end;  function DoCalcRightToLeft(const AxisInfo: TGridAxisDrawInfo; N: Integer): Integer;
      var
        I, Start, Stop: Longint;
        Line: Integer;
      begin
        N := ClientWidth - N;
        with AxisInfo do
        begin
          if N < FixedBoundary then
          begin
            Start := 0;
            Stop :=  FixedCellCount - 1;
            Line := ClientWidth;
          end
          else
          begin
            Start := FirstGridCell;
            Stop := GridCellCount - 1;
            Line := FixedBoundary;
          end;
          Result := -1;
          for I := Start to Stop do
          begin
            Inc(Line, AxisInfo.GetExtent(I) + EffectiveLineWidth);
            if N < Line then
            begin
              Result := I;
              Exit;
            end;
          end;
        end;
      end;begin
      if not UseRightToLeftAlignment then
        Result.X := DoCalc(DrawInfo.Horz, X)
      else
        Result.X := DoCalcRightToLeft(DrawInfo.Horz, X);
      Result.Y := DoCalc(DrawInfo.Vert, Y);
    end;end.
    然后动态建立一个Grid:
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      FGrid := TMyGrid.Create(Self);
      FGrid.Parent := Self;
      FGrid.ColCount := 10;
      FGrid.Width := 400;
      FGrid.Height := 200;
    end;
      

  3.   

    在 OnKeyPress事件中if stringgrid1.Col<4 then
        key:=#0;
      

  4.   

    谢谢了
    在onkeydown里就可以实现了
    如果写在onkeypress里,很奇怪,如果我点可编辑的列(第4或第5列),然后再点不能编辑的列(0-3列),
    点的那个列就可以输入了,不知道怎么回事重写有点麻烦,就没重写了