아래와 같이 버튼 클릭 시 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 15443
공지 [UX Summit 요약] 오른쪽 클릭은 옳다 (Right Click is Right) 관리자 2020.11.16 13962
공지 [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 39259
공지 RAD스튜디오(델파이,C++빌더) - 모바일 앱 개발 사례 (2020년 11월 업데이트 됨) 험프리 2014.01.16 174726
21 [환경설정] 아마존 EC2 이용해 리눅스 서버 환경 구축하기 file 험프리 2017.04.20 10642
20 [환경설정] 아마존 EC2 이용해 윈도우 서버 환경 구축하기 [1] file 험프리 2017.04.13 6213
19 [따라하기] 건강데이터 수집 및 기록 시스템 #1 - BLE 기반 스마트 체중계에서 실시간 데이터 받기 [1] Humphery 2015.10.02 5794
18 데이터스냅에서 제공하는 JSON 포맷 데이터 사용(분석)하기 [1] file Humphery 2015.07.20 3775
17 COM 오브젝트 연동 유닛 생성하기: Import a Type Library file 험프리 2016.09.30 2502
16 안면인식(Face Detection) 라이브러리(안드로이드, iOS) 험프리 2017.10.30 2418
15 리눅스 서버용 GUI 응용프로그램 만들기(10.2 도쿄 & FMXLINUX 이용) [1] 관리자 2017.04.25 2082
14 [오픈소스] Python for Delphi(P4D) [5] file 험프리 2017.09.01 1980
13 퀵레포트 보고서 엑셀로 내보내기(저장하기) [1] 험프리 2016.08.01 1664
12 [코드레이지11] 인공지능(AI) with 델파이 & C++빌더 - Boian Mitov [1] 험프리 2016.11.24 1580
» TCategoryButtons 동적 생성하고, TButtonItem 클릭 이벤트 연결하기 file 험프리 2016.03.23 1366
10 TTS(Text-to-Speech) 라이브러리(윈도우, 맥OS, iOS, 안드로이드) [2] 험프리 2017.10.30 1338
9 델파이 코드 분석 도구 [1] 험프리 2017.05.16 1142
8 컴파일 시 특정 경고 메시지를 발생하지 않도록 하는 방법 file 험프리 2017.01.31 1052
7 [VCL] 화면의 특정영역을 확대하는 “돋보기” 기능 구현방법 소개 file 험프리 2017.09.01 938
6 [동영상] 앱테더링 데이터 암호화/복호화 방법 file 험프리 2016.11.01 803
5 첨부파일을 포함한 이메일 전송하기(iOS, 안드로이드, 윈도우) file 험프리 2017.08.04 665
4 IBM 왓슨과 인공지능(AI) 활용하기 - 델파이/C++빌더 관리자 2019.10.25 665
3 [오픈소스] VerySimple.Lua file 험프리 2017.09.01 501
2 Tools API를 사용해 IDE를 확장할 수 있습니다. file 험프리 2017.07.20 498