Delphi-PRAXiS
Seite 1 von 3  1 23      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi StringGrid-Komponente mit Checkbox (https://www.delphipraxis.net/89595-stringgrid-komponente-mit-checkbox.html)

BillieJoe90 2. Apr 2007 23:14


StringGrid-Komponente mit Checkbox
 
Hallo,
ich suche eine StringGrid-Komponente, in der eine Checkbox vorhanden ist, die IMMER sichtbar ist. Bisher habe ich nur eine kostenlose Komponente gefunden, bei der man erst im "Editier-Modus" einer Zelle die Checkbox sieht, und eine kostenpflichtige, die meinen Wunsch erfüllt. Aber 75$ sind mir zu viel Geld für dieses eine Feature.
Kennt jemand eine kostenlose StringGrid-Komponente, die das unterstützt?

Danke schonmal!

Johannes

Hansa 2. Apr 2007 23:17

Re: StringGrid-Komponente mit Checkbox
 
Wieso schreibst Du die nicht selber ?

BillieJoe90 2. Apr 2007 23:27

Re: StringGrid-Komponente mit Checkbox
 
Zitat:

Zitat von Hansa
Wieso schreibst Du die nicht selber ?

:lol:




weil ich das nicht kann...

Aber wie schwer wäre sowas denn? Ist das das richtige für den Anfang der Komponenten-Programmierung? Überlegt habe ich es sogarschonmal, nur hätteich keine Ahnung, wie ich da überhaupt anfangen sollte... Irgendwie halt die Methode überschreiben, in der er die Zellen zeichnet :gruebel:

Hansa 2. Apr 2007 23:33

Re: StringGrid-Komponente mit Checkbox
 
"Überschreiben" ist eine Binsenweisheit. Der Rest wird allerdings schon gebraucht. Stichwort : OnDrawCell. Der auszufüllende Source dürfte gleich kommen.

Hobby-Programmierer 3. Apr 2007 05:18

Re: StringGrid-Komponente mit Checkbox
 
Moin ...,
bei einem StringGrid bedeutet das ein nicht unerheblicher Aufwand. Wie Hansa schon schrieb, musst du dich dabei um alles selber kümmern. CheckBox erstellen, ausrichten, freigeben usw.!
Versuche lieber von TListView abzuleiten.
Wenn du es trotz alledem versuchen möchtest, hier eine kleine unvollständige Vorlage wie ich es mal gemacht hatte. Für eine Komponente musst du das natürlich anpassen!

Delphi-Quellcode:
procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin
  with StringGrid1 do begin
    Cells[0,0]:= 'CheckBox';
    for i:= 1 to ColCount do Cells[i,0]:= 'Element '+ IntToStr(i);
    for i:= 1 to RowCount do Cells[1,i]:= IntToStr(i);
  end;

  AddCheckBox(0,1);
  AddCheckBox(1,2);
end;

procedure TForm1.MyCheckBoxClick(Sender: TObject);
begin
  ShowMessage('Click!');
end;

procedure TForm1.AddCheckBox(iCol, iRow: Integer);
var NewCheckBox: TCheckBox;
begin
  with StringGrid1 do begin
    if Objects[iCol,iRow] = nil then begin // wenn noch kein Objekt der Zelle zugew. wurde, dann ->
      NewCheckBox:= TCheckBox.Create(Application); // Objekt TCheckBox erstellen
      NewCheckBox.Parent:= Parent; // Eigenschaften zuweisen
      NewCheckBox.Color:= Color;
//      NewCheckBox.Caption:= 'MyCB';
      NewCheckBox.OnClick:= MyCheckBoxClick; // Ereignis zuweisen
      Objects[iCol,iRow]:= NewCheckBox; // CheckBox sagen zu welcher Zelle sie gehört
      Invalidate; // Neuzeichnen erzwingen
    end;
  end;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var NewCheckBox: TCheckBox;
begin
  if not (gdFixed in State) then begin // FixedCell ?
    NewCheckBox:= (StringGrid1.Objects[ACol, ARow] as TCheckBox);

    if NewCheckBox <> nil then begin // wenn unsere CheckBox existiert
      NewCheckBox.Left:= Rect.Left +2; // plazieren wir sie innerhalb der Zelle
      NewCheckBox.Top:= Rect.Top +2;
      NewCheckBox.Width:= (Rect.Right - Rect.Left);
      NewCheckBox.Height:= (Rect.Bottom - Rect.Top);
    end;

  end;
end;
LG Mario

Blackheart 3. Apr 2007 08:12

Re: StringGrid-Komponente mit Checkbox
 
Schau mal hier, musst Du nur umstellen auf CheckBox vieleicht klappt das ja.
http://www.s170867368.online.de/delphi/grdbtn.php

Schaedel 3. Apr 2007 08:31

Re: StringGrid-Komponente mit Checkbox
 
Hi,

hier ein Beispiel wie du in eine bestimmte Col fortlaufend Checkboxen setzt....

Delphi-Quellcode:
DrawCell(Sender: TObject; ACol,ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  AktStr : string;
  SmallRect : TRect;
begin
  AktStr := trim(Grid.Cells[ACol, ARow]);
  if (AktStr = '0') then
    AktStr:='';
  if ((ACol=ChkCol1) or (ACol=ChkCol2)) and (ARow>=SGSndDat.FixedRows) then begin
    Grid.Canvas.FillRect(Rect);

    SmallRect:=Rect;
    SmallRect.Top:=Rect.Top + (Rect.Bottom - Rect.Top) div 2 - 7;
    SmallRect.Bottom:=SmallRect.Top + 15;

    if AktStr <> '' then begin
      DrawFrameControl(SGSndDat.Canvas.Handle,SmallRect,DFC_Button,DFCS_BUTTONCHECK or DFCS_CHECKED);
    end else begin
      DrawFrameControl(SGSndDat.Canvas.Handle,SmallRect,DFC_Button,DFCS_BUTTONCHECK);
    end;
  end else begin
    inherited;
  end;
end;
Wenn irgendetwas in der Cell steht ist die Checkbox checked!

Hansa 3. Apr 2007 20:27

Re: StringGrid-Komponente mit Checkbox
 
Das ist nicht gantz trivial, aber es geht. Aber da gibts einige Tricks mehr. Zunächst mal muss der Stringgrid-Zelleninhalt gesetzt werden. Um eben erst unterscheiden zu können, ob die Box gechecked ist, oder nicht. Dieser Wert wird dann im OnDrawCell ausgewertet und je nach Lage wird zunächst dann ein Rechteck gezeichnet. Dieses Rechteck wird dann eben gecheckt oder nicht.

Delphi-Quellcode:
        if ACol in CheckBoxCols then begin
// 0 für Unchecked, 1 für checked, 2 für nur anzeigen
          if StrToInt (sgArtNr.Cells [ACol,ARow]) in [1,2] then begin
            InflateRect(Rect,-1,-1);
            DrawState := ISChecked[StrToInt (sgArtNr.Cells [ACol,ARow]) = 1];
// ^ die 0/1, die aus dem DB-Original in sgArtNr steht beeinflusst, ob die
// CheckBox gecheckt ist ! D.h. sie wird im OnDrawCell je nach Wert gezeichnet !
            sgArtNr.Canvas.FillRect(Rect);// hinter Cells lieg. 0/1/2, aber das soll man nicht sehen
            DrawFrameControl(sgArtNr.Canvas.Handle,Rect,DFC_BUTTON,DrawState);
          end
          else begin  // Checkbox nicht anzeigen !!
            InflateRect(Rect,-1,-1);
            Canvas.FillRect(Rect);
          end;
Der Haupttrick ist der, dass die Stringgrid-Zelle tatsächlich nur aus 0/1/2 besteht, aber die Darstellung als CheckBox so geht. Und das wird quasi überschrieben. Aber nicht im Sinne von OOP, sondern es geht um den Bildschirm, bzw. die StringGrid-Zelle.

Hobby-Programmierer 4. Apr 2007 09:00

Re: StringGrid-Komponente mit Checkbox
 
Liste der Anhänge anzeigen (Anzahl: 1)
Moin ...,
@Schädel & Hansa: genialer Vorschlag von euch :thumb:
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 = 'true' then    // Checked
      result:= DFCS_BUTTONCHECK or DFCS_CHECKED
     else if Value = 'false' then // 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] = 'true' then // Checked
          Cells[iCol, iRow]:= 'false'
         else if Cells[iCol, iRow] = 'false' then // not Checked
          Cells[iCol, iRow]:= 'true';
      end;  
    end;
end;
LG Mario

wschrabi 10. Apr 2017 17:11

AW: StringGrid-Komponente mit Checkbox
 
Liste der Anhänge anzeigen (Anzahl: 1)
hallo freunde,
danke für den tip hier.
Doch mein Probleme sind in meinem Prg 2:

1) Zeichne nur alle 2. Rows eine Checkbox. (rote Pfeile)
EDIT: Lösung zu 1 einfach mit mod einschränken
Delphi-Quellcode:
begin
  with TStringGrid(Sender) do
    if ((Arow mod 2)=0) and ((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;




2) Kann man zu jeder row in Col=0 die zweite nachfolgende Row zusammenfassen, so daß beide Zeilen in einer Cell drin sind. (grün Feile)

Mein Code:
Delphi-Quellcode:
unit DelphiForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids;

type
  TForm2 = class(TForm)
    ButtonQMCcalc: TButton;
    Memo1: TMemo;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    Label1: TLabel;
    Edit1: TEdit;
    Memo2: TMemo;
    ButtonTTCreate: TButton;
    StringGrid1: TStringGrid;
    procedure ButtonQMCcalcClick(Sender: TObject);
    procedure ButtonTTCreateClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
   procedure Split(Delimiter: Char; Str: string; ListOfStrings: TStrings) ;
   
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

uses
  DelphiAbstractClass;

var
  CPPClass : TAbstractClass;
  Editing: Boolean;
 

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

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  CPPClass.Free;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  CPPClass := CreateCppDescendant;
   Editing:=true;
   
end;

procedure TForm2.ButtonTTCreateClick(Sender: TObject);
var
  TTTOutPutList: TStringList;
  TTTOutPutListmNEG: TStringList;                
  QMC_TTT: Ansistring;              
  k: Integer;
 
begin
     try
       // if I make the CPPCLass locally then the prg crashes
       // at the second click on Create-True-Table
       // whithout the routine in CPPClass.cpp
       //  char* outputTerm(char* pcTTT, int bitfield, int mask, int num)
       // it works fine.
       // Workarount, make CPPCLass global and call Create of this Class
       // at the beginning only once.
       //CPPClass := CreateCppDescendant;
   
       Memo2.Clear;
       memo2.Lines.Add('Input TrueTable:');
       
       Stringgrid1.Cells[0,0]:='Term';
       Stringgrid1.Cells[1,0]:='Checked=1 Unchecked=0';
       
       
       TTTOutPutListmNeg := TStringList.Create;
       TTTOutPutList := TStringList.Create;
       CPPClass.MTCount:=strtoint(Edit1.text);

       if checkbox1.checked then CPPClass.Dummy := 1 else CPPClass.Dummy:=0;
 
       CPPClass.DoTTable;       //DOES NOT WORK! Even not for dummy
       QMC_TTT:=(CPPClass.TTT);
       //QMC_TTT:='  ;xyz#'; //(CPPClass.TTT);
       
       //Memo2.Lines.Add(Format('RUN-Nummer: %d',[i]));
       Split('#', QMC_TTT , TTTOutPutListmNEG) ;
       
         for k := 0 to TTTOUtputlistmNEG.count-2 do
         begin
          Split(';', TTTOUtputlistmNEG[k] , TTTOutPutList) ;
          Memo2.Lines.Add(TTTOutPutList[0]);
          Memo2.Lines.Add(TTTOutPutList[1]);
          Stringgrid1.Cells[0,1+(k*2)]:=TTTOutPutList[0];
          Stringgrid1.Cells[0,1+(k*2+1)]:=TTTOutPutList[1];
          Stringgrid1.Cells[1,1+(k*2+1)]:='true'; // Checkbox state in col 1
          stringgrid1.RowCount:=1+(k*2+1);
         
         
         end;
     finally
       //CPPClass.Free;
       TTTOutPutList.Free;
       TTTOutPutListmNeg.Free;
  end;

end;

procedure Tform2.Split(Delimiter: Char; Str: string; ListOfStrings: TStrings) ;
begin
   ListOfStrings.Clear;
   ListOfStrings.Delimiter      := Delimiter;
   ListOfStrings.StrictDelimiter := True; // Requires D2006 or newer.
   ListOfStrings.DelimitedText  := Str;
end;




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

  function CheckBox(Value: String): Cardinal;
  begin
    result:= DFCS_INACTIVE; // no Entry
    if Value = 'true' then // Checked
      result:= DFCS_BUTTONCHECK or DFCS_CHECKED
     else if Value = 'false' then // 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 TForm2.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] = 'true' then // Checked
          Cells[iCol, iRow]:= 'false'
         else if Cells[iCol, iRow] = 'false' then // not Checked
          Cells[iCol, iRow]:= 'true';
      end;
    end;
end;


procedure TForm2.ButtonQMCcalcClick(Sender: TObject);
var
  OutPutList: TStringList;
  QMC_RESULT: Ansistring;
  maxrun: integer;
  I: Integer;

begin
  if checkbox2.Checked then maxrun:=300 else maxrun:=1;

  for I := 0 to maxrun do
     begin
     
     try
       // if I make the CPPCLass locally then the prg crashes
       // at the second click on Create-True-Table
       // whithout the routine in CPPClass.cpp
       //  char* outputTerm(char* pcTTT, int bitfield, int mask, int num)
       // it works fine.
       // Workarount, make CPPCLass global and call Create of this Class
       // at the beginning only once.
       //CPPClass := CreateCppDescendant;
   

       OutPutList := TStringList.Create;
       CPPCLass.MTCount:=strtoint(Edit1.text);
 
       if checkbox1.checked then CPPClass.Dummy := 1 else CPPCLass.Dummy:=0;
       
       CPPClass.DoSomething;
       QMC_Result:=(CPPCLass.text);
       Split(':', QMC_Result , OutPutList) ;
       Memo1.Lines.Add(Format('RUN-Nummer: %d',[i]));
       Memo1.Lines.Add(OutPutList[0]);
       Memo1.Lines.Add(OutPutList[1]);
       
     finally
       //CPPClass.Free;
       OutPutList.Free;
   
   
     end;
  end;
end;

end.


Alle Zeitangaben in WEZ +1. Es ist jetzt 12:11 Uhr.
Seite 1 von 3  1 23      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz