[...]
You can, however, achieve the padding by overriding OnTextBoxChanged
protected override void OnTextBoxTextChanged(object source, EventArgse)
{
Text = Value.ToString("00");
No go. This statement throws a StackOverflowException.
I'm not a big fan of any of the suggestions. At some point, it seems like
it'd be better to just reimplement the control as a composite control,
handling the connection between the value and the displayed text
yourself. Just put a TextBox and a couple of buttons together yourself.
However, in the spirit of the hacking, I'll suggest that the above
technique _might_ work if you protect the assignment with a flag that
tracks whether you're actually trying to set the text. That is, the stack
overflow happens because you've got unterminated recursion, since the
method that handles a change in the text itself causes a change in the
text, causing the method to be called again.
So set a flag when you're changing the text and don't change it again if
you're already changing it:
bool _fChangingText;
protected override void OnTextBoxTextChanged(object source, EventArgs
e)
{
base.OnTextBoxTextChanged(source, e);
if (!_fChangingText)
{
_fChangingText = true;
Text = Value.ToString("00");
_fChangingText = false;
}
}
The reason I say "_might_" is that I don't know off the top of my head
what kinds of things the control does when you set the Text property. I
think it should work, but if the control starts doing something with the
text to parse or otherwise evaluate and reformat it, it could just wind up
overwriting whatever you put in there.
Pete