Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Como seleccionar un rango de celdas con Ctrl+clic de un StringGrid (https://www.clubdelphi.com/foros/showthread.php?t=93021)

wanda 21-04-2018 18:21:59

Como seleccionar un rango de celdas con Ctrl+clic de un StringGrid
 
1 Archivos Adjunto(s)
Hola a todos, tengo un problema que no se como solucionar, e buscando en diferentes foros y no encuentro una solución o alternativa. La cuestiona es la siguiente, tengo un StringGrid y quiero seleccionar un rango de celdas, eso es fácil de hacer, el problema es cuando quiero seleccionar un rango de celdas de diferentes filas.. para explicarme mejor les adjunto la siguiente imagen.

Archivo Adjunto 3666

quiero que al dar clic, en este caso en el numero 3 y al bajar hasta el numero 1 la selección se vea como en la imagen.. Si alguien me puede ayudar se los agradecería mucho

NOTA: uso Delphi 2005

duilioisola 24-04-2018 09:38:15

Los eventos OnMouseDown y OnMouseUp te indicarán donde se presionó el botón del mouse y el estado de CTRL, ALT, SHIFT y qué botón del mouse se ha presionado.
Con esto y las coordenadas podrás averiguar qué celda se ha seleccionado y pintarla según corresponda.

AgustinOrtu 28-04-2018 15:43:08

Revisa la propiedad Selection

Edito:

Ahora me doy cuenta que no te sirve esa propiedad porque tenés selecciones no contiguas. La unica forma que se me ocurre es como comentan arriba, pintando las filas seleccionadas por el usuario. Tendrías que mantener una estructura adicional que indique que el estado de cada fila (seleccionado o no)

Algo más sencillo es utilizar un chechbox embebido en cada fila. Hay ejemplos en el foro. Si te pasas a TListView esto viene resuelto solamente marcando la propiedad Checkboxes a True

ecfisa 28-04-2018 20:56:24

Hola.

Fijate si este ejemplo es similar a lo que buscas hacer:
Código Delphi [-]
...
type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    btCopy: TButton;
    ListBox1: TListBox;
    btClear: TButton;
    procedure FormCreate(Sender: TObject);
    procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure btCopyClick(Sender: TObject);
    procedure btClearClick(Sender: TObject);
  private
    procedure ClearSGSelection;
  public
  end;

var
  Form1: TForm1;

implementation {$R *.dfm}

procedure TForm1.ClearSGSelection;
var
  c: Integer;
  r: Integer;
begin
  for r := StringGrid1.FixedCols to StringGrid1.RowCount-1 do
    for c := StringGrid1.FixedRows to StringGrid1.ColCount-1 do
      StringGrid1.Objects[c,r] := Pointer(0);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  c: Integer;
  r: Integer;
begin
  ClearSGSelection;
  for r := StringGrid1.FixedCols to StringGrid1.RowCount-1 do
    for c := StringGrid1.FixedRows to StringGrid1.ColCount-1 do
      StringGrid1.Cells[c,r] := IntToStr(c+r);
end;

procedure TForm1.StringGrid1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  SG: TStringGrid;
  pt: TPoint;
  CellOnOff : Boolean;
begin
  if ssCtrl in Shift then
  begin
    SG := TStringGrid(Sender);
    SG.MouseToCell(X, Y, pt.X, pt.Y);
    CellOnOff := not Boolean(SG.Objects[pt.X, pt.Y]);
    SG.Objects[pt.X, pt.Y] := Pointer(Integer(CellOnOff));
  end;
end;


procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  SG: TStringGrid;
begin
  SG := Sender as TStringGrid;
  if Boolean(sg.Objects[ACol, ARow]) then
  begin
    SG.Canvas.Brush.Color := clActiveCaption;
    SG.Canvas.Brush.Style := bsSolid;
    SG.Canvas.FillRect(Rect);
    InflateRect(rect, -2, -2);
    sg.Canvas.TextRect(Rect, Rect.left, Rect.top, SG.Cells[acol, arow]);
  end;
end;

procedure TForm1.btCopyClick(Sender: TObject);
var
  c: Integer;
  r: Integer;
begin
  for r := StringGrid1.FixedCols to StringGrid1.RowCount-1 do
    for c := StringGrid1.FixedRows to StringGrid1.ColCount-1 do
      if Boolean(StringGrid1.Objects[c,r]) then
         ListBox1.Items.Add(StringGrid1.Cells[c,r]);
end;

procedure TForm1.btClearClick(Sender: TObject);
begin
  ListBox1.Items.Clear;
  ClearSGSelection;
end;
end.

Salida:


Saludos :)


La franja horaria es GMT +2. Ahora son las 18:17:25.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi