StringGrid单元格点击绑定控件后显示右键菜单

正常点击后出现动态创建的控件,会阻止右键菜单,需要以下处理
procedure Tfz_lab_ivf_sjpt.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  t:Boolean;
  r,c:Integer;  //stringgrid右键点击的单元格
begin
  t := True;
  r := 0;
  c := 0;
  if (Button = mbRight) then
  begin
    StringGrid1.MouseToCell(X, Y, c, r);
    //防止点击表头出现菜单传值错误,右键点击后选中当前单元格
    if (r > 0) and (c > 0) then
    begin
      StringGrid1.Row := r;
      StringGrid1.Col := c;
    end;
  end;
  //如果按鼠标左键则出现控件
  if (Button = mbLeft) then
  begin
    StringGrid1SelectCell(Sender, StringGrid1.Col, StringGrid1.Row, t);
  end;
end;

右键菜单增加一行,第一列为序号

  StringGrid1.RowCount := StringGrid1.RowCount + 1;
  StringGrid1.Cells[1, StringGrid1.RowCount - 1] := IntToStr(StringGrid1.RowCount -1);
  SendMessage(StringGrid1.Handle,WM_VSCROLL,SB_BOTTOM,0);  //滚动条滚动到最后

删除一行,重新修正序号

procedure Tfz_lab_ivf_sjpt.N2Click(Sender: TObject);
var
  i_row,j_col:integer;
begin
  if StringGrid1.RowCount <= 1 then
    Exit;
  StringGrid1.Rows[StringGrid1.row].Clear;
  for j_col := 0 to StringGrid1.colcount do
  begin
    for i_row := StringGrid1.Row to StringGrid1.rowcount - 1 do
    begin
      StringGrid1.cells[j_col, i_row] := StringGrid1.cells[j_col, i_row + 1];
      StringGrid1.Cells[1,i_row]:=IntToStr(i_row);
    end;
  end;
  StringGrid1.RowCount := StringGrid1.RowCount - 1;
  StringGrid1.Refresh;
end;

 

 

posted @ 2022-06-01 11:05  liessay  阅读(135)  评论(0编辑  收藏  举报