Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Adding leading zeros to NumericUpDown Control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joe Cool - 16 Mar 2008 22:20 GMT
I am trying to force a numericupdown control to display leading zeros.
The maximum is less than 100, so the displayed value will always be a
two digit number or less. So I have overridden the OnValueChanged
Event with the following code;

       protected override void OnValueChanged(EventArgs e)
       {
           base.OnValueChanged(e);
           if (Value < 9)
               Text = Value.ToString("00");
       }

But it doesn't work. Can anyone spot the problem?
Cor Ligthert[MVP] - 17 Mar 2008 06:00 GMT
Joe,

Why not simple the standard key up event, there are so many events for this
and you to the difficult route?

Cor

>I am trying to force a numericupdown control to display leading zeros.
> The maximum is less than 100, so the displayed value will always be a
[quoted text clipped - 9 lines]
>
> But it doesn't work. Can anyone spot the problem?
Morten Wennevik [C# MVP] - 17 Mar 2008 07:30 GMT
Hi Joe,

ValueChanged is fired when the Value property is changed, and any Text
changed at this point would get overwritten by the Control at a later stage.

You can, however, achieve the padding by overriding OnTextBoxChanged

protected override void OnTextBoxTextChanged(object source, EventArgs e)
{
   Text = Value.ToString("00");
}

but you will lose the ability to manually enter the value.

If you change to Text = Text.PadLeft(2, '0'); you can manually enter a
value, but you are no longer limited to maxvalue, and the user input is not
logical, but you may  be able to work something out.

Signature

Happy Coding!
Morten Wennevik [C# MVP]

> I am trying to force a numericupdown control to display leading zeros.
> The maximum is less than 100, so the displayed value will always be a
[quoted text clipped - 9 lines]
>
> But it doesn't work. Can anyone spot the problem?
Joe Cool - 18 Mar 2008 00:28 GMT
>Hi Joe,
>
[quoted text clipped - 9 lines]
>
>but you will lose the ability to manually enter the value.

That is fine with me.

>If you change to Text = Text.PadLeft(2, '0'); you can manually enter a
>value, but you are no longer limited to maxvalue, and the user input is not
>logical, but you may  be able to work something out.

Thanks for your informative reply.
Joe Cool - 18 Mar 2008 00:37 GMT
>Hi Joe,
>
[quoted text clipped - 6 lines]
>{
>    Text = Value.ToString("00");

No go. This statement throws a StackOverflowException.

>}
>
[quoted text clipped - 3 lines]
>value, but you are no longer limited to maxvalue, and the user input is not
>logical, but you may  be able to work something out.
Peter Duniho - 18 Mar 2008 03:46 GMT
> [...]
>> You can, however, achieve the padding by overriding OnTextBoxChanged
[quoted text clipped - 4 lines]
>
> 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
Joe Cool - 19 Mar 2008 00:16 GMT
>> [...]
>>> You can, however, achieve the padding by overriding OnTextBoxChanged
[quoted text clipped - 9 lines]
>handling the connection between the value and the displayed text  
>yourself.  Just put a TextBox and a couple of buttons together yourself.

Are you saying to have three controls on the form, a TextBox and two
Buttons? Or are you saying to create a UserControl that inherits from
the TextBox control and add a couple of buttons to it?

I would prefer the latter as it makes for a contorl that is easily
used in other projects. I looked into this briefly and cannot seem to
get the buttons to appear. The UserControl Designer looks different
from a Form Designer, and I have used code in the Form Designer to
figure out how to dynamically add controls to a form. And I have used
a UserControl that inherits from the NumericUpDown that adds rollover
and rollunder functionality, but I am having problems adding other
controls, like a couple of buttons to a TextBox.

Can you point me to a resource that can help with this understanding?
I have already done a Google search and checked with CodeGuru and
CodeProject with no luck.

>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  
[quoted text clipped - 20 lines]
>         }
>     }

I tried this and it doesn't seem to work.

>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  
[quoted text clipped - 3 lines]
>
>Pete
Peter Duniho - 19 Mar 2008 00:52 GMT
> Are you saying to have three controls on the form, a TextBox and two
> Buttons? Or are you saying to create a UserControl that inherits from
> the TextBox control and add a couple of buttons to it?

The latter, as you prefer.  The NumericUpDown control (ultimately)  
inherits from ContainerControl, rather than UserControl.  If you didn't  
need anything that UserControl added (and you probably don't), then you  
could just inherit from ContainerControl.  But using UserControl does  
provide Designer support for layout out the control, so that may be  
preferable.

> I would prefer the latter as it makes for a contorl that is easily
> used in other projects. I looked into this briefly and cannot seem to
> get the buttons to appear.

Well, normally it "just works".  It's not much different from designing a  
Form...there's just no title bar, etc.

> The UserControl Designer looks different
> from a Form Designer, and I have used code in the Form Designer to
> figure out how to dynamically add controls to a form. And I have used
> a UserControl that inherits from the NumericUpDown that adds rollover
> and rollunder functionality, but I am having problems adding other
> controls, like a couple of buttons to a TextBox.

I don't understand what you mean by "a UserControl that inherits from the  
NumericUpDown".  That's not possible, since both are already classes in  
.NET and C# doesn't support multiple inheritance.  You can't have a class  
that inherits both UserControl and NumericUpDown.

> Can you point me to a resource that can help with this understanding?
> I have already done a Google search and checked with CodeGuru and
> CodeProject with no luck.

Unfortunately, I don't have any specific leads.  I've never had any  
trouble using the VS Designer to create a UserControl.  If you have tried  
and have some specific thing that's not working for you, we can try to  
offer advice here.  But it's really fairly simple, and so it doesn't  
surprise me that there's no obvious reference to walk you through it.  If  
you know how to use the Designer generally, then creating a  
UserControl-derived class is pretty much the same.

Pete

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.