2 decimal places

The value in Text1 from database is 2.923, for example. It needs to be displayed with only 2 decimal places like:

2.92

me.Text1.Text=2.923.ToString("0.00")

should work.

Q1: What event should I place this code such that upon navigation from one record to another it will fire.

Q2: But because the value is changing from one record to another, how could it be done; maybe something like: me.Text1.Text=me.Text1.Text.ToString("0.00") ?

Could this be handeled via the property setting of Text1? If so, what property and the setting in it.

I am using vs2010.

Thank you.

BTW, Text1_TextChanged() event seems to be working ok.
LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shahan AyyubSenior Software EngineerCommented:
First of all, as you said:

me.Text1.Text=2.923.ToString("0.00")

will work as you can see here also.

Ans1: You can handle Text1_TextChanged event as you are using i guess. Since when you set text property of a TextBox control this event fires. So it is useful.

Ans2: You can also use string.Format() as well. See this relevant thread.
0
ChloesDadCommented:
You need to be careful about changing text in a text changed event handler as you can end up in an infinite loop.

Other methods would be to use a numeric up down control that can be formatted to 2dp and then you don't need to do any code at all. This also has the benefit that the user cannot enter a value that has more than 2dps as well.
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
How "numeric up down control" works?
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

ChloesDadCommented:
It has two properties, text as string and value as decimal, setting either updates the control, so you would bind the text property to your database.

It then has a property decimalplaces that can be set to 2. You also need to set the maximum and minimum properties, I normally set these to -999999999999 and +9999999999 if I don't need to limit to a specific range of values.

http://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown.aspx
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I though it will be in front-end.

I agree with you textChanged event is no good. Someone suggested using lost focus event of the form. Apparently during navigation from one record to another it will fire. I will check it and let you know.
0
ChloesDadCommented:
Lost focus will only fire when the text box loses focus. I would not expect this to fire when the record changes from one to another as the text box may not have the focus at the time. Although a recordchanged event or something similar will fire.
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I didn't mean lost focus of the text box
0
ChloesDadCommented:
oops, sorry. I see that you said the lost focus of the form, this will only fire when control moves from one form to another
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
You are right. But, the person saying lost focus works is sort of knowledgeable person. There may be a point I am missing. I will find it out and let you know.
0
ElrondCTCommented:
I'm rather late to the party here, but maybe I can offer some helpful advice.

While you do need to be careful about an infinite loop, it's not that big a deal. Just set a flag variable, and immediately exit the event handler if the flag is set:

    Private blnNoRace as Boolean

    Private Sub Text1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Text1.TextChanged
        If blnNoRace Then
            Return
        End If
        Dim str2Digit as String = cDbl(Text1.Text).ToString("0.00")
        If str2Digit <> Text1.Text Then
            blnNoRace = True
            Text1.Text = str2Digit 
            blnNoRace = False
        End If
    End Sub

Open in new window


Note that this will also add decimals to numbers that don't previously have it: 3.1 will become 3.10, 55 will become 55.00. If you don't want that, then you should make the comparison something like:

        Dim dbl2Digit as Double = cDbl(Text1.Text)
        If Math.Round(dbl2Digit, 2) <> dbl2Digit Then
            blnNoRace = True
            Text1.Text = dbl2Digit.ToString("0.00") 
            blnNoRace = False
        End If

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.