Q&A

  • 버튼생성과 히든에 관하여..
설정파일을 읽어서 버튼을 생성할려다가 실패해서
패널에 버튼을 15개 배치 햇습니다.기본버튼..

그리고 설정파일의 숫자를 읽어서 그 숫자가 만약에 10이라면
버튼을 10개만 순차적으로 보이게 할려고 하는데...이상하게 잘안되서조언을 구합니다.

배열을 넣어야될까요?

지금 대충 해본게....

<!--CodeS-->
for i :=0 to strtoint(tmpchr) do

    if i = 1  then  button1.visible :=true else button1.visible :=false;

<!--CodeE-->

어떤 숫자를 넣어도 1번 버튼이 안보입니다..
그리고 저버튼을 for문으로 계속 돌릴수는 없을까요?

for i :=0 to strtoint(tmpchr) do

   button[i].visible :=true ;

이렇게 하면안되겟지요?
button이 선언이 안
3  COMMENTS
  • Profile
    최진환 2006.10.06 09:27
    동적생성으로 버튼만드는걸로 전환햇는데..

    문제가버튼이 위에서 아래로 일자 배열이 되어버립니다.

    30개만 되도 창이 길게 늘어져버려서...

    이걸 옆으로 5칸가고 밑으로 내려가게 배열할수 없을까요?

    1
    2
    3
    4
    5
    ...
    이거 말고

    1 2 3 4 5
    6 7 8 9 10

    이렇게 요..

    조언부탁드립니다..

    아참 소스..
    <!--CodeS-->
    unit new;

    {******************************************************************************
    ******* 동적으로 객체를 생성하여 그에 대한 이벤트를 설정하는 프로그램 ********
    ******************************************************************************

    ------------------------------------------------------------------------------
    - 작성자 : 김 진 훈 -
    - 최종 Compile : 2000.11.13 (월) -
    - Input File : [없음] -
    - Output File : [없음] -
    - 설명 : 프로그램을 하다 보면 동적으로 그객체를 생성해 주어야 하는 -
    - 경우가 발생 할 수 있다. 즉 폼에다 미리 컴퍼넌느를 올려 놓지 -
    - 않고 프로그램의 필요에 따라서 새로 객체를 생성하는 방식이다.-
    - Visible를 이용할 수도 있지만 이때는 프로그램의 초기 크기가 -
    - 같이 커지기 때문에 업데이트용 파일을 만들때는 이런방식의 -
    - 동적생성이 필요할 것이다. 그렇다고 리소스를 조금 잡아 먹는다-
    - 는 것은 아니니 이점 유의 할 수 있도록 한다. 특히 동적으로 -
    - 생성한 객체를 반드시 리소스 반환이라는 작을업을 거치지 않고 -
    - 할 경우 생성한 객체는 리소스에 그대로 남아있게 되니 이점 -
    - 반드시 유의 하기 바람 -
    ------------------------------------------------------------------------------}

    interface

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

    type
    TForm1 = class(TForm)
    //procedure Button1Click(Sender: TObject);//버튼을 클릭 했을 때의 Event
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure Button1Click(Sender: TObject);
    private
    {Component 생성에 관련된 Procedure}

    procedure ButtonShow; // 일반 버튼들을 생성한다.



    {Component 소멸에 관련된 Procedure}

    procedure ButtonFree; // Button Free & nil



    {기능에 관련된 Porcedure}

    { Private declarations }
    public
    { Public declarations }
    end;

    var
    Form1 : TForm1;
    ClrBtn : TButton; // Clear 버튼 정의


    //BtnName : array[1..20] of String; // 레이블의 이름 저장하는 배열
    Btn : array[1..20] of TButton; // 버튼생성을 위한 배열

    implementation

    {$R *.DFM}

    {******************************************************************************}
    {************************** 동적으로 생성해 주는 부분 *************************}
    {******************************************************************************}



    procedure TForm1.ButtonShow;
    // 1~10 까지의 버튼을 배열을 이용하여 동적으로 생성한다.
    var
    i,j,k : Integer;
    begin
        k := 1;
    for j := 1 to 5 do begin
       for i := 1 to 10 do begin
       k := i +j ;
    Btn[k] := TButton.Create(self);

    with Btn[k] do begin
            Left := left + 50;
            top := (k*40);
            height := 40;
            Parent:= Form1; // Parent를 지정한다.
            Font.Size := 9;
            parent:=self;
            OnClick := Button1Click; // Button Click시 Event를 수행할 Procedure지정
            Caption    := inttostr(k)+'키실행';
            Tag := k;
            end;
    // Left :=i+  50;

    end;
    end;
    end;
       {SetLength(AAA, i);
    for c := 0 to i - 1 do
      SetLength(AAA[i], j);  }



    {************************** 여기 까지 동적 생성 부분 **************************}

    {
    ********************************************************************************
    ***************** 동적으로 생성한 객체를 메모리로 환원하는 부분 ****************
    ********************************************************************************
    --------------------------------------------------------------------------------
    - 동적 생성 객체를 환원 시켜주지 않을 경우 프로그램을 종료한 후에도 리소스를 -
    - 반환하지 못하여 재부팅 전까지는 그만큼의 리소스를 사용할 수 없게 된다. -
    - 추가로 프로그램 실행중에도 그 폼을 닫아도 리소스를 환원하지 않기 때문에 게속 -
    - 프로그램을 실행시 리소스 부족으로 컴퓨터가 다운되는 경우도 있다. 반드시 환원 -
    - 시켜 주어야 한다. %% 델파이에는 Garvage Collction 기능이 없다. %% -
    --------------------------------------------------------------------------------
    }



    procedure TForm1.ButtonFree;
    // Button Free & nil
    var
    k : Integer;
    begin
    for k := 1 to 20 do
    FreeAndNil(btn[k]);
    end;



    {************************ 여기 까지가 메모리 환원 부분 ************************}


    procedure TForm1.FormCreate(Sender: TObject);
    // 프로그램 실행과 동시에(메인폼이 생성되면서) 화면을 만들어 주는 부분
    begin

    ButtonShow; // Call TButton Create Procedure

    end;






    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    // Exit 버튼이 아닌 폼의 닫기 버튼으로 닫을 경우. 혹시 또 모르니까..
    begin

    ButtonFree;

    FreeAndNil(ClrBtn);
    end;


    procedure TForm1.Button1Click(Sender: TObject);
      begin
    showmessage('버튼' + inttostr(TSpeedButton(Sender).Tag) + '번이 눌렸네요~~');
    // ButtonFree; // Call TButton Distroy Procedure

    end;
             {
    procedure TForm1.Button1Click(Sender: TObject);
    // 각각의 버튼을 눌렀을때 Event

      var
      i: integer;
      begin

    for i := 1 to 10 do begin

    if Sender = btn[i] then
    //if (Sender as TButton).Caption = i then
    break;

    end;
    case i of

    1: ShowMessage(IntToStr(i)); //각 버턴에 맞는 루틴을 이부분에

    //쓰면 되겠죠.

    2: ShowMessage(IntToStr(i));

    3: ShowMessage(IntToStr(i));

    4: ShowMessage(IntToStr(i));

    5: ShowMessage(IntToStr(i));

    6: ShowMessage(IntToStr(i));

    7: ShowMessage(IntToStr(i));

    8: ShowMessage(IntToStr(i));

    9: ShowMessage(IntToStr(i));
    10: ShowMessage(IntToStr(i));
    11: ShowMessage(IntToStr(i)); //각 버턴에 맞는 루틴을 이부분에

    //쓰면 되겠죠.

    12: ShowMessage(IntToStr(i));

    13: ShowMessage(IntToStr(i));

    14: ShowMessage(IntToStr(i));

    15: ShowMessage(IntToStr(i));

    16: ShowMessage(IntToStr(i));

    17: ShowMessage(IntToStr(i));

    18: ShowMessage(IntToStr(i));

    19: ShowMessage(IntToStr(i));
    20: ShowMessage(IntToStr(i));

    {if (Sender as TButton).Caption = 'Clear' then begin
    ButtonFree; // Call TButton Distroy Procedure

    end;

    if(Sender as TButton).Caption = 'Exit' then begin
    ButtonFree;
    FreeAndNil(ClrBtn);
    Form1.Close;
    end;
    end;

       //TButton(Sender).Caption:= '아야!!';

        end;
          end;   }
    end.



    <!--CodeE-->
  • Profile
    민스맘 2006.10.10 21:33
    <!--CodeS-->
    with Btn[k] do begin
            Left := left + 50;
            top := (k*40);
            height := 40;
            Parent:= Form1; // Parent를 지정한다.
            Font.Size := 9;
            parent:=self;
            OnClick := Button1Click; // Button Click시 Event를 수행할 Procedure지정
            Caption    := inttostr(k)+'키실행';
            Tag := k;
       end;

    <!--CodeE-->

    여기서 문제는 top := (k * 50) 때문에 아래로 쭉 눌어서는 것이거든요

    버튼이 놓일 폼의 width 에서 앞에 생성시킨 버튼의 left, width 를 빼고 버튼 간격을 빼서 left 를 정하구요 만약 left + width 가 폼의 width 보다 크면 top 을 조절하고 left 를 조절하는 방법으로 해보세요

    코딩은 잘 하시것지요  ^^
  • Profile
    최진환 2006.10.11 01:10



    어찌어찌해서 포문 두번 돌리고 . 앞에다가 말씀하신대로 기본값을 주고 높이에도 변수줘서 원하는대로 만들었습니다..

    해보니 안되지는 않지만..역시 초보의 길은 힘들다는...
    저거 하나 해결할라고 일주일 꼬박.....구구단 프로그램만 봤다는 ..ㅠ.ㅠ;