none
How To Automatically Insert Commas After Every Word In Textbox??? - visual basic insert commas in text, vb 2008 textboxes

Answers

  • Hi Inforequester,

    This isn't the greatest way to do it, but what I would do is add a timer, have the timer start on form load, have the interval very small (like 10ms) and in the handle for the timer's tick, use this code:

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            TextBox1.Text = TextBox1.Text.Replace(".com ", ".com, ")
        End Sub

    This will add the comma once the person puts a space, however it puts the cursor back at the beginning, so you'll have to add some more to that to make the cursor stay at the end of the text in the box.

    Hope this helps!

    Alex
    Please mark answers so we can solve someone else's problem!
    Saturday, September 12, 2009 4:58 PM

All replies

  • Hi Inforequester,

    This isn't the greatest way to do it, but what I would do is add a timer, have the timer start on form load, have the interval very small (like 10ms) and in the handle for the timer's tick, use this code:

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            TextBox1.Text = TextBox1.Text.Replace(".com ", ".com, ")
        End Sub

    This will add the comma once the person puts a space, however it puts the cursor back at the beginning, so you'll have to add some more to that to make the cursor stay at the end of the text in the box.

    Hope this helps!

    Alex
    Please mark answers so we can solve someone else's problem!
    Saturday, September 12, 2009 4:58 PM
  • Hi Mr_Brooks211,

    Great idea!!

    So try this then with one TextBox on a FORM.

    Use the backspace to delete if you make a mistake.


    Regards,

    John


    Option Strict On
    
    Public Class Form1
    
        Friend WithEvents TextReplaceTimer As New Timer
    
        Private EmailExtensions() As String = {".com", ".co.uk"}
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            TextReplaceTimer.Interval = 500
            TextReplaceTimer.Start()
    
        End Sub
    
        Private Sub TextReplaceTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextReplaceTimer.Tick
    
            For Each Str As String In EmailExtensions
                If TextBox1.Text.Contains(Str & " ") Then
                    TextBox1.Text = TextBox1.Text.Replace(Str & " ", Str & ", ")
                End If
            Next
            TextBox1.SelectionStart = TextBox1.Text.Length
    
        End Sub
    
    End Class

    I'm currently looking for work in Vb.Net software development. :-)
    Saturday, September 12, 2009 5:14 PM
  • Hi ALL,

    The TextChanged event is even better though as you can move
     the cursor back with the arrow keys easier.  :-)  ;-)


    Regards,

    John


    Option Strict On
    
    Public Class Form1
    
        Private EmailExtensions() As String = {".com", ".co.uk"}
    
        Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    
            For Each Str As String In EmailExtensions
                If TextBox1.Text.Contains(Str & " ") Then
                    TextBox1.Text = TextBox1.Text.Replace(Str & " ", Str & ", ")
                End If
            Next
            TextBox1.SelectionStart = TextBox1.Text.Length
    
        End Sub
    End Class

    I'm currently looking for work in Vb.Net software development. :-)
    • Proposed as answer by _asgar Saturday, September 12, 2009 7:01 PM
    Saturday, September 12, 2009 5:19 PM
  • This is commonly done with a regular expression .

    Simply use Google or another search engine for that

    http://www.google.com/search?hl=en&source=hp&q=regular+expression+email+address&aq=0&oq=regular+expression+email&aqi=g10
    Success
    Cor
    Saturday, September 12, 2009 6:38 PM