【问题标题】:Deleting a selected row via mouse click in TStringGrid using delphi使用delphi在TStringGrid中通过鼠标单击删除选定的行
【发布时间】:2014-11-30 20:08:06
【问题描述】:

我不确定如何捕获 鼠标单击 选择的行,然后按一个按钮删除 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;

所以第一个过程是删除一行,第二个过程确保当用户单击一个单元格时,整行都被突出显示,而不仅仅是那个单元格。

鼠标点击是最重要的部分!

谢谢你:)

【问题讨论】:

  • 好吧,OnClick 事件发生了
  • 哦,我不知道如何使用这个:(
  • 你给鼠标施压很有趣。你知道键盘可以用来做选择吗?
  • 是的。但我认为用户使用鼠标点击会更容易。

标签: delphi tstringgrid


【解决方案1】:

鼠标点击不是最重要的部分。用户可以通过键盘或鼠标选择一行,没关系,你只想删除当前行。在鼠标点击等情况下,可以通过Row获取当前行。

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);

【讨论】:

    【解决方案2】:

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

    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。

    希望以上内容足以让您继续前进 - 显然您自己已经掌握了大部分内容。或者,您可以尝试其他答案中建议的方法,这种方法更“直接”,但作为“如何做”,这种方法可能更具指导性。你的选择...

    【讨论】:

    • 感谢您的回答,它帮助了:)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    相关资源
    最近更新 更多