Q&A

  • 스트링그리드에 관한 ..(소스포함)
스트링그리드에 버튼을 집어 넣는거 까지는 돼는데....



버튼이 위치한 (0,row)에 값을 뽑아내기가 힘드네요!



다음 소스를 봐주세요!...



첫번째 버튼을 클릭하면 '강아지'

두버째 버튼을 클리하면 '고양이'

세번째 버튼을 클릭하면 '병아리'

라는 메세지가 뜨게 할려고 하는데요.....



unit Unit1;



interface



uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

StdCtrls, Grids, Buttons, ComCtrls;



type

TForm1 = class(TForm)

SpeedButton1: TSpeedButton;

StringGrid1: TStringGrid;

//StatusBar1: TStatusBar;

procedure SpeedButton1Click(Sender: TObject);



procedure FormCreate(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

procedure CheckBoxMouseUp(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: integer);



end;



var

Form1: TForm1;



implementation



{$R *.DFM}



procedure TForm1.CheckBoxMouseUp(Sender: TObject; Button:

TMouseButton; Shift: TShiftState; X, Y: Integer);

var

i:string;

begin





i:=stringgrid1.Cells[0,stringgrid1.row];

showmessage(i);

end;











procedure TForm1.SpeedButton1Click(Sender: TObject);

var Y : integer;

i:string;

begin

for y := 0 to stringgrid1.rowcount - 1 do

begin



StringGrid1.Objects[1, y] := Tbutton.Create(StringGrid1);

with Tbutton(StringGrid1.Objects[1, y]) do

begin



OnMouseUp := CheckBoxMouseUp;

Parent := StringGrid1;

BoundsRect := StringGrid1.CellRect(1, y);

Width := StringGrid1.ColWidths[1];

Height := StringGrid1.RowHeights[1];

end;

end;

end;



procedure TForm1.FormCreate(Sender: TObject);

begin

stringgrid1.cells[0,1]:='강아지';

stringgrid1.cells[0,2]:='고양이';

stringgrid1.cells[0,3]:='병아리';

end;



end.



자꾸 어떤걸 클릭해도 강아지만 나와여... 흑흑흑

2  COMMENTS
  • Profile
    이진수 2001.03.06 23:10
    자 소스를 보니까 다음과 같은 오류가 있네요



    > i:=stringgrid1.Cells[0,stringgrid1.row];

    > showmessage(i);



    이 문장에 오해가 있으시네요

    stringgrid1.row 이 당연히 클릭하는 그리드의 행을 반환할 거라 생각 하신거죠



    그러나 그렇지 않습니다.

    그냥 1만 들어 있을 뿐입니다. 그러니 계속 강아지만 나오겠죠 ^^



    이때에는 뭐를 써야 하냐면요 TComponent에서 상속받은 Tag를 이용하세요



    > StringGrid1.Objects[1, y] := Tbutton.Create(StringGrid1);

    > with Tbutton(StringGrid1.Objects[1, y]) do

    > begin

    >

    > OnMouseUp := CheckBoxMouseUp;

    > Parent := StringGrid1;

    > BoundsRect := StringGrid1.CellRect(1, y);

    > Width := StringGrid1.ColWidths[1];

    > Height := StringGrid1.RowHeights[1];

    문장내에

    Tag := y 문장을 삽입시켜준후에



    procedure TForm1.CheckBoxMouseUp(Sender: TObject; Button:

    TMouseButton; Shift: TShiftState; X, Y: Integer);

    var

    i:string;

    Button : TButton;



    begin

    Button := Sender as TButton;

    if ( Button not nill ) then

    begin

    i:=stringgrid1.Cells[0,Button.Tag];

    showmessage(i);

    end;

    end;



    하면 될 겁니다. 지금 허접하게 작성한거라 컴파일 에러가 날 수도 ^^











    델초봅니다. wrote:

    > 스트링그리드에 버튼을 집어 넣는거 까지는 돼는데....

    >

    > 버튼이 위치한 (0,row)에 값을 뽑아내기가 힘드네요!

    >

    > 다음 소스를 봐주세요!...

    >

    > 첫번째 버튼을 클릭하면 '강아지'

    > 두버째 버튼을 클리하면 '고양이'

    > 세번째 버튼을 클릭하면 '병아리'

    > 라는 메세지가 뜨게 할려고 하는데요.....

    >

    > unit Unit1;

    >

    > interface

    >

    > uses

    > Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

    > StdCtrls, Grids, Buttons, ComCtrls;

    >

    > type

    > TForm1 = class(TForm)

    > SpeedButton1: TSpeedButton;

    > StringGrid1: TStringGrid;

    > //StatusBar1: TStatusBar;

    > procedure SpeedButton1Click(Sender: TObject);

    >

    > procedure FormCreate(Sender: TObject);

    > private

    > { Private declarations }

    > public

    > { Public declarations }

    > procedure CheckBoxMouseUp(Sender: TObject; Button: TMouseButton;

    > Shift: TShiftState; X, Y: integer);

    >

    > end;

    >

    > var

    > Form1: TForm1;

    >

    > implementation

    >

    > {$R *.DFM}

    >

    > procedure TForm1.CheckBoxMouseUp(Sender: TObject; Button:

    > TMouseButton; Shift: TShiftState; X, Y: Integer);

    > var

    > i:string;

    > begin

    >

    >

    > i:=stringgrid1.Cells[0,stringgrid1.row];

    > showmessage(i);

    > end;

    >

    >

    >

    >

    >

    > procedure TForm1.SpeedButton1Click(Sender: TObject);

    > var Y : integer;

    > i:string;

    > begin

    > for y := 0 to stringgrid1.rowcount - 1 do

    > begin

    >

    > StringGrid1.Objects[1, y] := Tbutton.Create(StringGrid1);

    > with Tbutton(StringGrid1.Objects[1, y]) do

    > begin

    >

    > OnMouseUp := CheckBoxMouseUp;

    > Parent := StringGrid1;

    > BoundsRect := StringGrid1.CellRect(1, y);

    > Width := StringGrid1.ColWidths[1];

    > Height := StringGrid1.RowHeights[1];

    > end;

    > end;

    > end;

    >

    > procedure TForm1.FormCreate(Sender: TObject);

    > begin

    > stringgrid1.cells[0,1]:='강아지';

    > stringgrid1.cells[0,2]:='고양이';

    > stringgrid1.cells[0,3]:='병아리';

    > end;

    >

    > end.

    >

    > 자꾸 어떤걸 클릭해도 강아지만 나와여... 흑흑흑

  • Profile
    컴맹.. 2001.03.06 23:02
    이렇게 한번 해보세여...

    unit Unit1;



    interface



    uses

    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

    Grids, StdCtrls;



    type

    TForm1 = class(TForm)

    StringGrid1: TStringGrid;

    Label1: TLabel;

    procedure FormActivate(Sender: TObject);

    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;

    Rect: TRect; State: TGridDrawState);

    procedure StringGrid1DblClick(Sender: TObject);

    private

    { Private declarations }

    public

    { Public declarations }

    end;



    var

    Form1: TForm1;



    implementation



    {$R *.DFM}



    procedure TForm1.FormActivate(Sender: TObject);

    var

    i, j: Integer;

    begin

    // Column의 title을 만든다

    for i := 1 to StringGrid1.ColCount - 1 do

    StringGrid1.Cells[i, 0] := Char(Ord('A')+i-1);



    // Row의 title을 만든다

    for i := 1 to StringGrid1.RowCount - 1 do

    StringGrid1.Cells[0, i] := IntToStr(i);;



    // 임의의 금액을 만들어서 각 cell에 입력합니다

    // format함수의 %n 은 금액(3자리수마다 콤마문자)표시 서식입니다

    for i := 1 to StringGrid1.ColCount - 1 do

    for j := 1 to StringGrid1.RowCount - 1 do

    StringGrid1.Cells[i, j] := Format('%.0n', [i * j * 10000.0]);



    end;



    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;

    Rect: TRect; State: TGridDrawState);

    begin

    if gdSelected in State then

    begin

    with (Sender as TstringGrid).Canvas do

    begin

    // 아래 네번째 파라미터에 or DFCS_FLAT 를 추가하면 들어간 형태의 버튼을

    // 만들 수 있습니다

    DrawFrameControl(Handle, Rect, DFC_BUTTON,

    DFCS_BUTTONPUSH or DFCS_ADJUSTRECT);

    Brush.Style := bsClear;

    Font.Color := clBlack;

    TextRect(rect, rect.left + 2, rect.top+2,

    TStringGrid(Sender).Cells[ACol, ARow]);



    end;

    end;

    end;



    procedure TForm1.StringGrid1DblClick(Sender: TObject);

    begin

    ShowMessage(StringGrid1.Cells[StringGrid1.Col, StringGrid1.Row]);

    end;



    end.