[Visual Basic] only one comma in a textBox
Solved
kikou93
Posted messages
438
Status
Member
-
kikou93 Posted messages 438 Status Member -
kikou93 Posted messages 438 Status Member -
Hello everyone, I am seeking your help for the following subject:
I would like to limit the number of commas in a textBox to 1.
That is, if I insert a comma in a textBox and try to insert a second one, nothing happens.
I found the following code:
in:
https://forums.commentcamarche.net/forum/affich-22991758-vb-net-virgule-textbox
but nothing changes (even though the person who posted the code marked the subject as resolved)
I have already figured out how to prohibit the entry of letters and other symbols, and also how to replace the period with a comma
Here is the Code:
I just need that
Please help me and thank you in advance
Configuration: Windows / Chrome 49.0.2623.112
--
How it works is cool!
1000 Thanks friends
I would like to limit the number of commas in a textBox to 1.
That is, if I insert a comma in a textBox and try to insert a second one, nothing happens.
I found the following code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If (e.KeyChar = ",") Then If (TextBox1.Text.Contains(",")) Then e.Handled = True
End Sub
in:
https://forums.commentcamarche.net/forum/affich-22991758-vb-net-virgule-textbox
but nothing changes (even though the person who posted the code marked the subject as resolved)
I have already figured out how to prohibit the entry of letters and other symbols, and also how to replace the period with a comma
Here is the Code:
Dim desNuméro() As Char = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not desNuméro.Contains(e.KeyChar) And Not Asc(e.KeyChar) = 8 Then
e.Handled = True
End If
If e.KeyChar = "."c Or e.KeyChar = ","c Then
e.Handled = True
TextBox1.AppendText(",")
End If
End Sub I just need that
Please help me and thank you in advance
Configuration: Windows / Chrome 49.0.2623.112
--
How it works is cool!
1000 Thanks friends
2 answers
-
If TextBox.Text.Contains(",") then 'refuse input of the dot/comma.
Otherwise, also try copying and pasting into your textbox to see if it still works. (like "jhdk51258...54fg" -> Textbox)
--
I primarily work in VB6 and VB.NET, with a bit of C#, but moderation often brings me to other languages.
In VB.NET, remember to enable "Option Explicit" and "Option Strict" -
Good evening
Try this
Dim desNuméro() As Char = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not desNuméro.Contains(e.KeyChar) And Not Asc(e.KeyChar) = 8 Then
e.Handled = True
End If
If e.KeyChar = "."c Or e.KeyChar = ","c Then
e.Handled = True
If Not (TextBox1.Text.Contains(",")) Then TextBox1.AppendText(",")
End If
End Sub