아래와 같이 버튼 클릭 시 10개 카테고리(TButtonCategory)에 5개의 버튼(TButtonItem)을 추가하고, 버튼 클릭시 버튼의 캡션을 얻어오는 예제입니다.

CategoryButtons.png

주의 할 점은 TButtonItem Click 시 발생하는 이벤트의 Sender가 TButtonItem이 아닌 모체인 TCategoryButtons가 넘어옵니다.

클릭한 TButtonItem은 TCategoryButtons.SelectedItem을 통해 접근할 수 있습니다.

 

참고로 TButtonItem은 Caption, Hint, Data(Pointer) 속성을 제공하므로 원하는 데이터를 알맞은 속성에 넣고 

클릭시 필요한 데이터를 꺼내서 구현할 수 있습니다.

 

(정정: 3/23)

TCategoryButtons.OnButtonClicked 이벤트를 이용하면 각 TButtonItem에 이벤트를 걸지 않아도 됩니다.

 

자세한 내용은 아래 코드를 참고하세요.

(첨부파일에서 다운로드 받을 수 있습니다.)

 

[코드]

type
  TForm1 = class(TForm)
    catMenuItems: TCategoryButtons;
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);

    // 정정: TCategoryButtons.OnButtonClicked
    procedure catMenuItemsButtonClicked(Sender: TObject;
  private
    { Private declarations }
    procedure ButtonItemClick(Sender: TObject);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  i, j: Integer;
  Cate: TButtonCategory;
  Item: TButtonItem;
begin
  catMenuItems.Categories.Clear;
  for i := 0 to 10 do
  begin
    Cate := catMenuItems.Categories.Add;
    Cate.Caption := 'Category ' + I.toString;
  end;


  //Menu Item
  for i := 0 to 10 do
  begin
    for j := 0 to 5 do
    begin
      Item := catMenuItems.Categories[i].Items.Add;
      Item.Caption := Format('Menu %d %d', [i,j]);
      Item.Hint := Format('Menu %d %d', [i,j]);
      Item.OnClick := ButtonItemClick;
    end;
  end;
end;

 

procedure TForm1.ButtonItemClick(Sender: TObject);
begin
  Memo1.Lines.Add(Sender.ClassName);
  Memo1.Lines.Add(catMenuItems.SelectedItem.ClassName);

  Memo1.Lines.Add(catMenuItems.SelectedItem.Caption);
end;

 

// 정정: TCategoryButtons.OnButtonClicked
procedure TForm1.catMenuItemsButtonClicked(Sender: TObject;
  const Button: TButtonItem);
begin
  Memo1.Lines.Add(Button.Caption);
end;

 

번호 제목 글쓴이 날짜 조회 수
공지 [DelphiCon 요약] 코드사이트 로깅 실전 활용 기법 (Real-world CodeSite Logging Techniques) 관리자 2021.01.19 15469
공지 [UX Summit 요약] 오른쪽 클릭은 옳다 (Right Click is Right) 관리자 2020.11.16 13974
공지 [10.4 시드니] What's NEW! 신기능 자세히 보기 관리자 2020.05.27 16505
공지 RAD스튜디오(델파이,C++빌더) - 고객 사례 목록 관리자 2018.10.23 22062
공지 [데브기어 컨설팅] 모바일 앱 & 업그레이드 마이그레이션 [1] 관리자 2017.02.06 23276
공지 [전체 목록] 이 달의 기술자료 & 기술레터 관리자 2017.02.06 18933
공지 RAD스튜디오(델파이, C++빌더) - 시작하기 [1] 관리자 2015.06.30 39266
공지 RAD스튜디오(델파이,C++빌더) - 모바일 앱 개발 사례 (2020년 11월 업데이트 됨) 험프리 2014.01.16 174729
51 [코드레이지11] FireDAC 이해하기, RAD 스튜디오 데이터 엑세스 프레임워크 선택 - Cary Jensen 험프리 2016.11.24 539
50 [동영상] 애플 터치 아이디 연동하기 file 험프리 2016.11.01 422
49 [동영상] 앱테더링 데이터 암호화/복호화 방법 file 험프리 2016.11.01 804
48 이 달의 기술자료 - 2016년 11월 험프리 2016.10.28 441
47 COM 오브젝트 연동 유닛 생성하기: Import a Type Library file 험프리 2016.09.30 2517
46 [발표자료] 20160929 나만의 앱 완성하기 with 델파이 험프리 2016.09.21 882
45 안드로이드 SDK 설정 방법 file 험프리 2016.09.20 2869
44 RAD 스튜디오(델파이, C++빌더) 웹개발 방법(WebBroker, IntraWeb) 험프리 2016.09.07 1831
43 TeeChart 컴포넌트를 통해 다양한 차트 및 그래프로 데이터를 출력할 수 있습니다. file 험프리 2016.08.30 5249
42 이 달의 기술자료 - 2016년 09월 file 험프리 2016.08.25 590
41 FireDAC 성능 비교(BDE, dbGO(ADO), dbExpress, FireDAC) 험프리 2016.08.09 1756
40 퀵레포트 보고서 엑셀로 내보내기(저장하기) [1] 험프리 2016.08.01 1668
39 이 달의 기술자료 - 2016년 07월 file 험프리 2016.06.30 2038
38 이 달의 기술자료 - 2016년 05월 file 험프리 2016.04.26 658
37 [안드로이드/iOS] 이미 배포(Deployment)된 파일 업데이트 하기 험프리 2016.03.31 1543
36 이 달의 기술자료 - 2016년 04월 file 험프리 2016.03.28 503
» TCategoryButtons 동적 생성하고, TButtonItem 클릭 이벤트 연결하기 file 험프리 2016.03.23 1369
34 이 달의 기술자료 - 2016년 03월 [1] file 험프리 2016.02.25 779
33 이 달의 기술자료 - 2016년 2월 file 험프리 2016.01.26 549
32 이 달의 기술자료 - 2016년 01월 file 험프리 2015.12.28 515