使用delphi在TStringGrid中通过鼠标单击删除选定行

使用delphi在TStringGrid中通过鼠标单击删除选定行,delphi,tstringgrid,Delphi,Tstringgrid,我不确定如何捕获鼠标单击所选的行,然后在delphi的stringGrid中按下按钮删除所选行 procedure DeleteRow(Grid: TStringGrid; ARow: Integer); var i: Integer; begin for i := ARow to Grid.RowCount - 2 do Grid.Rows[i].Assign(Grid.Rows[i + 1]); Grid.RowCount := Grid.RowCount - 1; en

我不确定如何捕获鼠标单击所选的行,然后在delphi的stringGrid中按下按钮删除所选行

procedure DeleteRow(Grid: TStringGrid; ARow: Integer);
var
  i: Integer;
begin
  for i := ARow to Grid.RowCount - 2 do
    Grid.Rows[i].Assign(Grid.Rows[i + 1]);
  Grid.RowCount := Grid.RowCount - 1;
end;

procedure TManageUsersForm.RemoveRowButtonClick(Sender: TObject);
var
  Recordposition : integer;
begin
  UserStringGrid.Options := UserStringGrid.Options + [goEditing];
  UserStringGrid.Options := UserStringGrid.Options + [goRowSelect];
end;
因此,第一个过程是删除一行,第二个过程确保当用户单击一个单元格时,整行高亮显示,而不仅仅是这一个单元格

鼠标点击是最重要的部分


谢谢:)

我想您可能遇到的问题是计算出用户单击了哪个网格行。一种方法是:

procedure TForm1.StringGrid1Click(Sender: TObject);
var
  StringGrid : TStringGrid;
  Row : Integer;
  GridRect : TGridRect;
begin
  // The Sender argument to StringGrid1Click is actually the StringGrid itself,
  // and the following "as" cast lets you assign it to the StringGrid local variable
  // in a "type-safe" way, and access its properties and methods via the temporary variable
  StringGrid := Sender as TStringGrid;

  // Now we can retrieve the use selection
  GridRect := StringGrid.Selection;

  // and hence the related GridRect
  // btw, the value returned for Row automatically takes account of
  // the number of FixedRows, if any, of the grid
  Row := GridRect.Top;

  Caption := IntToStr(Row);
  { ...}
end;
请参阅有关TGridRect的OLH


希望以上内容足以让你走下去——很明显你自己已经走了大部分路。或者,你可以尝试另一个答案中建议的方法,它更“直接”,但这种方法作为“如何”可能更有指导意义。您的选择…

我想您可能遇到的问题是确定用户单击了哪个网格行。一种方法是:

procedure TForm1.StringGrid1Click(Sender: TObject);
var
  StringGrid : TStringGrid;
  Row : Integer;
  GridRect : TGridRect;
begin
  // The Sender argument to StringGrid1Click is actually the StringGrid itself,
  // and the following "as" cast lets you assign it to the StringGrid local variable
  // in a "type-safe" way, and access its properties and methods via the temporary variable
  StringGrid := Sender as TStringGrid;

  // Now we can retrieve the use selection
  GridRect := StringGrid.Selection;

  // and hence the related GridRect
  // btw, the value returned for Row automatically takes account of
  // the number of FixedRows, if any, of the grid
  Row := GridRect.Top;

  Caption := IntToStr(Row);
  { ...}
end;
请参阅有关TGridRect的OLH


希望以上内容足以让你走下去——很明显你自己已经走了大部分路。或者,你可以尝试另一个答案中建议的方法,它更“直接”,但这种方法作为“如何”可能更有指导意义。你的选择…

鼠标点击不是最重要的部分。用户可以通过键盘或鼠标选择一行,这无关紧要,您只需要删除当前行。在鼠标单击或其他情况下,您可以通过
获取当前行

procedure DeleteCurrentRow(Grid: TStringGrid);
var
  i: Integer;
begin
  for i := Grid.Row to Grid.RowCount - 2 do
    Grid.Rows[i].Assign(Grid.Rows[i + 1]);
  Grid.RowCount := Grid.RowCount - 1;
end;
像这样称呼它

DeleteCurrentRow(UserStringGrid);

鼠标点击不是最重要的部分。用户可以通过键盘或鼠标选择一行,这无关紧要,您只需要删除当前行。在鼠标单击或其他情况下,您可以通过
获取当前行

procedure DeleteCurrentRow(Grid: TStringGrid);
var
  i: Integer;
begin
  for i := Grid.Row to Grid.RowCount - 2 do
    Grid.Rows[i].Assign(Grid.Rows[i + 1]);
  Grid.RowCount := Grid.RowCount - 1;
end;
像这样称呼它

DeleteCurrentRow(UserStringGrid);

嗯,有
OnClick
event哦,我不知道如何使用它:(你强调鼠标很有趣。你知道键盘可以用来进行选择吗?是的。但我认为用户使用鼠标点击会更容易。哦,有
OnClick
event哦,我不知道如何使用它:(你强调鼠标很有意思。你知道键盘可以用来选择吗?是的。但我认为用户使用鼠标点击会更容易。谢谢你的答案,它很有帮助:)。谢谢你的答案,它很有帮助:)。