473,473 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Adding leading zeros to NumericUpDown Control

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?
Mar 16 '08 #1
4 4374
Joe,

Why not simple the standard key up event, there are so many events for this
and you to the difficult route?
Cor
"Joe Cool" <jo*****@home.netschreef in bericht
news:fh********************************@4ax.com...
>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?
Mar 17 '08 #2
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.

--
Happy Coding!
Morten Wennevik [C# MVP]
"Joe Cool" wrote:
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?
Mar 17 '08 #3
On Sun, 16 Mar 2008 23:30:01 -0700, Morten Wennevik [C# MVP]
<Mo************@hotmail.comwrote:
>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.
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.
Mar 17 '08 #4
On Sun, 16 Mar 2008 23:30:01 -0700, Morten Wennevik [C# MVP]
<Mo************@hotmail.comwrote:
>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");
No go. This statement throws a StackOverflowException.
>}

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.
Mar 17 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: david | last post by:
Hi, I have an application as follows: MySQL database Back-Eend linked to MS Access Front-End and ASP Web Application. I require users to enter Serial Numbers such as: 0105123567 (10...
15
by: Stormkid | last post by:
Hey Gang, I'm trying to figure out the best way to add two times together of the format hh:mm:ss any suggestions would be great thanks Todd
5
by: samik_tanik | last post by:
I need to export a datagrid to Excel. I could did this. But, also need to keep the leading zeros in the data. How can I acheive this? Any help would be appreciated. -- Thanking you in...
5
by: OneDay | last post by:
I've got a field that has some old data with text in it, but all forward data will be a 3 digit number. But many of the numbers are still only 2 digits. I would like to force the leading zero in...
3
by: abc my vclass | last post by:
My win-form have many numericupdown controls to applied. But numericupdown control don't like textbox, text box control can automatic selected text when got focus. Is there any method can let me...
5
by: GarryJones | last post by:
I have code numbers in 2 fields from a table which correspond to month and date. (Month, Code number) Field name = ml_mna 1 2 3 etc up to 12 (Data is entered without a leading zero)
6
by: JimmyKoolPantz | last post by:
Task: Customer wants a script of the data that was processed in a "CSV" file. Problem: Zip-Code leading zeros are dropped Basically we have a client that has requested a custom script for...
2
by: cmdolcet69 | last post by:
Does anyone know how to set a property to display the leading zeros in the numberic updown control for vb 2005? I'm using the control and want to input a value of 01 however it will just show 1?
4
by: Joe Cool | last post by:
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...
0
by: Monty | last post by:
Hi All, I am having a problem with leading zeros being stripped from fields in a CSV file when I bring them in using Jet/OleDB. In VB.Net/VS 2008, I am accessing a CSV file like so: sSQL =...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.