I'd like to display text in a text box based on the value of a spin
control. Using EVT_SPINCTRL, I hooked up a method to update the
text box. This works fine when the user clicks on the spin arrows
or enters text directly. However, the event does not fire when
I call SetValue on the spin control within the program.
What's the normal way to handle this? Am I missing something? I don't
want to update the text box manually after a SetValue, because that
would result in duplicated code. I'd rather let the event handler
handle all updates; that's how I typically do it in GTK.
I thought about subclassing the spin control, and overriding the
SetValue method and having it send out a signal, but this seems like
overkill. I'm hoping there's a better solution.
I'd like to display text in a text box based on the value of a spin
control. Using EVT_SPINCTRL, I hooked up a method to update the
text box. This works fine when the user clicks on the spin arrows
or enters text directly. However, the event does not fire when
I call SetValue on the spin control within the program.
The simplest way that I see doing this, is to put all of your text-box-update code in a single parameter-less function/method, and have EVT_SPINCTRL call that method. This may be the only thing that the event handler does. The update method can draw all the information that it needs directly from the spin control, so it's really just a way of nudging the text box to say "do your thing". Now, when you call SetValue() on your spin control, you immediately follow that with a call to this update method.
The next option would be to manually construct an EVT_SPINCTRL event object and pass it to the appropriate event handler, but this would be a lot more work (and slower, and not as clear).
A third option would be to create a custom composite control containing both the spin control and the text box. Most of the composite control's events would be passed directly to the contained controls, but (for example) SetValue() would update both controls. This would probably be easiest if built on top of the first option, actually -- so that composite.SetValue() simply calls spinctrl.SetValue() and then text.Update(). (This is similar to your idea of subclassing SpinCtrl, but is a bit more versatile.)
Option three is probably worth the extra effort if you're going to be using this control setup extensively (or widely), especially if it's going to be used by multiple developers. If it's a single-use item, though, then you're probably best off going with option one.
Jeff Shannon
Technician/Programmer
Credit International
I'd like to display text in a text box based on the value of a spin
control. Using EVT_SPINCTRL, I hooked up a method to update the
text box. This works fine when the user clicks on the spin arrows
or enters text directly. However, the event does not fire when
I call SetValue on the spin control within the program.
What's the normal way to handle this? Am I missing something? I don't
want to update the text box manually after a SetValue, because that
would result in duplicated code. I'd rather let the event handler
handle all updates; that's how I typically do it in GTK.
It's not always successful because of platform differences, but the general policy is that changes to a control's value made by the user generates events and changes made programatically do not. So you should plan on SetValue not sending the event, (but also be aware that sometimes it may and act accordingly.)
···
--
Robin Dunn
Software Craftsman http://wxPython.org Java give you jitters? Relax with wxPython!