How to format a textbox while typing in vb.net

I have 200+ text fields in a vb.net application. Let me clarify that these are all simple text boxes. now consumer demand has a formatted numeric value during input or browsing. With Format () I can play to view, but in add / edit mode in the textbox (while the user enters a value) nothing happened I want this result 1234567.0090 to 1 234 567.0090 during input.

or guide me in any way that I change all the textboxes to mask the textboxes with any tool or code.

Any help is appreciated. Thanks in Advance.

+2


source to share


4 answers


First, I would strongly recommend that you try to convince your client of this requirement. Masked text fields in general are a royal pain in the butt, for both the programmer and the end user. In my opinion, if you have to format user input, it is much better to format whatever they have entered after the control loses focus than to try to format them while they are still typing it.

Either approach, the easiest way to do this is to create your own custom control (unless you want to use a third-party control, which I would not recommend for this purpose for a variety of reasons) that inherits from TextBox (instead of inheriting from UserControl). If you want to format the text after the user has finished typing and navigated to another control, you can add an EventHandler to your LostFocus element and format them there.

If, however, you want to format when you type, you have a couple of terrible solutions. First, you can handle KeyPress or KeyDown controls, and catch and uncap non-numeric characters, or format the general Text property at this time. This is a common approach and often does not work in unexpected ways because it is not related to the text that is copied and pasted into the control (which happens quite often in data-entry applications).

An alternative approach is to handle the TextChanged event, which will respond to both keyboard input and inserted text, and reformat the text on the fly. Since you frequently change text as you type, your code should pay attention to the SelectionStart property (among others) so that you don't unexpectedly change the caret position as the user enters. Also, when you change the property of a text control while formatting it, that change itself will trigger another TextChanged event, so you need to be careful not to get stuck in an infinite loop.

To echo my main point, you will be much happier formatting in the LostFocus event, and your end users will too.



Once you've written your control, you can simply make a global override in your code by replacing "MyMaskedTextBox" with "TextBox" (case sensitivity is recommended here).

Update: Here is some simple parsing / formatting code you can use on the TextBox LostFocus event:

double d;
TextBox tb = (TextBox)sender;
if (double.TryParse(tb.Text, out d))
{
    tb.Text = d.ToString("#,###,###,###.0000");
    tb.BackColor = SystemColors.Window;
}
else
{
    tb.BackColor = Color.Red;
}

      

This code will format the user input as a number the way you need it if the entered text can be parsed as double. If the input is not a valid double, the text remains as is and the BackColor is changed to red. This is a good way to indicate invalid input to the user (as opposed to bubbling up the MessageBox).

+7


source


Override these events in a custom custom element with a text box. But, remember, no formatting as they print,



Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
    MyBase.OnLostFocus(e) 
    Me.Text = Strings.FormatNumber(Me.Text, _
                                      m_FormatNumDigitsAfterDecimal, _
                                      m_FormatIncludeLeadingDigit, _
                                      m_FormatUseParensForNegativeNumbers, _
                                      m_FormatGroupDigits)
End Sub
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
    MyBase.OnTextChanged(e)
    If Me.Focused = False Then
        Me.Text = Strings.FormatNumber(Me.Text, _
                                       m_FormatNumDigitsAfterDecimal, _
                                       m_FormatIncludeLeadingDigit, _
                                       m_FormatUseParensForNegativeNumbers, _
                                       m_FormatGroupDigits)
    End If
End Sub

      

0


source


This is a different method.

Private Sub TBItemValor_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TBItemValor.KeyPress
        If (Char.IsDigit(e.KeyChar) = False AndAlso Char.IsControl(e.KeyChar) = False AndAlso Char.IsPunctuation(e.KeyChar) = False) OrElse Not IsNumeric(Me.TBItemValor.Text & e.KeyChar) Then
            e.Handled = True
        End If
    End Sub

      

0


source


Public Sub checktextbox2(txt As TextBox)
dim bg as string
For t = 1 To txt.Text.Length
        If txt.Text.Chars(txt.Text.Length - (txt.Text.Length - t)) = "." Then
            bq = txt.Text.TrimEnd(New String({"0", "."}))
            txt.Text = bq
            Exit For
        End If
    Next
end sub

      

this will format the number in the textbox as ###. ###

0


source







All Articles