Combobox addlistener


#1

I want to add a listerer to see if the combobox content has changed ( for selecting a midi input ). I’ve the following code, but when debugging (VC++) it pop’s up juceasyncupdater.cpp

ComboBox* comboBox1;
ComboBoxListener* test;

comboBox1 = new ComboBox (T("combo"));
addAndMakeVisible (comboBox1);
comboBox1->addListener(test);
comboBox1->setBounds (10, 75, 200, 24);		

How do I need change the code to check if the listerer works ?

//void comboBoxChanged(ComboBox *combobox) //{ if(combobox==comboBox1) // { DBG (T("test")); // } //} [/code]


#2

Er yes, well if you pass it an uninitialised pointer as its listener, then I wouldn’t be too surprised if it crashes when it tries to call it!


#3

How can I initialise a pointer ? I’m really new in c++ programming, I’ve some trouble understanding pointers.

I thought I had initialised it with :

It might be really obvious, but I hope you would give me a hint.

A good en clear resource on understanding the use of pointers would also be helpfull.


#4

When you do

ComboBoxListener *test;

that’s just creates a pointer that points nowhere (NULL depending on a compiler i guess). Anyway it’s “nothing”. You need to initialize it with

ComboBoxListener *test = new ComboBoxListener();

but that’s propably not something you want in this case, you propably want to write your own class that’s derived from ComboBoxListener()

class MyClass : public ComboBoxListener
{
   public:
       /* whatever variables you need and
           whatever pure virtual methods need to be overloaded */
}

than write the code for the class to do the hard work and pass your class as a ComboBoxListener

MyClass *test = new MyClass();
comboBox1->addListener(test); 

and now your code is the listener. Neat ha ?


#5

Thank you. It think I get it.


#6