Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Checkboxen in Stringgrid (https://www.delphipraxis.net/189199-checkboxen-stringgrid.html)

michael.cohrs 16. Mai 2016 07:48

Checkboxen in Stringgrid
 
Guten morgen zusammen,

ich habe in eine Stringgrid mit folgender Methode Checkboxen in Column 0 gezeichnet:

Delphi-Quellcode:
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;
Das MouseDown Event sieht folgendermaßen aus:
Delphi-Quellcode:
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] = '1' then // Checked
          Cells[iCol, iRow]:= '0'
         else if Cells[iCol, iRow] = 'false' then // not Checked
          Cells[iCol, iRow]:= 'true';
      end;
    end;
end;
soweit klappt alles hervoragend, mein Problem ist folgendes: Ich möchte beim Aufbau des Programms und beim Einlesen aus einer Datenbank die Checkboxen entstprechend des gelesenen Wertes entweder Checken oder halt nicht. Das gelingt mir leider nicht, nach dem Einlesen und setzen des Wertes reagieren die Checkboxen nicht mehr auf den Click, soll heissen der neue Wert wird nicht gezeichnet. Hat vielleicht jemand einen Lösungsansatz?

(Die beiden Methoden stammen nicht von mir!!):twisted:

Vielen Dank

Michael

HolgerX 16. Mai 2016 09:36

AW: Checkboxen in Stringgrid
 
Zitat:

Zitat von michael.cohrs (Beitrag 1338208)
Guten morgen zusammen,

Delphi-Quellcode:
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] = '1' then // Checked
          Cells[iCol, iRow]:= '0'
         else if Cells[iCol, iRow] = 'false' then // not Checked
          Cells[iCol, iRow]:= 'true';
      end;
    end;
end;

Wo setzt Du denn hier wieder von 'not Cheched' auf 'Checked' zurück, wenn der Wert '0' ist?
Und umgekehrt wenn der Wert 'true' ist ?

Also entweder nur mit '0' und '1' oder 'fasle' und 'true' arbeiten und dann auch bei jedem Click 'umschalten' ;)

michael.cohrs 16. Mai 2016 12:41

AW: Checkboxen in Stringgrid
 
Hallo HolgerX,

es wird doch in dem MouseDown Event geprüft welcher Wert gerade gesetzt
wurde und dann entprechend umgekehrt gsetzt.

(Oder mache ich hier grundliegend etwas falsch????)

michael.cohrs 16. Mai 2016 12:50

AW: Checkboxen in Stringgrid
 
Sorry, Copy-Paste Fehler :oops:, hier der richtige Source:

Delphi-Quellcode:
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] = '1' then // Checked
          Cells[iCol, iRow]:= '0'
         else if Cells[iCol, iRow] = '0' then // not Checked
          Cells[iCol, iRow]:= '1';
      end;
    end;
end;

HolgerX 16. Mai 2016 13:51

AW: Checkboxen in Stringgrid
 
Zitat:

Zitat von michael.cohrs (Beitrag 1338208)
Delphi-Quellcode:
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;

OK, dann hier nur noch 'true' und 'false' gegen '1' und '0' tauschen, da dort nur die Zahl drinnen steht und nicht der Text.


oder

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

  function CheckBox(Value: boolean): Cardinal;
  begin
    result:= DFCS_INACTIVE; // no Entry
    if Value then // Checked
      result:= DFCS_BUTTONCHECK or DFCS_CHECKED
    else // not Checked
      result:= DFCS_BUTTONCHECK;
    if not Editing then
    result:= result or DFCS_MONO; // no Editing
  end;
 
var
  b : boolean;
begin
  try
    with TStringGrid(Sender) do
      if (ACol {in CheckBoxCols)} =1) and not (gdFixed in State) then begin
        b := StrToBool(Trim(Cells[ACol, ARow]));
        Canvas.FillRect(Rect);
        InflateRect(Rect, -4, -4);
        DrawFrameControl(Canvas.Handle, Rect,DFC_Button,CheckBox(b));
      end; // if gdFixed in State
  except
  end;
end;
Dann ist es egal, ob '0' und '1' oder 'true' und 'false', da StrToBoll beides konvertieren kann..

michael.cohrs 16. Mai 2016 14:00

AW: Checkboxen in Stringgrid
 
Danke, soweit habe ich alles umgesetzt, das Problem nun ist folgendes: wenn ich beim laden aus der Datenbank die eine Checkbox als Checked setze, reagieren die Boxen beim Click nicht mehr, soll heissen ich kann in keins der Boxen den Haken mehr setzen:cry:

HolgerX 16. Mai 2016 16:07

AW: Checkboxen in Stringgrid
 
Zeige doch mal, wie du den Wert (True/false) in das Stringgrid reinschreibst..

Wird da '0' und '1' verwendet oder doch was anderes?

michael.cohrs 16. Mai 2016 18:25

AW: Checkboxen in Stringgrid
 
Hallo,
so schreibe ich die Werte in das Grid:

Delphi-Quellcode:

procedure TFAuftragsDruckliste.BuildCheckListView;
Var
    oPrintOBJ        : TPrintObj;
    i                : Integer;
    sTemp            : String;
begin

  Dbg_Auftragsauswahl_new.RowCount := FPrint_lst.Count + 1;
  for I := 0 to FPrint_lst.Count - 1 do
  Begin
    oPrintOBJ := FPrint_lst[i] As TPrintObj;
    if oPrintOBJ <> nil then
    Begin
      if oPrintOBJ.APASW then
        sTemp := '1'
      else
        sTemp := '0';

      with Dbg_Auftragsauswahl_new do
      Begin
        Objects[8,i+1]    := oPrintOBJ;
        Cells[0, i+1]     := sTemp;
      end;
    end;
  end;
end;

HolgerX 18. Mai 2016 04:31

AW: Checkboxen in Stringgrid
 
Hmm..


Zitat:

Zitat von michael.cohrs (Beitrag 1338221)
Sorry, Copy-Paste Fehler :oops:, hier der richtige Source:

Delphi-Quellcode:
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] = '1' then // Checked
          Cells[iCol, iRow]:= '0'
         else if Cells[iCol, iRow] = '0' then // not Checked
          Cells[iCol, iRow]:= '1';
      end;
    end;
end;



Hier wird auf (iCol > 0) abgefragt, aber deine Column bei



Zitat:

Delphi-Quellcode:
procedure TFAuftragsDruckliste.BuildCheckListView;
Var
    oPrintOBJ : TPrintObj;
    i : Integer;
    sTemp : String;
begin

  Dbg_Auftragsauswahl_new.RowCount := FPrint_lst.Count + 1;
  for I := 0 to FPrint_lst.Count - 1 do
  Begin
    oPrintOBJ := FPrint_lst[i] As TPrintObj;
    if oPrintOBJ <> nil then
    Begin
      if oPrintOBJ.APASW then
        sTemp := '1'
      else
        sTemp := '0';

      with Dbg_Auftragsauswahl_new do
      Begin
        Objects[8,i+1] := oPrintOBJ;
        Cells[0, i+1] := sTemp;
      end;
    end;
  end;
end;


ist doch gleich 0! Somit aus (iCol > 0) ein (iCol >= 0) machen und dann sollte es gehen....

Ansonsten zeige nochmal alle beiden Funktionen, um Copy-Fehler aus zu schließen...


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:18 Uhr.

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