SG不连续多选 草稿

本文介绍了在Delphi中使用TStringGrid控件实现按Ctrl键进行多行选择的方法。通过在MouseDown事件中判断ShiftState是否包含ssCtrl,并设置Options属性来开启或关闭多选功能。同时,提供了获取已选择单元格的示例代码,遍历Selection区域并设置单元格内容。
摘要由CSDN通过智能技术生成

procedure TForm1.StringGrid1DrawCell(Sender: TObject. ACol, ARow: Integer. Rect: TRect. State: TGridDrawState).
begin
  with stringgrid1 do
  begin
  if cells[0,arow] = checked then
  begin
  canvas.brush.color := clhighlight;
  canvas.font.color := clhighlighttext;
  end
  else
  Canvas.Brush.Color := clWindow;
  Canvas.Font.Color := clWindowText;
  end
  Canvas.FillRect(Rect);
  TextRect(Rect, Rect.Left 2, Rect.Top 2, Cells[ACol, ARow]);
  end;
end;

procedure 1.StringGrid1SelectCell(Sender: TObject. ACol, ARow: Integer. var CanSelect: Boolean);
begin
with 1 do
begin if ARow = Row then Exit;
 if Cells[0, ARow] = Checked then
 Cells[0, ARow] := UnChecked
 else
 Cells[0, ARow] := Checked
 end;
end;  


如何在TStringGrid控件中按ctrl键选择多行啊
推荐解答:
在MouseDown事件写入以下代码:
procedure TForm1.StringGrid1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if ssCtrl in Shift then
  StringGrid1.Options := [goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine,goRangeSelect,goRowSelect]
  else
    StringGrid1.Options := [goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine,goRowSelect] ;
end; 

 

 

delphi中StringGrid打开goRangeSelect可以多选,如何知道选择了那些Cell?
procedure TForm1.Button1Click(Sender: TObject);
var
i,j:integer;
begin
for i:=Stringgrid1.Selection.left to Stringgrid1.Selection.Right do
begin
for j:=Stringgrid1.Selection.Top to Stringgrid1.Selection.Bottom do
Stringgrid1.cells[i,j]:='ok';
end;
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值