Einzelnen Beitrag anzeigen

Hobby-Programmierer

Registriert seit: 19. Jan 2006
Ort: München
392 Beiträge
 
Delphi XE Starter
 
#9

Re: StringGrid-Komponente mit Checkbox

  Alt 4. Apr 2007, 09:00
Moin ...,
@Schädel & Hansa: genialer Vorschlag von euch
Ich habe ein wenig herumgespielt und herausgekommen ist dies:
Delphi-Quellcode:
var
  Form1: TForm1;
  Editing: Boolean;

Const CheckBoxCols = [1,2]; // Spalte 2 ohne Einträge

implementation
{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin
  for i:= 1 to StringGrid1.RowCount do begin
    StringGrid1.Cells[0,i]:= 'Zeile '+ IntToStr(i);
    StringGrid1.Cells[1,i]:= 'true';
  end;
  Editing:= true; // False = nur Anzeige, true = EditModus
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);

  function CheckBox(Value: String): Cardinal;
  begin
    result:= DFCS_INACTIVE; // no Entry
    if Value = 'truethen // Checked
      result:= DFCS_BUTTONCHECK or DFCS_CHECKED
     else if Value = 'falsethen // not Checked
      result:= DFCS_BUTTONCHECK;
    if not Editing then
      result:= result or DFCS_MONO; // no Editing
  end;

begin
  with TStringGrid(Sender) do
    if (ACol in CheckBoxCols) and not (gdFixed in State) then begin
      Canvas.FillRect(Rect);
      InflateRect(Rect, -4, -4);
      DrawFrameControl(Canvas.Handle, Rect,DFC_Button,
                       CheckBox(Trim(Cells[ACol, ARow])));
    end; // if gdFixed in State
end;

procedure TForm1.StringGrid1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var iCol, iRow: Integer;
begin
  with TStringGrid(Sender) do
    if (Button = mbLeft) and Editing then begin
      MouseToCell(x, y, iCol, iRow);
      if (iCol > 0) and (iRow > 0) then begin
        if Cells[iCol, iRow] = 'truethen // Checked
          Cells[iCol, iRow]:= 'false'
         else if Cells[iCol, iRow] = 'falsethen // not Checked
          Cells[iCol, iRow]:= 'true';
      end;
    end;
end;
LG Mario
Angehängte Dateien
Dateityp: zip stringgrid_mit_checkbox_117.zip (1,8 KB, 139x aufgerufen)
Mario
'Lesen Sie schnell, denn nichts ist beständiger als der Wandel im Internet!'
  Mit Zitat antworten Zitat