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
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.
Last edited by jmcilhinney; Nov 27th, 2022 at 05:50 AM.
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
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.
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
Re: How do I add hyphens at different positions in textbox on input?
Originally Posted by .paul.
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.
Re: How do I add hyphens at different positions in textbox on input?
Originally Posted by brenussein
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
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.