开发者

DisplayFormat/DisplayStyle property of TextBox (Visual Studio 2010, .Net Framework 4)

Is there any predefined property by which I can set the DisplayFormat/DisplayStyle of a textbox? Suppose I want a textbox to hold the actual value 8开发者_开发问答88888888888.88 and show it formatted as 888,888,888,888.88. In short it should hold 'Double' data type value and show it with thousand separators and decimal places upto two digits. If text is cleared from the textbox, it should have 0 as value and 0.00 as displayed value. Also the number of digits in the textbox may vary accordingly. So how can it be done? Please help. Regards.


You could probably use MaskedTextBox.

Or you can format the string yourself:

double d = 123456789.10
textBox.Text = d.ToString("#,#0.00");

However the textbox value will always be a string, not a double. You'd have to convert it back to double:

double d = Convert.ToDouble(textBox.Text);


Create your own class for the textbox. Below you find an example, that allows only to enter numbers, a decimal point, the backspace and the delete key.

The editing might be enhanced (maintain current cursor position?), but it works as I wrote it.

Add the class once and create the textbox in the FormLoad. Save time: create a fake textbox where you would like to have yours in the form, copy all details from the designer to the form load and delete the fake textbox

I added a button only to be able to see the value in the textbox.

Public Class Form1
Friend WithEvents dTextbox1 As dTextbox
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Me.dTextbox1 = New dTextbox()
    Me.dTextbox1.Location = New System.Drawing.Point(278, 122)
    Me.dTextbox1.Name = "dTextBox1"
    Me.dTextbox1.Size = New System.Drawing.Size(79, 20)
    Me.dTextbox1.TabIndex = 2
    Me.dTextbox1.Visible = True
    Me.Controls.Add(Me.dTextbox1)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    MsgBox(CStr(dTextbox1.Value))
End Sub
End Class
Class dTextbox
Inherits TextBox
Friend Value As Double
Dim DecimalWasEntered As Boolean
Dim DecimalPlaces As Integer
Dim ChangedByProgram As Boolean
Dim KeyAlreadyHandled As Boolean
Sub New()
    Value = 0
    DecimalWasEntered = False
    DecimalPlaces = 0
    ChangedByProgram = False
    KeyAlreadyHandled = False
End Sub
Private Sub dTextbox_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    ChangedByProgram = False
    If KeyAlreadyHandled Then
        e.Handled = True
    ElseIf e.KeyChar = "." Then
        DecimalWasEntered = True
        e.Handled = True
        DisplayText()
    ElseIf DecimalWasEntered Then
        DecimalPlaces += 1
        If DecimalPlaces > 2 Then DecimalPlaces = 2
    End If
End Sub
Private Sub dTextbox_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    Dim MeTextInProgress As String
    KeyAlreadyHandled = False
    If e.KeyCode = Keys.Delete Or e.KeyCode = Keys.Back Then
        MeTextInProgress = Text
        If SelectionLength = MeTextInProgress.Length() Then
            DecimalWasEntered = False
            DecimalPlaces = 0
            Value = 0
        ElseIf SelectionLength > 0 Then
            MeTextInProgress = MeTextInProgress.Remove(SelectionStart, SelectionLength)
            ConvertToDouble(MeTextInProgress)
        Else
            If Not DecimalWasEntered Then
                MeTextInProgress = MeTextInProgress.Remove(MeTextInProgress.Length() - 4, 1)
            Else
                MeTextInProgress = MeTextInProgress.Substring(0, Text.Length() - 3 + DecimalPlaces)
                DecimalPlaces -= 1
                If DecimalPlaces = 0 Then DecimalWasEntered = False
            End If
            ConvertToDouble(MeTextInProgress)
        End If
        DisplayText()
        e.Handled = True
        KeyAlreadyHandled = True
    End If
End Sub
Private Sub TextBox2_TextChanged(sender As System.Object, e As System.EventArgs) Handles Me.TextChanged
    If ChangedByProgram Then Exit Sub
    ConvertToDouble(Text)
    DisplayText()
End Sub
Sub ConvertToDouble(t As String)
    Dim d As Double
    Try
        d = CDbl(t)
        Me.Value = d
    Catch ex As Exception
        Beep()
    End Try
End Sub
Sub DisplayText()
    ChangedByProgram = True
    Me.Text = Me.Value.ToString("#,##0.00")
    If DecimalWasEntered Then
        Me.SelectionStart = Me.Text.Length() - 2 + DecimalPlaces
    Else
        Me.SelectionStart = Me.Text.Length() - 3
    End If
End Sub
End Class
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜