So anyway, during work today I was assigned a bug that had to do with the color picking menu not working properly. Turns out it was working fine, the handler for entering text was just registered to handle text entry after the user hit "enter". I thought this would be an easy, one-line fix, I just had to change the message ID the handler was registered for.
Turns out that it wasn't that easy (nothing ever is). You see, once text was entered we were calling another function that updated the text fields with the new value. This had to stay in to update the text fields if the user entered an invalid number. Problem was, in that function we called the SetValue() function for the wxTextControls. This in turn generated a wxTextEntry message (or whatever the actual message ID is), which called our event handler, which called the function to update the text fields...
The real solution here is to call ChangeValue instead of SetValue, which updates the text control without generating a text entry event, but that wasn't implemented in the version of wxWidgets that we use at work, so no go on that front. The first thing that came to mind was to set a global flag in the UpdateTextBoxes function, and to immediately return from the event handler if it was set. It wasn't pretty by any means, but it worked.
After going through the usually-excellent-but-in-this-case-laking documentation, I figured out a (in my mind) much cleaner solution to the whole problem. At first, we were setting the event handler with the EVT_COMMAND macro, which statically registers a handler for the lifetime of the program. I changed this to being set dynamically, via the Connect function. That way, when we are updating the text controls, all I have to do is Disconnect() the handler, make the SetValue() calls, then re-Connect() the handler.
It's not the best solution, but it's a lot cleaner than messing about with global flags and whatnot.
So, the moral of the story is, if the only way you can think of to solve a bug is to hack together some code, think harder. And do more research. There is always a cleaner solution out there somewhere.
Sure, there is a better way to do that: don't change this damn control value [smile].
I mean, the user it typing something inside, and there is not much reason to change it after that. From a GUI design point of view, if you want to display more textual information based on the user input, you'd better write the information in a label near the text box - that's probably cleaner.
Or maybe I missed something. What is you reason to do so?