ofxButton addlistener calls event repeatedly

Hi,

I’m trying to add an event listener for a button and it works 90% of what I need it to do but it gets called over and over again and I cant seem to figure out why.

Here are the functions that are giving me pain:

void ofApp::setup(){
	button.addListener(this, &ofApp::button_pressed);
	gui.setup();
	gui.add(radius.setup("radius", 140, 10, 300));
	gui.add(button.setup("Select file"));
}
........
void ofApp::button_pressed(){
		const auto result = ofSystemLoadDialog("Select input file");
		if (result.bSuccess){
			ofLogNotice() << "testButtonChange: release" << endl;
		}

}

This will repeatedly open the file load dialog box and I cant seem to figure out why. I only want it called when the button is pressed?

Try yo declare your button as follows in ofApp.h;
ofParameter<void> button = {"Select file"};
then the rest is same. But do not initialize button in add method this time;

void ofApp::setup(){
	button.addListener(this, &ofApp::button_pressed);
	gui.setup();
	gui.add(radius.setup("radius", 140, 10, 300));
    gui.add(button); // No need to initialize the button object here, you did it in your header file
}
1 Like

Hi,
Thanks for the reply, I tried made the change you suggested but the error still persists where its repeatedly opening the file dialog.

hey @proffparadox, your code is working fine here.
I just declared into ofApp.h like this:

		ofxButton button;
		void button_pressed();
		ofxFloatSlider radius;
		ofxPanel gui;

The code from @alp is working here too.
So there’s another problem there. Did you declare the variables and methods like me into ofApp.h?

1 Like

Hi, So I’ve fixed the problem, turns out I left the old code from before I added the event listener in the draw function so it was getting called from draw.

oops.

Thanks for all the help though!