Showing "1 000" instead of "1000" in textbox.

OIkai, this might be very simple, but I have a textbox with "1000000" in it. And I want it to be shown as "1 000 000".

There must be a way to get the data out and change it, and then put it back in the textbox again.

Anyone know how

And question 2, I found a old vb6 code:

If KeyAscii > 26 Then If InStr("0123456789", Chr(KeyAscii)) = 0 Then KeyAscii = 0

Its for making a texbox accept numbers only, but it keeps giving me error on KeyAscii.
How can I make it work in vb exp





Answer this question

Showing "1 000" instead of "1000" in textbox.

  • xxfredxx

    Dim regex As Regex = New Regex("^[0-9]*$")

    If regex.IsMatch(txtNumber.Text) Then

    txtLength.Text = txtNumber.Text.Length

    Dim length As Integer

    length = txtNumber.Text.Length

    txtNew.Text = txtNumber.Text

    Dim three As Integer = 3

    If length >= three + 1 Then

    txtNew.Text = txtNumber.Text.Insert(length - three, " ")

    If length >= 2 * three + 1 Then

    txtNew.Text = txtNew.Text.Insert(length - 2 * three, " ")

    End If

    End If

    End If

    This is a very long winded way as you need to add a line of code for each group, but it works and ensures that only numbers are entered.

    import System.Text.RegularExpressions and create the three txtboxes from the code.


  • Sean Connolly

    Use a masked textbox to setup the format that you want displayed in the textbox...

    You can also use the following in a textbox to restrict user input to numeric values:

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    If Not IsNumeric(e.KeyChar) AndAlso Not Char.IsPunctuation(e.KeyChar) Then

    e.Handled = True

    End If

    End Sub



  • joseacta

    Try this:

    Imports System.Globalization
    Public Class Form1
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    Static busy As Boolean
    If busy Then Return
    busy = True
    Dim fmt As New NumberFormatInfo
    fmt.NumberGroupSeparator = " "
    Try
    With TextBox1
    Dim value As Decimal = Convert.ToDecimal(.Text, fmt)
    .Text = value.ToString("N0", fmt)
    .SelectionStart = .Text.Length
    End With
    Catch
    End Try
    busy = False
    End Sub
    End Class



  • frankjespo

    Bit tricky to do it in a line but the following works:

    Dim S1 As String = TextBox1.Text
    Dim S2 As String
    Dim X As Integer = S1.Length Mod 3
    S2 = S1.Substring(0, X)
    For count As Integer = X To S1.Length - 1 Step 3
    S2 = S2 & " " & S1.Substring(count, 3)
    Next
    TextBox1.Text = S2.Trim


  • vina

    DMan1 wrote:

    Use a masked textbox to setup the format that you want displayed in the textbox...

    You can also use the following in a textbox to restrict user input to numeric values:

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    If Not IsNumeric(e.KeyChar) AndAlso Not Char.IsPunctuation(e.KeyChar) Then

    e.Handled = True

    End If

    End Sub



    Yeah , but problem is if the mask is "# ### ###" then the result with 1000 will be:
    "1 000 ___", the "_" are annoying! So is there a line of code wich can separate my numbers instead


  • clint 2

    bankbalanse.Text = Trim(Format(CLng(bankbalanse.Text), "# ### ### ### ###"))

    It made this error:

    System.StackOverflowException was unhandled

    But the strange thing is, it only make that error when I add more than one of those codelines.
    If I like add another to another textbox, it makes that error.



  • ImGivingUp

    to get rid of the line after the control looses focus you can set the "HidePromptOnLeave" to true and "AllowPromptAsInput" = true....if this still does not give you the desired look then yes you will have to create a cusom mask with code



  • Martin1307

    You are probably getting the Change event to run over and over. Note the use of the "busy" variable in the code sample I gave.


  • villan_1

    Try this....

    TextBox2.Text = Trim(Format(CLng(TextBox1.Text), "# ### ### ### ###"))

    Dustin



  • Showing "1 000" instead of "1000" in textbox.