Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Format number #0+00.00

Format number #0+00.00

Format number #0+00.00

I have a need to display (format) a number certain way.

Type this:    Display this:
       0        0+00.00
       1        0+01.00
     1.5        0+01.50
      20        0+20.00
   35.75        0+35.75
  123400     1234+00.00
55566.75      555+66.75
I can do it in Excel with Custom Format: #0+00.00 which allows me to enter the number into a cell, and then – when I leave this cell – format the display the way I need. The number itself – of course – does not change, just the display of it. I can come back to this cell to modify the number.

I want the same functionality in VB.NET 2013 Windows Form.

I did try the masked text box with different masks, I didn’t get any of them right.

I also need to display the numbers with this format in the DataGridView

Do I need to create one Function where I pass the number and return the formatted String? And another Function where I pass a String (555+66.75) and return the number 55566,75 (if I need to modify it)?

Any help is appreciated.

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.

RE: Format number #0+00.00

I would create a my own TextBox with a Private _Value property. Override the Text property and in its Set method to place the input value into the _Value property and format the the value and place the result in the Text property. Don't allow editing of the Text Property in the PropertyGrid (there's an Attribute for this - DesignerVisible or something similar, I think) and then retriieve the private _Value property in the Get method.

I've not done this, but I think with a bit of care it would work.

RE: Format number #0+00.00

Something like the following (apologies for the slightly cumbersome formatting function; it is pulled from a different project where I was emulating the VBA formatting function):


Public Class Form2
    Private Sub TextBox1_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox1.GotFocus
        TextBox1.Text = TextBox1.Tag
    End Sub

    Private Sub TextBox1_LostFocus(sender As Object, e As System.EventArgs) Handles TextBox1.LostFocus
        TextBox1.Tag = TextBox1.Text
        TextBox1.Text = vbaFormat(Val(TextBox1.Tag), "#0+00.00")
    End Sub

    Private Function vbaFormat(Expression As Object, Format As String)
        vbaFormat = CoreFormat("{0:" & Format & "}", Expression)
    End Function

    ' Allows more of the .NET formatting functionality to be used directly if required
    Private Function CoreFormat(Format, Expression)
        CoreFormat = Expression
        On Error Resume Next
        With CreateObject("System.Text.StringBuilder")
            .AppendFormat(Format, Expression)
            If Err.Number = 0 Then CoreFormat = .ToString
        End With
    End Function
End Class 

RE: Format number #0+00.00

Thank you strongm

Works like a charm with a text box.

I hope I can use it also with modifying the display of the number in the DataGridView, I just need to play with it. I hope to exchange Tag with a String variable to achieve the same outcome.

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close