Results 1 to 9 of 9

Thread: How do I add hyphens at different positions in textbox on input?

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    8

    How do I add hyphens at different positions in textbox on input?

    Good Day Everyone,

    I want a textBox where I type numbers, it'll accordingly add hyphens (-) at certain positions.
    To be clear, I want it in the following format:
    P2002-3NVD-2CS

    Many Thanks.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,268

    Re: How do I add hyphens at different positions in textbox on input?

    Here's one I prepared earlier:

    https://www.vbforums.com/showthread....Box&highlight=

    Note that that is a custom control that you will use in place of a standard TextBox. You need to add the class to your project and build and then it will appear in the Toolbox window and you can add it like any other control. If you already have a regular TextBox, you can edit the designer code file and change the type to the new custom control.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    8

    Re: How do I add hyphens at different positions in textbox on input?

    Quote Originally Posted by jmcilhinney View Post
    Here's one I prepared earlier:

    https://www.vbforums.com/showthread....Box&highlight=

    Note that that is a custom control that you will use in place of a standard TextBox. You need to add the class to your project and build and then it will appear in the Toolbox window and you can add it like any other control. If you already have a regular TextBox, you can edit the designer code file and change the type to the new custom control.
    Thanks alot and I appreciate
    Is there a simple code to insert hyphen in the following manner P3003-2NVD-1CS
    Many thanks

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,268

    Re: How do I add hyphens at different positions in textbox on input?

    I didn't notice that your groups were different sizes the first time around. It was a while ago that I created that custom control so I can't recall the specifics but it would be easy enough to add a property that enabled you to specify multiple groups of different sizes. I'll take a closer look and get back to you.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,059

    Re: How do I add hyphens at different positions in textbox on input?

    Something like this should work...

    Code:
    Private ignoreEdit As Boolean = False
    
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If ignoreEdit Then Return
        Dim hyphenPositions() As Integer = {5, 10}
        ignoreEdit = True
        TextBox1.Text = TextBox1.Text.Replace("-", "")
        For Each hp As Integer In hyphenPositions
            If TextBox1.Text.Length >= hp Then TextBox1.Text = TextBox1.Text.Insert(hp, "-")
        Next
        TextBox1.SelectionStart = TextBox1.Text.Length
        ignoreEdit = False
    End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    8

    Re: How do I add hyphens at different positions in textbox on input?

    Quote Originally Posted by .paul. View Post
    Something like this should work...

    Code:
    Private ignoreEdit As Boolean = False
    
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If ignoreEdit Then Return
        Dim hyphenPositions() As Integer = {5, 10}
        ignoreEdit = True
        TextBox1.Text = TextBox1.Text.Replace("-", "")
        For Each hp As Integer In hyphenPositions
            If TextBox1.Text.Length >= hp Then TextBox1.Text = TextBox1.Text.Insert(hp, "-")
        Next
        TextBox1.SelectionStart = TextBox1.Text.Length
        ignoreEdit = False
    End Sub
    All grateful words fail to express my thanks to you.
    It is working well and good.

  7. #7
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,713

    Re: How do I add hyphens at different positions in textbox on input?

    Quote Originally Posted by brenussein View Post
    Good Day Everyone,

    I want a textBox where I type numbers, it'll accordingly add hyphens (-) at certain positions.
    To be clear, I want it in the following format:
    P2002-3NVD-2CS

    Many Thanks.
    Maybe a mask:
    Attached Images Attached Images  
    Please remember next time...elections matter!

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,059

    Re: How do I add hyphens at different positions in textbox on input?

    Quote Originally Posted by TysonLPrice View Post
    Maybe a mask:
    Thatís a MaskedTextBox BTW

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    37,863

    Re: How do I add hyphens at different positions in textbox on input?

    I've never been thrilled with the masked textbox. It always seems like it could have been easier in some way, though I can't say I ever bothered with figuring out how to do so.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width