Q&A

  • 스트링그리드 내에 체크박스 넣기...
StringGrid 내의 특정 칼럼에 checkbox 를 넣고 싶거든요...(스트링 그리드 내의
생성되는 레코드 수만큼..........)

용도는.........

특정 레코드를 삭제할때 checkbox 를 선택해서 선택된 레코드를 한꺼번에
삭제하려고요..........


그럼..........

수고하시고..........


답변 미리 감사합니다...........
2  COMMENTS
  • Profile
    홍성락 2002.07.09 05:37
    hsr//////////////////////////////////////////////////////
    예전에 작성하던것이 있어 소스와 자료실에 오려보았습니다.
    스크롤이나 셀 컬럼 크기조정시 잔상 제거입니다.
    잘 다듬어 사용해보세요
    unit Unit1;

    interface

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

    type
      TForm1 = class(TForm)
        SGstock: TStringGrid;
        procedure SGstockDrawCell(Sender: TObject; ACol, ARow: Integer;
          Rect: TRect; State: TGridDrawState);
        procedure CheckBoxMouseUp(Sender: TObject; Button: TMouseButton;
                                  Shift: TShiftState; X, Y: integer);
        procedure FormShow(Sender: TObject);
      private
        { Private declarations }

        procedure StringGridInCheckBoxCreate(sg :TStringGrid; Col, Row :Integer; pChecked :Boolean);
      public
        { Public declarations }
      end;

    var
      Form1: TForm1;

    implementation

    {$R *.DFM}
    procedure TForm1.FormShow(Sender: TObject);
    var
        i, j : integer;
        CheckBox_Col : set of 0..10;
    begin
        SGstock.Cells[0,0]   := '입고여부';
        SGstock.cells[1,0]   := '바코드';
        SGstock.cells[2,0]   := '부품상태';

        //체크박스 넣을 col번호 초기화
        CheckBox_Col := [1,2];

        for j := 1 to SGstock.RowCount-1 do begin
            for i := 1 to SGstock.ColCount-1 do begin
               sgstock.cells[i,j] := intToStr(i) + '/' + intToStr(j);
               if i in CheckBox_Col then
                  StringGridInCheckBoxCreate(SGstock,i,j,True);
            end;
        end
    end;
    /////////////////////////////////////////////////////////////////////////////
    procedure TForm1.StringGridInCheckBoxCreate(sg :TStringGrid; Col, Row :Integer; pChecked :Boolean);
    var
      LeftWidth, iCnt :Integer;
    begin
      if not (sg.Objects[Col, Row] is TCheckBox) then
        sg.Objects[Col, Row] := TCheckBox.Create(sg);
      with TCheckBox(sg.Objects[Col, Row]) do
      begin
        OnMouseUp  := CheckBoxMouseUp;
        Parent     := sg;
        Checked    := pChecked;
        BoundsRect := sg.CellRect(Col, Row);
        Width      := 14;
        Height     := sg.RowHeights[Row];
      end;
    end;
    {==================================
      CheckBoxMouseUp Event Process
    ==================================}
    procedure TForm1.CheckBoxMouseUp(Sender: TObject; Button:
    TMouseButton; Shift: TShiftState; X, Y: Integer);
    begin
      with TCheckBox(Sender) do
        Checked := not Checked;
    end;
    /////////////////////////////////////////////////////////////////////////////
    procedure TForm1.SGstockDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
        if (TStringGrid(Sender).Objects[ACol, ARow] is TCheckBox) then begin
           with TCheckBox(TStringGrid(Sender).Objects[ACol, ARow]) do begin
                top  := Rect.Top;
                Left := Rect.Left;
           end;

           with TStringGrid(Sender) do begin
                Canvas.Font := Font;
                Canvas.TextRect(Rect,
                                Rect.left+14,(Rect.Top+Rect.Bottom-Font.Size-2) div 2,
                                TStringGrid(Sender).Cells[ACol,ARow] );
           end;
        end;
    end;
    /////////////////////////////////////////////////////////////////////////////

    end.
  • Profile
    머슴 2002.05.29 02:29

    저도 Tip에서(검색어 :스트링그리드하면 4번째 자료) 베껴서 쓰고
    있는데요....

    약간은 문제가 있는것 같아요...

    그리드에 포커스가 있는 상태에서 스크롤바를 옮기면 check박스의
    잔상이 남아요....어찌 해결을 하나...  

    public
    procedure CheckBoxMouseUp(Sender: TObject; Button: TMouseButton;
    Shift: TShiftState; X, Y: integer);
    procedure CheckBoxKeyUp(Sender: TObject; var Key: Word;
    Shift: TShiftState);
    ....
    procedure TMainForm.CheckBoxMouseUp(Sender: TObject; Button:
    TMouseButton; Shift: TShiftStates; X, Y: Integer);
    begin
         with TCheckBox(Sender) do
           CheckBox := not Checked;
    end;

    procedure TMainForm.CheckBoxKeyUp(Sender: TObject; var Key: Word;
    Shift: TShiftState);
    begin
         if Key = VK_SPACE then
        with TCheckBox(Sender) do
         Checked := not Checked;
    end;

    procedure TMainForm.BitBtn1Click(Sender: TObject);
    begin
       StringGrid1.Objects[1, 1] := TCheckBox.Create(StringGrid1);
       with TCheckBox(StringGrid1.Objects[1, 1]) do
         begin
           OnKeyUp := CheckBoxKeyUp;
           OnMouseUp := CheckBoxMouseUp;
           Parent := StringGrid1;
           BoundsRect := StringGrid1.CellRect(1, 1);
           Width := StringGrid1.ColWidths[1];
           Height := StringGrid1.RowHeights[1];
           Caption := '이것은 체크박스';
           Checked := false;
       end;
    end;