From $ROOTSYS/tutorials/gui/listBox.C

//
// Author: Ilka Antcheva   1/12/2006

// This macro gives an example of how to create a list box
// and how to set and use its multiple selection feature.
// To run it do either:
// .x listBox.C
// .x listBox.C++

#include <TApplication.h>
#include <TGClient.h>
#include <TGButton.h>
#include <TGListBox.h>
#include <TList.h>

class MyMainFrame : public TGMainFrame {

private:
   TGListBox           *fListBox;
   TGCheckButton       *fCheckMulti;
   TList               *fSelected;

public:
   MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h);
   virtual ~MyMainFrame();
   void DoExit();
   void DoSelect();
   void HandleButtons();
   void PrintSelected();

   ClassDef(MyMainFrame, 0)
};

void MyMainFrame::DoSelect()
{
   Printf("Slot DoSelect()");
}

void MyMainFrame::DoExit()
{
   Printf("Slot DoExit()");
   gApplication->Terminate(0);
}

MyMainFrame::MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h) :
   TGMainFrame(p, w, h)
{
   // Create main frame

   fListBox = new TGListBox(this, 89);
   fSelected = new TList;
   char tmp[20];
   for (int i = 0; i < 20; ++i) {
      sprintf(tmp, "Entry %i", i+1);
      fListBox->AddEntry(tmp, i+1);
   }
   fListBox->Resize(100,150);
   AddFrame(fListBox, new TGLayoutHints(kLHintsTop | kLHintsLeft |
                                        kLHintsExpandX | kLHintsExpandY,
                                        5, 5, 5, 5));

   fCheckMulti = new TGCheckButton(this, "&Mutliple selection", 10);
   AddFrame(fCheckMulti, new TGLayoutHints(kLHintsTop | kLHintsLeft,
                                           5, 5, 5, 5));
   fCheckMulti->Connect("Clicked()", "MyMainFrame", this, "HandleButtons()");
   // Create a horizontal frame containing button(s)
   TGHorizontalFrame *hframe = new TGHorizontalFrame(this, 150, 20, kFixedWidth);
   TGTextButton *show = new TGTextButton(hframe, "&Show");
   show->SetToolTipText("Click here to print the selection you made");
   show->Connect("Pressed()", "MyMainFrame", this, "PrintSelected()");
   hframe->AddFrame(show, new TGLayoutHints(kLHintsExpandX, 5, 5, 3, 4));
   TGTextButton *exit = new TGTextButton(hframe, "&Exit ");
   exit->Connect("Pressed()", "MyMainFrame", this, "DoExit()");
   hframe->AddFrame(exit, new TGLayoutHints(kLHintsExpandX, 5, 5, 3, 4));
   AddFrame(hframe, new TGLayoutHints(kLHintsExpandX, 2, 2, 5, 1));

   // Set a name to the main frame
   SetWindowName("List Box");
   MapSubwindows();

   // Initialize the layout algorithm via Resize()
   Resize(GetDefaultSize());

   // Map main frame
   MapWindow();
   fListBox->Select(1);
}

MyMainFrame::~MyMainFrame()
{
   // Clean up main frame...
   Cleanup();
   if (fSelected) {
      fSelected->Delete();
      delete fSelected;
   }
}

void MyMainFrame::HandleButtons()
{
   // Handle check button.
   Int_t id;
   TGButton *btn = (TGButton *) gTQSender;
   id = btn->WidgetId();

   printf("HandleButton: id = %d\n", id);

   if (id == 10)
      fListBox->SetMultipleSelections(fCheckMulti->GetState());
}


void MyMainFrame::PrintSelected()
{
   // Writes selected entries in TList if multiselection.

   fSelected->Clear();

   if (fListBox->GetMultipleSelections()) {
      Printf("Selected entries are:\n");
      fListBox->GetSelectedEntries(fSelected);
      fSelected->ls();
   } else {
      Printf("Selected entries is: %d\n", fListBox->GetSelected());
   }
}

void listBox()
{
   // Popup the GUI...
   new MyMainFrame(gClient->GetRoot(), 200, 200);
}
 listBox.C:1
 listBox.C:2
 listBox.C:3
 listBox.C:4
 listBox.C:5
 listBox.C:6
 listBox.C:7
 listBox.C:8
 listBox.C:9
 listBox.C:10
 listBox.C:11
 listBox.C:12
 listBox.C:13
 listBox.C:14
 listBox.C:15
 listBox.C:16
 listBox.C:17
 listBox.C:18
 listBox.C:19
 listBox.C:20
 listBox.C:21
 listBox.C:22
 listBox.C:23
 listBox.C:24
 listBox.C:25
 listBox.C:26
 listBox.C:27
 listBox.C:28
 listBox.C:29
 listBox.C:30
 listBox.C:31
 listBox.C:32
 listBox.C:33
 listBox.C:34
 listBox.C:35
 listBox.C:36
 listBox.C:37
 listBox.C:38
 listBox.C:39
 listBox.C:40
 listBox.C:41
 listBox.C:42
 listBox.C:43
 listBox.C:44
 listBox.C:45
 listBox.C:46
 listBox.C:47
 listBox.C:48
 listBox.C:49
 listBox.C:50
 listBox.C:51
 listBox.C:52
 listBox.C:53
 listBox.C:54
 listBox.C:55
 listBox.C:56
 listBox.C:57
 listBox.C:58
 listBox.C:59
 listBox.C:60
 listBox.C:61
 listBox.C:62
 listBox.C:63
 listBox.C:64
 listBox.C:65
 listBox.C:66
 listBox.C:67
 listBox.C:68
 listBox.C:69
 listBox.C:70
 listBox.C:71
 listBox.C:72
 listBox.C:73
 listBox.C:74
 listBox.C:75
 listBox.C:76
 listBox.C:77
 listBox.C:78
 listBox.C:79
 listBox.C:80
 listBox.C:81
 listBox.C:82
 listBox.C:83
 listBox.C:84
 listBox.C:85
 listBox.C:86
 listBox.C:87
 listBox.C:88
 listBox.C:89
 listBox.C:90
 listBox.C:91
 listBox.C:92
 listBox.C:93
 listBox.C:94
 listBox.C:95
 listBox.C:96
 listBox.C:97
 listBox.C:98
 listBox.C:99
 listBox.C:100
 listBox.C:101
 listBox.C:102
 listBox.C:103
 listBox.C:104
 listBox.C:105
 listBox.C:106
 listBox.C:107
 listBox.C:108
 listBox.C:109
 listBox.C:110
 listBox.C:111
 listBox.C:112
 listBox.C:113
 listBox.C:114
 listBox.C:115
 listBox.C:116
 listBox.C:117
 listBox.C:118
 listBox.C:119
 listBox.C:120
 listBox.C:121
 listBox.C:122
 listBox.C:123
 listBox.C:124
 listBox.C:125
 listBox.C:126
 listBox.C:127
 listBox.C:128
 listBox.C:129
 listBox.C:130
 listBox.C:131
 listBox.C:132
 listBox.C:133