text box format

  • Thread starter Thread starter John
  • Start date Start date
J

John

What method is considered the best way to ensure users enter the correct
data in a text box if you want an integer or decimal?
 
John said:
What method is considered the best way to ensure users enter the correct
data in a text box if you want an integer or decimal?

Check out NumericUpDown and MaskedTextBox (.NET 2.0) controls.
 
Hi John,

Thank you for posting.

I agree with what Herfried K. Wagner said. .NET 2.0 has provided
MaskedTextBox for inputing some specific data format.

You could drag&drop MaskedTextBox onto your form, select it and click the
button at the Mask part in Properties window. In the Input Mask window,
select <Custom> item in the listbox and input "999999999" in the Mask
textbox. Press Ok.

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
It seems alright except if I want the user to be able to enter a decimal the
only way I found to do it is I put it in for them. I suspect they will not
like being forced to enter zeros or such instead of just typing he number.
Is there a mask that doesn't force the decimal into a position but allows
them to use one?
 
Hi John,

I think you could add a "." in the Mask string of the MaskedTextBox in
order to input a decimal in the MaskedTextBox.

For example, you may set the Mask property of the MaskedTextBox to
"999999999.999999999". Thus, when the program is running, there's a decimal
in the MaskedTextBox.

Hope this helps.
If you have any concerns, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
No it doen't work. it puts a decimal in one particular location. So if the
user wants to type "24.5625" and I use the mask you gave the user gets
something like 245625___._________
Am I missing something?
 
Hi John,

Yes, you are right. Using MaskedTextBox, we could only put a decimal or
something else in one particular location. In this case, it's up to the
users to input the numbers correctly.

If you don't like this solution, I think you could derive a new class from
TextBox and override the OnKeyPress method in the new class. The following
is a sample for you.

class MyTextBox:TextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{

if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar ==
'.'|| e.KeyChar == '\b')
{
if (e.KeyChar != '.')
{
base.OnKeyPress(e);
}
else
{
if (this.Text.IndexOf('.') < 0)
{
base.OnKeyPress(e);
}
else
{
e.Handled = true;
}
}
}
else
{
e.Handled = true;
}

}
}


You should add this MyTextBox onto the form. When the program is running,
users could only input numbers and one decimal into the MyTextBox.

Hope this helps.
If you have anything unclear, please feel free to tell me.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
Back
Top