Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 10-06-2003
seduerey seduerey is offline
Miembro
 
Registrado: jun 2003
Posts: 60
Poder: 21
seduerey Va por buen camino
Unhappy ComboBoxes en StringGrid

Hola, tengo una duda:

Quiero saber si se puede poner un combobox en una celda de una StringGrid. Creo que no, pero me gustaría saberlo, si bien ahí o bien en las DrawGrid.

Y si se puede, agradecería saber como se hace, si un link a un pequeño tutorial o lo que sea.

También aplicable a CheckBox, etc.... cualquier cosa que no sea una string.

Gracias de antemano
Responder Con Cita
  #2  
Antiguo 10-06-2003
Avatar de marcoszorrilla
marcoszorrilla marcoszorrilla is offline
Capo
 
Registrado: may 2003
Ubicación: Cantabria - España
Posts: 11.221
Poder: 10
marcoszorrilla Va por buen camino
Código:
unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids;

type
  TfrmMain = class(TForm)
    StringGrid1: TStringGrid;
    cbInplaceComboBox: TComboBox;
    btnClose: TButton;
    MemoDescription: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure cbInplaceComboBoxChange(Sender: TObject);
    procedure StringGrid1SelectCell(Sender: TObject; Col, Row: Integer;
      var CanSelect: Boolean);
    procedure StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure btnCloseClick(Sender: TObject);
  private
    { Private declarations }
    procedure CMDialogKey(var msg: TCMDialogKey); message CM_DIALOGKEY;
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.DFM}

procedure TfrmMain.FormCreate(Sender: TObject);
var i: Integer;
begin
  StringGrid1.DefaultRowHeight := cbInplaceComboBox.Height;
  cbInplaceComboBox.Visible := False;

  StringGrid1.Cells[0, 0] := 'Row No';
  StringGrid1.Cells[1, 0] := 'Values (from Combobox)';
  for i := 1 to StringGrid1.RowCount-1 do
    StringGrid1.Cells[0, i] := IntToStr(i);
end;

procedure TfrmMain.CMDialogKey(var msg: TCMDialogKey);
begin
  if (ActiveControl = cbInplaceComboBox) then
  begin
    if (msg.CharCode = VK_TAB) then
    begin
      //set focus back to the grid and pass the tab key to it
      cbInplaceComboBox.SetFocus;
      cbInplaceComboBox.Perform(WM_KEYDOWN, msg.CharCode, msg.KeyData);
      // swallow this message
      msg.result := 1;
      Exit;
    end;
  end;

  inherited;
end;

procedure TfrmMain.cbInplaceComboBoxChange(Sender: TObject);
var intRow: Integer;
begin
  inherited;

  {Obtener la selección del ComboBox y colocarla en la Rejilla}
  with cbInplaceComboBox do
  begin
    intRow := StringGrid1.Row;
    if (StringGrid1.Col = 2) then
      StringGrid1.Cells[2, intRow] := Items[ItemIndex]
    else
      StringGrid1.Cells[StringGrid1.Col, intRow] := Items[ItemIndex];
    Visible := False;
  end;
  StringGrid1.SetFocus;
end;

procedure TfrmMain.StringGrid1SelectCell(Sender: TObject; Col, Row: Integer;
  var CanSelect: Boolean);
var R: TRect;
begin
  if ((Col = 1) and (Row <> 0)) then
  begin
    {Tamaño y posición del ComboBox para llenar la celdilla}
    R := StringGrid1.CellRect(Col, Row);
    R.Left := R.Left + StringGrid1.Left;
    R.Right := R.Right + StringGrid1.Left;
    R.Top := R.Top + StringGrid1.Top;
    R.Bottom := R.Bottom + StringGrid1.Top;

    {Mostrar el CombBox}
    with cbInplaceComboBox do
    begin
      Left := R.Left + 1;
      Top := R.Top + 1;
      Width := (R.Right + 1) - R.Left;
      Height := (R.Bottom + 1) - R.Top;

      ItemIndex := Items.IndexOf(StringGrid1.Cells[Col, Row]);
      Visible := True;
      SetFocus;
    end;
  end;
  CanSelect := True;
end;

procedure TfrmMain.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
  Rect: TRect; State: TGridDrawState);
const
  AlignFlags: array [TAlignment] of Integer =
     (DT_LEFT or DT_VCENTER or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX,
      DT_RIGHT or DT_VCENTER or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX,
      DT_CENTER or DT_VCENTER or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX);
var s: string;
begin
  inherited;

  with Rect do
  begin
    Left := Left + 2;
    Top := Top + 2;
    Right := Right - 5
  end;

  s := StringGrid1.Cells[Col, Row];

  if (Row = 0) and (Col < StringGrid1.ColCount) then
  begin
    StringGrid1.Canvas.Font.Style := StringGrid1.Canvas.Font.Style + [fsBold];
    StringGrid1.Canvas.Brush.Color := StringGrid1.FixedColor;
    StringGrid1.Canvas.FillRect(Rect);

    DrawText(StringGrid1.Canvas.Handle,
             PChar(s), Length(s),
             Rect, AlignFlags[taCenter]);
  end
  else
    if (Col = 0) and (Row > 0) and (Row < StringGrid1.RowCount) then
    begin
      StringGrid1.Canvas.FillRect(Rect);
      DrawText(StringGrid1.Canvas.Handle,
               PChar(s), Length(s),
               Rect, AlignFlags[taRightJustify]);
    end;
end;

procedure TfrmMain.btnCloseClick(Sender: TObject);
begin
  Close
end;

end.
Saludos.
Responder Con Cita
  #3  
Antiguo 11-06-2003
seduerey seduerey is offline
Miembro
 
Registrado: jun 2003
Posts: 60
Poder: 21
seduerey Va por buen camino
Muchas gracias, era una duda que tenia bastante tiempo
Responder Con Cita
  #4  
Antiguo 12-06-2003
seduerey seduerey is offline
Miembro
 
Registrado: jun 2003
Posts: 60
Poder: 21
seduerey Va por buen camino
Creo que no he hecho las cosas como tu me has indicado del todo bien.... yo quiero que me salga algo como lo de la imagen, pero no lo consigo.




Muchas gracias. Es que da bastante más juego que a secas
Responder Con Cita
  #5  
Antiguo 12-06-2003
Avatar de marcoszorrilla
marcoszorrilla marcoszorrilla is offline
Capo
 
Registrado: may 2003
Ubicación: Cantabria - España
Posts: 11.221
Poder: 10
marcoszorrilla Va por buen camino
Lo que quieres:

Supongo que te falta poder poner un "CheckBox" en la rejilla y poderlo activar y desactivar.

Te adjunto parte de código propio que utilizo para que de las líneas de una factura solamente se sumen las que indique el usuario, en definitiva creo resuelve tu problema que es como mostrar una casilla de verificación "CheckBox", en una rejilla.


Código:
procedure TfrFacturas.GrLineasColEnter(Sender: TObject);
begin
  if GrLineas.Columns [GrLineas.SelectedIndex].
      Field = DmLux.LinfactSumar  then
    DbChkSumar.Visible := True
  else
    DbChkSumar.Visible := False;
end;

procedure TfrFacturas.GrLineasDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
begin
  if (gdFocused in State) and
    (Column.Field = DmLux.LinfactSumar) then
  begin
    DbchkSumar.SetBounds (
      Rect.Left + grLineas.Left + 1,
      Rect.Top + grLineas.Top + 1,
      Rect.Right - Rect.Left,
      Rect.Bottom - Rect.Top);
  end;
end;

procedure TfrFacturas.GrLineasKeyPress(Sender: TObject; var Key: Char);
begin
  if DbChkSumar.Visible and (Ord (Key) > 31) then
  begin
    Key := #0;
    DmLux.Linfact.Edit;
    DbChkSumar.Checked := not
      DbChkSumar.Checked;
    DbChkSumar.Field.AsBoolean :=
      DbChkSumar.Checked;
  end;
Un Saludo.
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro


La franja horaria es GMT +2. Ahora son las 21:39:38.


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
Copyright 1996-2007 Club Delphi