Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format number #0+00.00 1

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,486
5
38
US
I have a need to display (format) a number certain way.

[pre]
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
[/pre]
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.
 
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.
 
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):

Code:
[blue]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[/blue]
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top