Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

TextBox Format

Posted on 2010-08-19
6
Medium Priority
?
203 Views
Last Modified: 2013-11-14
Hi,
I have a textbox.
In this textbox, the user enters miles information. This would be a decimal. Right now he can enter any value
123.456 or 1234.56 or 12.3456
I want this to be restricted to 9 digits i did this by setting the max length to 9.
Now i want this to have only 2 decimals
the user should be entering the text in this format 123456.78
The digits to the left of decimal should not be greater than 6
Is there any way i can do this without using a masked text box?

Thanks
0
Comment
Question by:rowternet
6 Comments
 
LVL 27

Assisted Solution

by:nmarun
nmarun earned 375 total points
ID: 33477925
If this is a winforms application, you can add a handle to the keypressed event and then take control of what the user enters in the textbox.You can also do this for web applications, but you'll have to use javascript for that.Arun
0
 
LVL 5

Assisted Solution

by:jijeesh
jijeesh earned 375 total points
ID: 33478109
Try using RegEx and refer below article
http://www.csharphelp.com/2006/04/masked-c-textbox-control/
0
 

Author Comment

by:rowternet
ID: 33478531
Hi nmarun,

I already have a key press event in place which prevetns the user from entering any junk character.
What i need is :
Whenever the user enters something it should take 2 decimals.
jijeesh:
I cannot change my controls at this point.

On the textbox there is a property called Precision adn i set it to 2 decimals.
But, it does not seem to do anything.
Any idea what this is for?



Thanks
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
LVL 17

Assisted Solution

by:Zhaolai
Zhaolai earned 375 total points
ID: 33478623
Add this code in your textbox's KeyDown event:


        Dim oTextBox As TextBox = CType(sender, TextBox)
        If oTextBox.Text.Contains(".") Then
            If e.KeyCode = Keys.Decimal Then
                e.SuppressKeyPress = True
                Exit Sub
            End If
            If oTextBox.Text Like "*.##" Then
                e.SuppressKeyPress = True
            End If
        End If

Open in new window

0
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 375 total points
ID: 33481044
Alternative Method.
You already said that you are using Key_Press Event ....Put  e.Handled = DesiredFormat(sender, e.KeyChar)
and have no junk character..The Function Do what you want
Hope it helps


Public Function DesiredFormat(ByVal sender As System.Object, ByVal eChar As Char) As Boolean
        Dim CheckString As String = "0123456789."
        If CType(sender, TextBox).TextLength = 7 AndAlso Not CType(sender, TextBox).Text.Contains(".") And eChar <> vbBack Then
            Return True
        ElseIf CType(sender, TextBox).TextLength > 9 And eChar <> vbBack Then
            Return True
        ElseIf CType(sender, TextBox).TextLength < 6 AndAlso CType(sender, TextBox).Text.Contains(".") And eChar <> vbBack Then
            Return True
        Else
            If CheckString.IndexOf(eChar) > -1 OrElse eChar = vbBack Then
                If CType(sender, TextBox).Text.IndexOf(".") > -1 Then
                    If CType(sender, TextBox).TextLength - CType(sender, TextBox).Text.IndexOf(".") > 2 And eChar <> vbBack Then
                        Return True
                    End If
                End If

                If eChar = "." Then
                    If CType(sender, TextBox).Text.IndexOf(eChar) > -1 Then
                        Return True
                    Else
                        Return False
                    End If
                End If
                Return False
            Else
                Return True
            End If
        End If
    End Function

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        e.Handled = DesiredFormat(sender, e.KeyChar)
    End Sub

Open in new window

0
 

Author Comment

by:rowternet
ID: 33618987
Hi,

I did not get a chance to look at this.
I will change it in a couple of days.

Thanks
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question