Getting all entries from TGListBox

Hi,

I’m have a TGListBox in which I can select multiple choices… How do I get what was selected?

I’m assuming it’s

TList* selected = new TList;
listBox->GetSelectedEntries(selected);

But when what do I do from there?

Thanks.

Hi,

Depends on what you want to do… Maybe something like this :

   listbox->GetSelectedEntries(selected);
   TGTextLBEntry *entry;
   TIter next(selected);
   while ((entry = (TGTextLBEntry *)next())) {
      TString name = obj->GetTitle();
      // Do whatever you want...
   }

Cheers,
Bertrand.

Hi,

Thanks for the reply.

I did that:

TGListBox *fListBox;
[…]
TGListBox fListBox = new TGListBox(hframe, 90);
fListBox->AddEntry(“hi”, 1);
fListBox->Resize(150,80);
hframe->AddFrame(fListBox, new TGLayoutHints(kLHintsCenterX, 5, 5, 3, 4));
[…]
TList
selected = new TList;
fListBox->GetSelectedEntries(selected);
TIter next(selected);
while ((entry = (TGTextLBEntry *)next())){
TString name = obj->GetTitle();
}

When that last bit of code is executed, I get one of two things:
If I selected nothing in the list box, I get:
root [0] .L numberEntry.C
root [1] example()
root [2] Error: illegal pointer to class object fListBox 0x0 795 FILE:numberEntry.C LINE:170
*** Interpreter error recovered ***

If I selected something in the list box, I get:
root [0] .L numberEntry.C
root [1] example()
root [2]
*** Break *** segmentation violation
Generating stack trace…
[…]

Thanks again,
Mikhail

Hi Mikhail,

Please attach - or send me ( bertrand.bellenot@cern.ch ) your code or a short macro reproducing the problem.

Cheers,
Bertrand.

Hi,

After a second look, I realized that you define fListBox twice :

TGListBox *fListBox;
[…]
TGListBox *fListBox = new TGListBox(hframe, 90);

First of all, you should avoid this kind of double definition…
If you still have problems, please send the code.

Cheers,
Bertrand.

Here it is… run with:

root [0] .L numberEntry.C
root [1] example()

I commented out any other dependencies.

Thank you very much,
Mikhail
numberEntry.C (6.01 KB)

Hi Mikhail,

If you declare your listbox as a class member :

class MyMainFrame {
[…]
public:
TGListBox *fListBox;

You MUST NOT redeclare it later in the code:

TGListBox *fListBox = new TGListBox(hframe, 90);

but just initialize (create) it:

fListBox = new TGListBox(hframe, 90);

Because the second definition becomes invalid when going out of scope, and the class member is still not initialized.

Cheers,
Bertrand.

Hi Bertrand,

Good call.

One other question though… what does this line do? TString name = obj->GetTitle();
What is obj?

Definitely seems to crash my little application if you run it
Root[] example()
select something in dropdown
press go…

I don’t think obj was ever declared anywhere? What is that?

Thank you,
Mikhail

Never mind, I figured it out… Should be:

TList* selected = new TList;
fListBox->GetSelectedEntries(selected);
TIter next(selected);
TString name;
while ((entry = (TGTextLBEntry *)next())){
	name = entry->GetTitle();
}

Thanks again

Ooops! Sorry, it was a typo. The correct code is :

Cheers,
Bertrand.