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 / Windows Forms / WinForm General / July 2006

Tip: Looking for answers? Try searching our database.

text box format

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John - 27 Jul 2006 19:48 GMT
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?
Herfried K. Wagner [MVP] - 27 Jul 2006 22:55 GMT
"John" <Johna@nospam.nospam> schrieb:
> 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.

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>

Linda Liu [MSFT] - 28 Jul 2006 09:02 GMT
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.
John - 28 Jul 2006 11:34 GMT
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,
>
[quoted text clipped - 35 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
Linda Liu [MSFT] - 28 Jul 2006 12:25 GMT
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
John - 31 Jul 2006 12:23 GMT
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,
>
[quoted text clipped - 12 lines]
> Linda Liu
> Microsoft Online Community Support
Linda Liu [MSFT] - 31 Jul 2006 13:14 GMT
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

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.