아래와 같이 버튼 클릭 시 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 15438
공지 [UX Summit 요약] 오른쪽 클릭은 옳다 (Right Click is Right) 관리자 2020.11.16 13961
공지 [10.4 시드니] What's NEW! 신기능 자세히 보기 관리자 2020.05.27 16499
공지 RAD스튜디오(델파이,C++빌더) - 고객 사례 목록 관리자 2018.10.23 22055
공지 [데브기어 컨설팅] 모바일 앱 & 업그레이드 마이그레이션 [1] 관리자 2017.02.06 23268
공지 [전체 목록] 이 달의 기술자료 & 기술레터 관리자 2017.02.06 18923
공지 RAD스튜디오(델파이, C++빌더) - 시작하기 [1] 관리자 2015.06.30 39255
공지 RAD스튜디오(델파이,C++빌더) - 모바일 앱 개발 사례 (2020년 11월 업데이트 됨) 험프리 2014.01.16 174699
56 RAD 스튜디오(델파이, C++빌더) 웹개발 방법(WebBroker, IntraWeb) 험프리 2016.09.07 1829
55 TeeChart 컴포넌트를 통해 다양한 차트 및 그래프로 데이터를 출력할 수 있습니다. file 험프리 2016.08.30 5240
54 이 달의 기술자료 - 2016년 09월 file 험프리 2016.08.25 590
53 FireDAC 성능 비교(BDE, dbGO(ADO), dbExpress, FireDAC) 험프리 2016.08.09 1755
52 퀵레포트(Quick Report)에 사진 출력하기 험프리 2016.08.01 1864
51 BLOB 컬럼에 (이미지 등의)데이터 읽고 쓰기 험프리 2016.08.01 6093
50 퀵레포트 보고서 엑셀로 내보내기(저장하기) [1] 험프리 2016.08.01 1662
49 이 달의 기술자료 - 2016년 08월 file 험프리 2016.07.28 464
48 이 달의 기술자료 - 2016년 07월 file 험프리 2016.06.30 2038
47 이 달의 기술자료 - 2016년 06월 file 험프리 2016.05.27 632
46 이 달의 기술자료 - 2016년 05월 file 험프리 2016.04.26 658
45 [안드로이드/iOS] 이미 배포(Deployment)된 파일 업데이트 하기 험프리 2016.03.31 1540
44 이 달의 기술자료 - 2016년 04월 file 험프리 2016.03.28 503
» TCategoryButtons 동적 생성하고, TButtonItem 클릭 이벤트 연결하기 file 험프리 2016.03.23 1366
42 이 달의 기술자료 - 2016년 03월 [1] file 험프리 2016.02.25 779
41 이 달의 기술자료 - 2016년 2월 file 험프리 2016.01.26 549
40 이 달의 기술자료 - 2016년 01월 file 험프리 2015.12.28 515
39 이 달의 기술자료 - 2015년 12월 file 험프리 2015.11.27 531
38 이 달의 기술자료 - 2015년 11월 file 험프리 2015.10.30 745
37 [따라하기] 건강데이터 수집 및 기록 시스템 #1 - BLE 기반 스마트 체중계에서 실시간 데이터 받기 [1] Humphery 2015.10.02 5794